4 ms·
data Sexp = Int Int | Nil | Cons Sexp Sexp megasum Nil = 0 megasum (Int i) = i megasum (Cons l r) = megasum l + megasum r
by rntz 12y ago
data Sexp = Int Int | Nil | Cons Sexp Sexp
megasum Nil = 0
megasum (Int i) = i
megasum (Cons l r) = megasum l + megasum r
- harryjo 12y agoIndeed. The first step to grokking Lisp, from Haskell (or any typed language), is to realize that Lisp's "list" is not equivalent to any single data type you've used before, but it's really a general weakly-structured representation of any and all structured types. Even this solution is not completely equivalent to Lisp megasum -- it fails to compile on the list `('a)` ;-)