Chez スキームでの andmap & ormap 操作に関する情報を見つけようとしました。
それでも、これらの操作の使用法と、それとマップの違いはわかりません。
Chez スキームでの andmap & ormap 操作に関する情報を見つけようとしました。
それでも、これらの操作の使用法と、それとマップの違いはわかりません。
疑似スキームでは、
(andmap f xs) == (fold and #t (map f xs))
(ormap f xs) == (fold or #f (map f xs))
それ以外で:
and
このようにandを使用することはできませんor
。andmap
リストの処理をormap
短絡できます。つまり、わずかに異なる短絡動作を除いて、
(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) ...)
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