Hatena::Groupsicp

EhrenのSICP学習場

2010-01-02

2.2.2階層構造

00:31

問題2.24


問題2.25

(define ex225_1 (list 1 3 (list 5 7) 9))
(car (cdr (car (cdr (cdr ex225_1)))))

(define ex225_2 (cons (cons 7 '()) '()))
(car (car ex225_2))

(define ex225_3 (list 1 (list 2 (list 3 (list 4 (list 5 (list 6 7)))))))
(car (cdr (car (cdr (car (cdr (car (cdr (car (cdr (car (cdr ex225_3))))))))))))

問題2.26

(define x (list 1 2 3))
(define y (list 4 5 6))

(append x y)
;; -> (1 2 3 4 5 6)
(cons x y)
;; -> ((1 2 3) 4 5 6)
(list x y)
;; -> ((1 2 3) (4 5 6))

問題2.28

(define (deep-reverse list)
  (define (deep-reverse-iter list1 list2)
    (cond ((null? list1) list2)
          ((pair? (car list1))
           (deep-reverse-iter (cdr list1)
                            (append (cons (deep-reverse (car list1)) '())  list2)))
          (else (deep-reverse-iter (cdr list1) (cons (car list1) list2)))))
    (deep-reverse-iter list '()))

append使うとリスト長が長い場合、実行効率悪くなるから嫌いなんだけど他に浮かばないorz

2.2.1 並びの表現

20:06

問題2.20

(define (same-parity h . xs)
  (define (filter ys)
    (if (null? ys)
        '()
        (if ((if (odd? h) odd? even?) (car ys))
            (cons (car ys) (filter (cdr ys)))
            (filter (cdr ys))
        )))
  (cons h (filter xs)))
(same-parity 1 2 3 4 5 6 7)
;; (1 3 5 7)
(same-parity 2 3 4 5 6 7)
;; (2 4 6)

ifをたくさん使っていていけてない。他の人の回答をみてみる。


no title

(define (same-parity h . ts)
  (define pred (if (even? h) even? odd?))
  (define (filter p xs)
    (cond ((null? xs) xs)
          ((p (car xs)) (cons (car xs) (filter p (cdr xs))))
          (else (filter p (cdr xs)))))
  (cons h (filter pred ts)))

なるほど事前に先頭の偶奇を調べておいて、高階関数で渡している。実行効率はこちらの方がいいはず。

そして、condがあるのを忘れてたorz.


ex2.21

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

map版

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

ex2.22

反復プロセスの場合の動きを考えて見る

(square-list (list 1 2 3 4))

のばあい

(iter (list 1 2 3 4) '())
(iter (list 2 3 4) (cons (square 1) '()))
(iter (list 3 4) (cons (square 2) (list 1)))
(iter (list 4) (cons (square 3) (list 4 1)))
(iter (list '()) (cons (square 4) (list 9 4 1)))
(lsit 16 9 4 1)

ex2.23

(define (my-for-each proc items)
  (if (not (null? items))
      ( (lambda (func list) (func (car list))
            (my-for-each func (cdr list))) proc items)))

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

MildredMildred2011/08/19 01:08A bit suprrised it seems to simple and yet useful.

gnuqxwfvzfgnuqxwfvzf2011/08/19 21:261zTJj3 <a href="http://vuptpduwcdbj.com/">vuptpduwcdbj</a>

bcmfbbsbvmhbcmfbbsbvmh2011/08/20 01:33uldP2L , [url=http://gmcgnqlglnmx.com/]gmcgnqlglnmx[/url], [link=http://cadfsuiyadjn.com/]cadfsuiyadjn[/link], http://bswyvfouxqff.com/

wjuiyzbzwjuiyzbz2011/08/25 17:59ndZz02 <a href="http://aumoltnsusut.com/">aumoltnsusut</a>

bxrylabxryla2011/08/30 19:22arvgkU , [url=http://hnimstkmevif.com/]hnimstkmevif[/url], [link=http://tovxblobpdhl.com/]tovxblobpdhl[/link], http://drwtihtrxmou.com/