Question

I tried to find information about andmap & ormap operations in chez scheme.

Still, I don't understand the use of these operations, and what is the difference between it and map.

Was it helpful?

Solution

In pseudo-Scheme,

(andmap f xs)  ==  (fold and #t (map f xs))
(ormap  f xs)  ==  (fold or  #f (map f xs))

except that:

  1. You can't use and and or in this way.
  2. andmap and ormap can short-circuit processing the list.

That is, except for slightly different short-circuiting behavior,

(andmap f (list x1 x2 x3 ...))  ==  (and (f x1) (f x2) (f x3) ...)
(ormap  f (list x1 x2 x3 ...))  ==  (or  (f x1) (f x2) (f x3) ...)

OTHER TIPS

Petite Chez Scheme Version 8.3
Copyright (c) 1985-2011 Cadence Research Systems

> (define (andmap f xs)
    (cond ((null? xs) #t)
          ((f (car xs))
            (andmap f (cdr xs)))
          (else #f)))
> (define (ormap f xs)
    (cond ((null? xs) #f)
          ((f (car xs)) #t)
          (else (ormap f (cdr xs)))))
> (andmap even? '(2 4 6 8 10))
#t
> (andmap even? '(2 4 5 6 8))
#f
> (ormap odd? '(2 4 6 8 10))
#f
> (ormap odd? '(2 4 5 6 8))
#t

Courtesy of practical-scheme.net:

(ormap procedure list1 list2 ...)

Applies procedure to corresponding elements of the lists in sequence until either the lists run out or procedure returns a true value.

(andmap procedure list1 list2 ...)

Applies procedure to corresponding elements of the lists in sequence until either the lists run out or procedure returns a false value.

http://practical-scheme.net/wiliki/schemexref.cgi/ormap

Figured this was worth putting here, since this StackOverflow question is the first result on Google.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top