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
Post a Comment