Hacker Newsnew | past | comments | ask | show | jobs | submit | antonvs8's commentslogin

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?"


neat code golf reason! :)

btw, I've been to some of the recent LispNYC events, have you been to recent ones?


I live about two hours away, more by public transport, and don't get in to the city much. I'll try to make a meeting in 2013...


Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: