0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

SICP読書女子会#18 2.2.1 ~ 2.2.2

Last updated at Posted at 2016-08-10

2.2.1

SICP/2.2.1.scm at master · cocodrips/SICP

Ex 2.21

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

(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

(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 に似ている。引数として手続きと要素のリストを取る。

(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 で手続きを 2つする処理はかけない?
; -> 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

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

(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))) をグラフ化する

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

Ex 2.25

(7)を取り出す!!

(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)
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?