Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

Lisp and Scheme

Name: Anonymous 2007-03-08 8:14 ID:fc2iv5gF

Why is this shit still around? Didn't Haskell obsolete it 20 years ago?

Compare map:

 (define (map f lst)
   (let loop ((lst lst)
              (res '()))
     (if (null? lst)
       (reverse res)
       (loop (cdr lst)
             (cons (f (car lst)) res)))))

vs.

 map f []     = []
 map f (x:xs) = f x : map f xs

Name: Anonymous 2007-07-15 20:17 ID:3OgO/mI2

first of all, lisp's map is different than haskell map. Lisp's map takes a variable number of arguments, so you can write something like

(map + '(1 2 3) '(4 5 6)) --> (5 7 9)


still, if you want a nice definition of a haskell-like map in scheme, I'm guessing it would be something like

(define (map f list)
  (foldr nil (lambda (e acc) (cons (f e) acc)) list))

which of course is the equivalent of

map f list = foldr [] (/ e acc -> (f e):acc)) list

which of course may seem shorter, but only because of syntax, and if haskell macros are any indication of, it matters little.
(The same applies to pattern matching vs. accesor functions, unless you like 20+ functions breaking when you add a field in your data type).

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List