[plt-scheme] evaluation order for fold

From: Veer (diggerrrrr at gmail.com)
Date: Mon Feb 9 02:33:42 EST 2009

Hello ,

Consider following  functions :

(define (search-each los )
    (cond
      [(empty? los) empty]
      [(member (first los) visited) (search-each (rest los))]
      [else (cons (search-tree (first los)) (search-each (rest los)))]))


and the same above function using foldr :

(define (search-each-fold los)
  (foldr (lambda (s r)
           (if (member s visited)
               r
               (cons (search-tree s) r))) empty los))


Also variable 'visited' is global and updated by search-tree function .

When given the same input both function produces the result which though
correct but are in different order.

My question is are these two function equivalent?

I see that in the line [else (cons (search-tree (first los))
(search-each (rest los)))]))
(search-tree (first los)) is evaluated first

But  the fold example does not seem to follow this.

Thanks
Veer


Posted on the users mailing list.