Home > Archive > Scheme > June 2004 > prob using 'and' with DrScheme
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 |
prob using 'and' with DrScheme
|
|
| Alex Gittens 2004-06-03, 7:27 pm |
| I wrote a program that uses the SICP-style function accumulate, which
works under STK. But it doesn't work under DrScheme; here's the relevant
code:
; given an operator, an initial value, and a list of values, returns the
result
; of applying the operator recursively to the list
(define (accumulate op init ls)
(define (internal-accum list accum)
(if (null? list)
accum
(internal-accum (cdr list) (op accum (car list)))))
(internal-accum ls init))
; tests if a polynomial is the zero polynomial for its ring
(define (zero-poly? x)
(accumulate and #t (map
(lambda (coeff) (ring-zero? (find-ring x) coeff))
(get-poly-coeff-list x))))
DrScheme gives the error
and: bad syntax in: and
and highlights my use of 'and' in the zero-poly? predicate.
What is causing this?
Thanks,
Alex
--
| |
| David Van Horn 2004-06-03, 7:27 pm |
| Alex Gittens wrote:
> ; tests if a polynomial is the zero polynomial for its ring
> (define (zero-poly? x)
> (accumulate and #t (map
> (lambda (coeff) (ring-zero? (find-ring x) coeff))
> (get-poly-coeff-list x))))
>
> DrScheme gives the error
> and: bad syntax in: and
> and highlights my use of 'and' in the zero-poly? predicate.
>
> What is causing this?
`and' is not a function, but here you are using it like a function.
David
| |
| Matthias Felleisen 2004-06-03, 7:27 pm |
| Alex Gittens wrote:
> I wrote a program that uses the SICP-style function accumulate, which
> works under STK. But it doesn't work under DrScheme; here's the relevant
> code:
>
> ; given an operator, an initial value, and a list of values, returns the
> result
> ; of applying the operator recursively to the list
> (define (accumulate op init ls)
> (define (internal-accum list accum)
> (if (null? list)
> accum
> (internal-accum (cdr list) (op accum (car list)))))
> (internal-accum ls init))
>
> ; tests if a polynomial is the zero polynomial for its ring
> (define (zero-poly? x)
> (accumulate and #t (map
> (lambda (coeff) (ring-zero? (find-ring x) coeff))
> (get-poly-coeff-list x))))
>
> DrScheme gives the error
> and: bad syntax in: and
> and highlights my use of 'and' in the zero-poly? predicate.
>
> What is causing this?
As David points out, and is syntax in Scheme so you can't pass it around as if
it were a function.
One could have defined and with mzscheme's identifier syntax rules like this:
(define-syntax and
(syntax-id-rules ()
[(_ exp ...) (true:and exp ...)]
[_ (lambda x (andmap identity x))]
[(set! my-and exp) (error 'myand "bad")]))
Then you could use it as above, but this wasn't done. -- Matthias
| |
| ifconfig 2004-06-03, 7:27 pm |
| and, surprisingly, is not a procedure, it's a syntax. That means that not
all of its arguments have to be evaluated: e.g., (and #f (display "This
doesn't print")) wouldn't display that text since no matter what the result
of the expression (display "This doesn't print") is, the expression (and #f
(display "This doesn't print")) is false.
What you should do is define an and procedure:
(define (and-procedure . x)
(if (equal? x empty)
#t
(and (car x) (apply and-procedure (cdr x)))))
You can use this and-procedure with your zerp-poly?.
ifconfig.
"Alex Gittens" <swiftset removethis at imap dot removethis cc> wrote in
message news:opr8kidjmxjkl6vx@news.ev1.net...
> I wrote a program that uses the SICP-style function accumulate, which
> works under STK. But it doesn't work under DrScheme; here's the relevant
> code:
>
> ; given an operator, an initial value, and a list of values, returns the
> result
> ; of applying the operator recursively to the list
> (define (accumulate op init ls)
> (define (internal-accum list accum)
> (if (null? list)
> accum
> (internal-accum (cdr list) (op accum (car list)))))
> (internal-accum ls init))
>
> ; tests if a polynomial is the zero polynomial for its ring
> (define (zero-poly? x)
> (accumulate and #t (map
> (lambda (coeff) (ring-zero? (find-ring x) coeff))
> (get-poly-coeff-list x))))
>
> DrScheme gives the error
> and: bad syntax in: and
> and highlights my use of 'and' in the zero-poly? predicate.
>
> What is causing this?
>
> Thanks,
> Alex
>
> --
|
|
|
|
|