I wasn't insisting on lists, but on square brackets, to match McCarthy's syntax. This golf game has strict rules!
If we drop the brackets we may as well go the whole way and use curried functions, in which case the diff function looks like:
diff y x =
atom y → (eq y x → ONE $ ZERO) $
eq (car y) PLUS →
cons PLUS (maplist (cdr y) (\z -> diff (car z) x)) $
eq (car y) TIMES →
cons PLUS (maplist (cdr y)
(\z -> cons TIMES
(maplist (cdr y)
(\w -> z /= w → car w $
diff (car w) x)))) $
error ("Unexpected term" ++ show y)
Good tip in general. Symbolic differentiation was McCarthy's motivation in 1960. My own motivation was nothing more sophisticated than "I wonder how much Haskell can look like M-expressions, without resorting to Template Haskell?"
If we drop the brackets we may as well go the whole way and use curried functions, in which case the diff function looks like: