SICP読書女子会#18 2.2.1 ~ 2.2.2


# 2.2.1
[SICP/2.2.1.scm at master · cocodrips/SICP](https://github.com/cocodrips/SICP/blob/master/2.1/2.2.1.scm)

## Ex 2.21
引数として数値のリストをとり、
それらの数値の二乗の列を持つリストを返す手続き square-list

```lisp
(display "==========Ex 2.21===========")
(newline)

(define (square-list items)
    (if (null? items)
        (list)
        (cons (* (car items) (car items)) (square-list (cdr items)))))

(display (square-list (list 1 2 3 4)))
(newline)
;(1 4 9 16)


(define (square-list items)
    (map (lambda (x) (* x x)) items))

(display (square-list (list 5 6 7 8)))
(newline)
;(25 36 49 64)
```

## Ex 2.22
反復プロセスで2.21を書くと、リストが逆順になる。
もがいてもだめ。なぜか。

前回の2.20で色々考えたやつだ。
リストの構造的に逆からは作れないから、 
1こ目の方法で書いて、reverseするのがいいんじゃないか？
という話だったはず。


[SICP読書女子会#17 2.2.1 > 別解放 filter && reverseでstack節約 - Qiita](http://qiita.com/cocodrips/items/e9d9142ed6a1d16ae733#%E5%88%A5%E8%A7%A3%E6%94%BE-filter--reverse%E3%81%A7stack%E7%AF%80%E7%B4%84)

```lisp
(define (square x) (* x x))

(define (square-list items)
    (define (iter things answer)
        (if (null? things)
            answer
            (iter (cdr things)
                    (cons (square (car things))
                    answer))))
    (iter items (list)))
(display (square-list (list 5 6 7 8)))
(newline)
;(64 49 36 25)

(define (square-list items)
    (define (iter things answer)
        (if (null? things)
            answer
            (iter (cdr things)
                (cons answer
                (square (car things))))))
    (iter items (list)))
(display (square-list (list 5 6 7 8)))
(newline)
;((((() . 25) . 36) . 49) . 64)
```


## Ex2.23

手続き for-each は map に似ている。引数として手続きと要素のリストを取る。

```lisp
(display "==========Ex 2.23===========")
(newline)


(define (for-each-orig f items)
    (cond 
        ((null? items) #t)
        (else 
            (f (car items))
            (for-each-orig f (cdr items)))))
        
     
;(define (for-each-orig f items)
;   (if
;       (null? items) 
;       #t
;       ((f (car items))
;        (for-each-orig f (cdr items)))))
     
; *** ERROR: invalid application: (#<undef> #t) なぜ～
; -> if で手続きを ２つする処理はかけない？
; -> beginやletでgroupingしてあげるのがいいかも

(for-each-orig 
    (lambda (x)
            (newline)
            (display x))
    (list 57 321 88))

;57
;321
;88
```

# 2.2.2

[SICP/2.2.2.scm at master · cocodrips/SICP](https://github.com/cocodrips/SICP/blob/master/2.1/2.2.2.scm)

Count-leaves
リスト構造の葉の部分を数えるやつ

```lisp
(display "*********2.2.2**********")
(newline)

(define linked-2-list (cons (list 1 2) (list 3 4)))
(display linked-2-list) 
;((1 2) 3 4)
(newline)

(define (count-leaves x)
    (cond 
        ((null? x) 0) 
        ((not (pair? x)) 1)
    (else 
        (+ 
            (count-leaves (car x))
            (count-leaves (cdr x))))))
            
(display (count-leaves linked-2-list))
(newline)
;4
```

## Ex 2.24
(1 (2 (3 4))) をグラフ化する

```lisp
(display "==========Ex 2.24===========")
(newline)
(display (list 1 (list 2 (list 3 4))))
;  *   
; / \
;1   *
;   / \
;   2  *
;     / \
;    3   4
(newline)
```


## Ex 2.25

 (7)を取り出す!!

```lisp
(display "==========Ex 2.25===========")
(newline)
(define l (list 1 3 (list 5 7) 9))
(display l) (newline)
(display (cdr (car (cdr (cdr l))))) (newline)
;(1 3 (5 7) 9)
;(7)
(define l (list (list 7)))
(display l) (newline)
(display (car l)) (newline)
;((7))
;(7)

(define l (list 1 (list 2 (list 3 (list 4 (list 5 (list 6 7)))))))
(display l) (newline)
(display (cdr (car (cdr (car (cdr (car (cdr (car (cdr (car (cdr l))))))))))))
(newline)
;(1 (2 (3 (4 (5 (6 7))))))
;(7)
```