Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

the difference between if and cond?

Tags:

scheme

sicp

i'm learning sicp now and do the ex2.23 i have wrirten the following code:

(define (for-each proc items)
   (if (null? items)
       #t
       ((proc (car items))
        (for-each proc (cdr items)))))

but when running, cause error: procedure application: expected procedure, given: #; arguments were: ()

i think i know the reason: I call the for-each function recursively, every called for-each wanted to return value

but when i have modified the code:

(define (for-each proc items)
  (cond ((null? items) #t)
        (else (proc (car items)) (for-each proc (cdr items)))))

it runs well. I don't understand, why? in cond, does every called for-each no need to return value?

i used DrScheme, and choose language SICP

i'm not a native speaker of english, so if there is sth which isn't described clearly, pls tell me

like image 970
Vincent Avatar asked Feb 28 '23 00:02

Vincent


1 Answers

but when running, cause error: procedure application: expected > procedure, given: #; arguments were: ()

i think i know the reason: I call the for-each function recursively, > every called for-each wanted to return value

No, it is because in the alternative clause of if you have the combination ((proc (car items)) (for-each proc (cdr items))). You intended to evaluate the two combinations (proc (car items)) and (for-each proc (cdr items)) sequentially, and to that end you thought putting them in another pair of parentheses would work. But in actuality, what you have specified is that the result of (proc (car items)) is a procedure to be applied to the argument which is the return value of (for-each proc (cdr items)). This is not the case, and you get an error. The key point being that parentheses in Lisp are not for grouping, but have a definite significance.

The problem is that if can only have a single combination in that position, whereas you want to have two in a row. On the other hand, cond does not suffer such a restriction; you can put as long a sequence of individual combinations in the consequent part of a cond clause as your heart desires. This state of affairs is simply how the language is defined to work.

You can just as well use cond in these situations, but if you still want to use if there are some options for stuffing multiple combinations into one. E. g. you can create a lambda procedure whose body is the two combinations and immediately fire it off:

(define (for-each proc items)
   (if (null? items)
       #t
       ((lambda ()
          (proc (car items))
          (for-each proc (cdr items)) )) ))

Or you can use begin which is actually meant to be used for such a purpose:

(define (for-each proc items)
   (if (null? items)
       #t
       (begin
          (proc (car items))
          (for-each proc (cdr items)) ) ))
like image 162
Nietzche-jou Avatar answered Apr 30 '23 09:04

Nietzche-jou