Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check if a list contains only #t

Tags:

scheme

racket

I was trying with the following code in racket and MIT scheme, surprise me that the compiler throw err

(foldr and #t '(#t #t #f))

Is there any way to use reduce/fold way to check if a list contains only true or false? I know a lambda can do the job, but it really make we wonder why this is not a valid code. I remember I can do it in Haskell.....

TIA.

like image 517
user734736 Avatar asked May 02 '11 16:05

user734736


3 Answers

and is a macro, so it doesn't have a value by itself. Specifically, it short-circuits evaluation, and using it as you tried to will not make any sense. For that reason, Racket has andmap which you can use in such cases. (Other implementations have similar functionality under different names -- for example, srfi-1 uses every.)

like image 165
Eli Barzilay Avatar answered Oct 23 '22 14:10

Eli Barzilay


And is a macro and can not be used as a function. Put it in a function:

(foldr (lambda (a b) (and a b)) #t '(#t #t #f))

like image 26
knivil Avatar answered Oct 23 '22 13:10

knivil


This works in guile:

(primitive-eval (cons 'and '(#t #f)))
like image 40
drysdam Avatar answered Oct 23 '22 15:10

drysdam