For each and for the card in the diagram

Is there any difference between these two functions in the schema? I am using Dr Racket R5RS to create a simulation game and I couldn't decide which one is better.

+3


source to share


2 answers


for-each

evaluates the given function in the list items from left to right and discards the return value of the function. It is ideal for performing side-actions on every item in a list.

map

evaluates the given function on list items in no particular order (although most implementations will use either right-to-left or left-to-right) and stores the return value of the function to return to the caller. It is ideal for performing purely functional processing on each list item.

If the return value map

will not be used, it is better to use for-each

. This way it shouldn't bother collecting return values ​​from function calls.

(Other than that: in Clojure, the return value map

is a lazy sequence, which means that this function is only called on materialized elements.)


Details of technical implementation. The simplified version with one list is for-each

usually done like this:



(define (for-each func lst)
  (let loop ((rest lst))
    (unless (null? rest)
      (func (car rest))
      (loop (cdr rest)))))

      

Really straightforward and guarantees left-to-right order. Compare to the simplified map

single-list version :

(define (map func lst)
  (let recur ((rest lst))
    (if (null? rest)
        '()
        (cons (func (car rest)) (recur (cdr rest))))))

      

The schema does not specify the order in which the function arguments are evaluated. Thus, for a type expression, (foo (bar) (baz) (qux))

calls bar

, baz

and qux

can be made in any order, but they will all complete before being called foo

.

In this case, the event can happen first (func (car rest))

, or it can happen after (recur (cdr rest))

. This was not guaranteed in any way. This is why people say it map

doesn't guarantee the order of evaluation.

+4


source


There is a big difference: it map

returns a list containing the results of applying this procedure to the elements of the list (or lists), and for-each

returns void.

> (for-each (Ξ» (x) (add1 x)) '(1 2 3))
> (map (Ξ» (x) (add1 x)) '(1 2 3))
'(2 3 4)

      

You use map

whenever you need the result of a computation, and you use for-each

when you are interested in the side effects of this procedure.



The second important difference is that it for-each

ensures that the given procedure applies to ordered items. For map

, although the order of the original list [s] is returned in the list, it is not guaranteed that the calls were made in order.

further here

+2


source







All Articles