如何 运行 和方案中的布尔值列表?
How to run and on a list of booleans in scheme?
我正在对列表进行映射以在其上执行逻辑以做出决策,并且我不断地回来产生这样的东西:
((#t #t #f)(#t #t #t)(#t #t #t)(#f #t #t)(#t #f #t))
我需要的是将上面的list列表改成
(#f #t #t #f #f)
我曾想过使用 and
来实现我想要的逻辑; (and #f #t #t) ⇒ #f
。但我不确定如何移动布尔值列表,以便我可以 运行 and
处理它们。我考虑过使用 apply
但它没有用:
(apply and '(#t #t #f))
While compiling expression:
Syntax error:
unknown location: source expression failed to match any pattern in form and
有没有办法 运行 and
列表中的布尔值?
因为, we need to do something like this (using map
and foldl
):
(define lst '((#t #t #f) (#t #t #t) (#t #t #t) (#f #t #t) (#t #f #t)))
(map (lambda (sl)
(foldl (lambda (e acc) (and e acc)) #t sl))
lst)
=> '(#f #t #t #f #f)
在某些 Scheme 方言(例如 Racket)中,我们可以利用 curry
, identity
and andmap
:
编写更简单的表达式
(map (curry andmap identity) lst)
=> '(#f #t #t #f #f)
(define input '((#t #t #f)(#t #t #t)(#t #t #t)(#f #t #t)(#t #f #t)))
(map (lambda (a) (every (lambda(x) x) a))
input)
我正在对列表进行映射以在其上执行逻辑以做出决策,并且我不断地回来产生这样的东西:
((#t #t #f)(#t #t #t)(#t #t #t)(#f #t #t)(#t #f #t))
我需要的是将上面的list列表改成
(#f #t #t #f #f)
我曾想过使用 and
来实现我想要的逻辑; (and #f #t #t) ⇒ #f
。但我不确定如何移动布尔值列表,以便我可以 运行 and
处理它们。我考虑过使用 apply
但它没有用:
(apply and '(#t #t #f))
While compiling expression:
Syntax error:
unknown location: source expression failed to match any pattern in form and
有没有办法 运行 and
列表中的布尔值?
因为map
and foldl
):
(define lst '((#t #t #f) (#t #t #t) (#t #t #t) (#f #t #t) (#t #f #t)))
(map (lambda (sl)
(foldl (lambda (e acc) (and e acc)) #t sl))
lst)
=> '(#f #t #t #f #f)
在某些 Scheme 方言(例如 Racket)中,我们可以利用 curry
, identity
and andmap
:
(map (curry andmap identity) lst)
=> '(#f #t #t #f #f)
(define input '((#t #t #f)(#t #t #t)(#t #t #t)(#f #t #t)(#t #f #t)))
(map (lambda (a) (every (lambda(x) x) a))
input)