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

这篇博客探讨了Scheme编程中的六个问题,包括Virahanka-Fibonacci数列计算、使用list、quote和cons创建列表、列表连接、map函数实现、构建树数据抽象以及求解树中所有数值之和的方法。通过这些问题,展示了Scheme语言的基本操作和递归思维的应用。
摘要由CSDN通过智能技术生成

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.
请添加图片描述

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
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值