Help us understand the problem. What is going on with this article?

SICP読書女子会#18 2.2.1 ~ 2.2.2

More than 3 years have passed since last update.

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)
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
No comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
ユーザーは見つかりませんでした