3

Chez スキームでの andmap & ormap 操作に関する情報を見つけようとしました。

それでも、これらの操作の使用法と、それとマップの違いはわかりません。

4

3 に答える 3

4

疑似スキームでは、

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

それ以外で:

  1. andこのようにandを使用することはできませんor
  2. 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) ...)
于 2011-11-22T18:22:18.453 に答える
1
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
于 2011-11-22T18:56:40.067 に答える