# weima learns to program

my attempt to do the exercises in sicp.

## Wednesday, July 16, 2008

### sicp exercise 2.21

;  Exercise 2.21. The procedure square-list takes a list of numbers as argument and returns a list of the
;  squares of those numbers.
;      (square-list (list 1 2 3 4))
;      (1 4 9 16)
;  Here are two different definitions of square-list. Complete both of them by filling in the missing
;  expressions:
;      (define (square-list items)
;  (if (null? items)
;      nil
;  (cons <??> <??>)))

;      (define (square-list items)
;  (map <??> <??>))

(define nil (list))

(define (square x)( * x x))

; produces a list of square of items in the input list
(define square-list (lambda ( items )
(if (null? items) nil
(cons (* (car items) (car items)) (square-list (cdr items))))))

; produces a list of square of items in the input list, using map
(define square-list1 (lambda (items)
(map (lambda (x) (* x x)) items)))

; produces a list of square of items in the input list, but in reverse order
(define (square-list2 items)
(if (null? things)
(iter (cdr things) (cons (square (car things)) answer))))
(iter items nil))

; produces a mess of square of items in the input list
(define (square-list3 items)
(if (null? things)