Home > Archive > Scheme > November 2007 > and, or logical functions
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
and, or logical functions
|
|
| rocco.rossi@gmail.com 2007-11-23, 4:30 am |
| I would really like to know if there are any and/or logical
"functions" (with variable argument lists) in scheme. I am well aware
that "and", "or" are actually macros and so they cannot be passed as
argument functions (which is really what I want). It would be to
know how they can be implemented in source. But I was also wondering
if any of the more popular implementations of Scheme such as Guile,
PLT Scheme, Scheme 48, and so on, have defined their own versions (I
understand that the standard does not appear to include such
procedures).
Thank you.
Rock
| |
| Jussi Piitulainen 2007-11-23, 4:30 am |
| rocco.rossi@gmail.com writes:
> I would really like to know if there are any and/or logical
> "functions" (with variable argument lists) in scheme. I am well
> aware that "and", "or" are actually macros and so they cannot be
> passed as argument functions (which is really what I want). It would
> be to know how they can be implemented in source. But I was
> also wondering if any of the more popular implementations of Scheme
> such as Guile, PLT Scheme, Scheme 48, and so on, have defined their
> own versions (I understand that the standard does not appear to
> include such procedures).
They are quite straightforward to implement. The hardest problem is
what to call them.
(define (every ok? xs) ; (every odd? '(1 2 3)) => #f
(or (null? xs)
(and (ok? (car xs))
(every ok? (cdr xs)))))
(define (all-truish? . xs) ; (all-truish? 1 2 3) => #t
(every values xs))
Similarly for (some ok? xs) and (exists-truish? x ...).
I'm sure various implementations have versions in their libraries. At
least they'll have equivalents of `every' and `some'.
(I didn't test the code.)
| |
| Abdulaziz Ghuloum 2007-11-23, 8:11 am |
| rocco.rossi@gmail.com wrote:
> I would really like to know if there are any and/or logical
> "functions" (with variable argument lists) in scheme. I am well aware
> that "and", "or" are actually macros and so they cannot be passed as
> argument functions (which is really what I want). It would be to
> know how they can be implemented in source. But I was also wondering
> if any of the more popular implementations of Scheme such as Guile,
> PLT Scheme, Scheme 48, and so on, have defined their own versions (I
> understand that the standard does not appear to include such
> procedures).
What you're looking for is often called andmap (chez, ikarus, plt),
for-all (r6rs), and every (srfi-1), among other possible names.
(andmap f '()) =~ #t
(andmap f (cons x '())) =~ (f x)
(andmap f (cons x xs)) =~ (and (f x) (andmap f xs))
where "=~" means "almost the same as".
Aziz,,,
| |
| Jens Axel Soegaard 2007-11-23, 8:11 am |
| rocco.rossi@gmail.com skrev:
> I would really like to know if there are any and/or logical
> "functions" (with variable argument lists) in scheme. I am well aware
> that "and", "or" are actually macros and so they cannot be passed as
> argument functions (which is really what I want).
The easy definitions are:
(define (andf x y) (and x y))
(define (orf x y) (or x y))
It is common to see (lambda (x y) (and x y)) where
and-as-a-function is needed.
More general versions are shown in the other responses.
--
Jens Axel Søgaard
| |
| Barak A. Pearlmutter 2007-11-23, 8:11 am |
| rocco.rossi@gmail.com wrote:
> I would really like to know if there are any and/or logical
> "functions" (with variable argument lists) in scheme.
The functional analogues to AND and OR are traditionally
called AND* and OR*. Trivially defined,
(define (and* . args)
(or (null? args) (and (car args) (apply and* (cdr args)))))
(define (or* . args)
(and (pair? args) (or (car args) (apply or* (cdr args)))))
In fact these are common homework problems. I've even assigned
them myself on occasion. Hey wait a second...! Okay I guess I'll
leave a minor bug in the above definitions.
--
Barak A. Pearlmutter
Hamilton Institute & Dept Comp Sci, NUI Maynooth, Co. Kildare, Ireland
http://www.bcl.hamilton.ie/~barak/
| |
| Kjetil S. Matheussen 2007-11-23, 7:12 pm |
| On Fri, 23 Nov 2007 rocco.rossi@gmail.com wrote:
> I would really like to know if there are any and/or logical
> "functions" (with variable argument lists) in scheme. I am well aware
> that "and", "or" are actually macros and so they cannot be passed as
> argument functions (which is really what I want). It would be to
> know how they can be implemented in source. But I was also wondering
> if any of the more popular implementations of Scheme such as Guile,
> PLT Scheme, Scheme 48, and so on, have defined their own versions (I
> understand that the standard does not appear to include such
> procedures).
>
Here's a low-level macro which transforms a macro to a function:
(define-macro (macro->func macro)
(define args (gensym))
(define num-args (gensym))
`(lambda ,args
(case (length ,args)
,@(map (lambda (n)
`((,n) (,macro ,@(map (lambda (n)
`(list-ref ,args ,n))
(iota n)))))
(iota 2000)))))
(apply (macro->func and) '(2 3 4 5))
-> 5
(apply (macro->func or) '(2 3 4 5))
-> 2
Theres a limitation here though. Your macro can only have 2000 arguments
when run as a function, but that can easely be worked around by increasing
"2000" in the macro above, or using eval.
|
|
|
|
|