14

Programming language: Scheme/DrRacket

We're currently going over map, filter, and foldr in my comp sci class. I understand that all three can be used to create abstract functions, but I am honestly a little confused about the difference between the three and when I'd use each one.

Anyone care to explain what each is used for and how they are different? Unfortunately my book is not very clear.

Tikhon Jelvis
  • 67,485
  • 18
  • 177
  • 214
Lukas Pleva
  • 381
  • 2
  • 5
  • 17
  • 2
    Out of curiosity, which book are you using? I'm going to plug a book one of my professors wrote called *[Simply Scheme](http://www.cs.berkeley.edu/~bh/ss-toc2.html)*; it's available for free online and is worth a look. He was the best professor I've had, so I suspect the book is very good although I haven't read it myself. – Tikhon Jelvis Oct 24 '11 at 08:35

2 Answers2

42

The basic idea is that all three are ways of applying some function to all the elements of a list.

Map is perhaps the simplest--you just apply the function to each element of the list. This is basically the same as a for-each loop in other languages:

 (map (lambda (x) (+ x 1)) '(1 2 3))
   => (2 3 4)

Basically, map is like this: (map f '(1 2 3)) is the same as (list (f 1) (f 2) (f 3)).

Filter is also simple: the function acts like an arbiter, deciding whether to keep each number. Imagine a really picky eater going through a menu and whining about the things he won't eat ;)

 (filter (lambda (x) (equal? x 1)) '(1 2 3))
   => (1)

Fold is the hardest to understand, I think. A more intuitive name would be "accumulate". The idea is that you're "combining" the list as you go along. There are some functions in ever day use that are actually folds--sum is a perfect example.

 (foldr + 0 '(1 2 3)) 
   => 6

You can think of the fold as taking the function and putting it between each element in the list: (foldr + 0 '(1 2 3)) is the same as 1 + 2 + 3 + 0.

Fold is special because, unlike the other two, it usually returns a scalar value--something that was the element of the list rather than the list itself. (This isn't always true, but think of it this way for now anyway.)

Note that I may not have gotten every detail of the code perfect--I've only ever used a different, older implementation of Scheme so I might have missed some Racket details.

John Clements
  • 16,895
  • 3
  • 37
  • 52
Tikhon Jelvis
  • 67,485
  • 18
  • 177
  • 214
  • Always glad to help. If anything still isn't clear, feel free to tell me so I can try to revise my explanation. – Tikhon Jelvis Oct 24 '11 at 08:46
  • Would (foldl + 0 '(1 2 3)) be the same as 3 + 2 + 1 + 0 ? – No_name Nov 30 '12 at 06:04
  • @No_name: Yep. You can't really tell the two apart with `+` because it's commutative, but try `(foldl cons '() '(1 2 3))` and `(foldr cons '() '(1 2 3))`. – Tikhon Jelvis Nov 30 '12 at 06:23
  • More accurate would be to say that you can't tell them apart because `+` is associative. `foldl` and `foldr` give the same result if the operation they're passed is associative. – Pig Sep 03 '17 at 12:55
2

I can recommend these finger exercises (and the text that comes before):

http://htdp.org/2003-09-26/Book/curriculum-Z-H-27.html#node_idx_1464

soegaard
  • 30,661
  • 4
  • 57
  • 106