3

How can I map a function for instance (square x) over a list of lists (list (list 1 2) (list 3 4)) and at the same time concatenates the result. For example the result would be (1 4 9 16). I can't find any detailed explanations on the web... thanks for any advice!

Best Regards, Eunice

1 Answers1

2

Try this:

(append-map (lambda (slst) (map sqr slst))
            (list (list 1 2) (list 3 4)))

The innermost map squares each number, and the outermost append-map traverses the sublists, appending them at the end. In case your language doesn't support append-map, here's an equivalent solution:

(apply append
       (map (lambda (slst) (map sqr slst))
            (list (list 1 2) (list 3 4))))
Óscar López
  • 232,561
  • 37
  • 312
  • 386