Using AND with the apply function in Scheme

前端 未结 9 1026
被撕碎了的回忆
被撕碎了的回忆 2020-12-03 05:26

Why doesn\'t the following work?

(apply and (list #t #t #f))

While the following works just fine.

(apply + (list 1 3 2))
         


        
9条回答
  •  生来不讨喜
    2020-12-03 05:56

    I've stumbled across the same problem and found an elegant solution in Racket. Since the problem is that "and" is a macro and not a function in order to prevent the evaluation of all its arguments, I've read a little on "lazy racket" and found that "and" is a function in that language. So I came up with the following solution where I just import the lazy and as "lazy-and":

    #lang racket
    (require (only-in lazy [and lazy-and]))
    
    (define (mm)
      (map number? '(1 2 3)))
    
    (printf "~a -> ~a\n" (mm) (apply lazy-and (mm)))
    

    which yields

    (#t #t #t) -> #t
    

提交回复
热议问题