scheme - DrRacket gives the " '(#<procedure> #<procedure> #<procedure>) " output -


i doing ex2.22 of sicp, exercise gives procedure intends square list output reverses list. when type in drracket output unexpected. code:

(define (square-list items)  (define (iter things answer)   (if (null? things)     answer     (iter (cdr things)           (cons (square (car things))                 answer))))  (iter items null))  (square-list (list 1 2 3)) 

the expected output (9 4 1) '(#<procedure> #<procedure> #<procedure>).i don't know why.

the result depends on definition of square, have defined in wrong way. here right definition of square returns correct answer:

(define (square item)   (* item item))  (define (square-list items)  (define (iter things answer)   (if (null? things)     answer     (iter (cdr things)           (cons (square (car things))                 answer))))  (iter items null))  (square-list (list 1 2 3))  (9 4 1) 

here can note result, given way in calculate it, reversed respect original list. if want obtain in same order add, instance, reverse @ end of call of iter:

(define (square-list items)  (define (iter things answer)   (if (null? things)     answer     (iter (cdr things)           (cons (square (car things))                 answer))))  (reverse (iter items null))) 

Comments

Popular posts from this blog

java - UnknownEntityTypeException: Unable to locate persister (Hibernate 5.0) -

python - ValueError: empty vocabulary; perhaps the documents only contain stop words -

ubuntu - collect2: fatal error: ld terminated with signal 9 [Killed] -