CS61A Fall 2021 discussion 10 scheme相关——q1-q6

Q1: Virahanka-Fibonacci

Write a function that returns the n-th Virahanka-Fibonacci number.

(define (vir-fib n)
    (cond
        ((< n 2) n)
        (else (+ (vir-fib (- n 1)) (vir-fib (- n 2))))
    )
)

(expect (vir-fib 2) 1)
(expect (vir-fib 10) 55)
(expect (vir-fib 1) 1)

Q2: List Making

Let’s make some Scheme lists. We’ll define the same list with list, quote, and cons. The following list was visualized using the draw feature of code.cs61a.org.
CS61A Fall 2021 discussion 10 scheme相关——q1-q6_第1张图片

First, use list:

(define with-list
    (list
        (list 'a 'b) 'c 'd (list 'e)
    )
)
(draw with-list)

Now use quote. What differences are there?

(define with-quote
    '(
        (a b) c d (e)
    )
)
(draw with-quote)

Now try with cons. For convenience, we’ve defined a helpful-list and another-helpful-list:

(define helpful-list
   (cons 'a (cons 'b nil)))
(draw helpful-list)

(define another-helpful-list
    (cons 'c (cons 'd (cons (cons 'e nil) nil))))
(draw another-helpful-list)

(define with-cons
    (cons
        helpful-list another-helpful-list
    )
)
(draw with-cons)

Q3: List Concatenation

Write a function which takes two lists and concatenates them.

Notice that simply calling (cons a b) would not work because it will create a deep list. Do not call the builtin procedure append, since it does the same thing as list-concat should do.

'See from stackoverflow
(define (list-concat a b)
    (if (null? a) 
        b
        (cons (car a) (list-concat (cdr a) b))
    )
)

'My realization
(define (list-concat-1 a b)
    (if (null? a) 
        (if (null? b)
            ()
            (list-concat b ())
        )
        (cons (car a) (list-concat (cdr a) b))
    )
)

(expect (list-concat '(1 2 3) '(2 3 4)) (1 2 3 2 3 4))
(expect (list-concat '(3) '(2 1 0)) (3 2 1 0))

上课听说了cdr意思是contents of the decorator register, car意思是contents of the address register,这两个名字来源于MIT(可能有误)使用的计算机的两个寄存器的名字,而现在计算机中已经没有这两个东西了,老师也对这种奇怪的命名方式表示遗憾,这告诉我们这种关键字最好别和可能改变的硬件名绑定。

注意只要(cdr list)是列表就不会造成嵌套,如果(car list)是列表那么得到的会是一个嵌套列表。

Q4: Map

Write a function that takes a procedure and applies it to every element in a given list.

(define (map-fn fn lst)
    (cond
        ((null? lst) nil)
        (else (cons (fn (car lst)) (map-fn fn (cdr lst))))
    )
)

(map-fn (lambda (x) (* x x)) '(1 2 3))
; expect (1 4 9)

Q5: Make Tree

Fill in the following to complete an tree data abstraction:

(define (make-tree label branches) (cons label branches))

(define (label tree)
    (car tree)
)

(define (branches tree)
    (cdr tree)
)

(make-tree 1 (list (make-tree 2 '()) (make-tree 3 '())))
; expect (1 (2) (3))

Q6: Tree Sum

Using the data abstraction above, write a function that sums up the entries of a tree, assuming that the entries are all numbers.

Hint: You may want to use map with a helper function to sum list entries.

(define (map-fn fn lst)
    (cond
        ((null? lst) nil)
        (else (cons (fn (car lst)) (map-fn fn (cdr lst))))
    )
)

(map-fn (lambda (x) (* x x)) '(1 2 3))
; expect (1 4 9)


(define (make-tree label branches) (cons label branches))

(define (label tree)
    (car tree)
)

(define (branches tree)
    (cdr tree)
)

(make-tree 1 (list (make-tree 2 '()) (make-tree 3 '())))
; expect (1 (2) (3))

(define (tree-sum-helper lst)
    (cond
        ((null? lst) 0)
        (else (+ (car lst) (tree-sum-helper (cdr lst))))
    )
)

(define (tree-sum tree)
    (cond
        ((null? tree) 0)
        (else (cond
                ((null? (branches tree)) (label tree))
                (else (+ (label tree) (tree-sum-helper (map-fn tree-sum (branches tree)))))
                )
        )
    )
)

(define (sum lst)
    (cond
        ((null? lst) 0)
        (else (+ (car lst) (sum (cdr lst))))
    )
)

(define t (make-tree 1 (list (make-tree 2 '()) (make-tree 3 '()))))
(expect (tree-sum t) 6)

做的时候内部的cond两个结果反了导致输出1,找了一阵才发现。

你可能感兴趣的:(补课,CS61a,开发语言,scheme)