2011-11-22 42 views
1

我試圖在chez方案中找到關於和映射&或映射操作的信息。Andmap ormap - chez scheme

不過,我不明白這些操作的用法,它和map有什麼區別。

+1

所有這些功能都切斯計劃用戶指南中詳細記錄。 – erjiang

回答

3

在僞方案,

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

不同之處在於:

  1. 你不能以這種方式使用andor
  2. andmapormap可以對列表進行短路處理。

也就是說,除了略有不同短路行爲,

(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) ...) 
0
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