SICP-Notes-Lecture 16 Scheme - Elementary

本文介绍了Scheme语言的基本概念,包括原子表达式、组合表达式、特殊形式表达式等,并通过实例讲解了如何使用Scheme进行递归计算。

Lecture 16 Scheme - Elementary

These are my notes for SICP(Structure and Interpretation of Computer Programs). Hope they’ll be of some help to you.

Scheme is a Dialect of Lisp

Scheme Expressions

Scheme programs consist entirely of two types of expressions.

  • Atomic expressions (Atoms: primitive values that cannot be broken up into smaller parts)

  • Self-evaluating: numbers, booleans

    3, 5.5, -10, #t, #f

  • Symbols: names bound to values

    +, modulo, list, x, foo, hello-world

  • Combinations

    (<operator> <operand1><operand2>...)

    • A combination is either a call expression or a special form expression.
Call expressions

Syntax:(<operator> <operand1><operand2>...)

A call expression applies a procedure to some arguments.

How to evaluate call expressions:

  1. Evaluate the operator to get a procedure.
  2. Evaluate all operands left to right to get the arguments.
  3. Apply the procedure to the arguments.

Call expressions include an operator and 0 or more operands in the parentheses.

>(quotient 10 2)
5
>(quotient (+ 8 7) 5)
3
>( + (* 3
        (+ (* 2 4)
           (+ 3 5))
     (+ (- 10 7)
        6)))

“quotient” names Scheme’s built-in integer division procedure(i.e function)

Combinations can span multiple lines (spacing doesn’t matter)

Special form expressions
Assigning values to names

The define special form assigns a value to a name: (define <name><expr>)

How to evaluate:

  1. Evaluate the given expression
  2. Bind the resulting value to the given name in the current frame.
  3. Return the names as a symbol.
scm> (define x (+ 3 4))
x
scm> x
7
scm> (define x (+ x 5))
x
scm> x
12
Control flow

The if special form allows us to evaluate an expression based on a condition: (if <predicate> <if-true><if-false>)

How to evaluate:

  1. Evaluate the <predicate>.
  2. If <predicate> evaluates to anything but #f, evaluate <if-true> and return the value. Otherwise, evaluate <if-false> if provided and return the value.

#f is the only Falsy value in Scheme.

scm> (if #t 3 5)
3
scm> (if 0 (+ 1 0)(/ 1 0))
1
scm> (if (> 10 1)(* 5 6))
30
scm> (if (not 4) 1 (if #f 5 6))
6
Define functions with names

The second version of define is a shorthand for creating a function with a name: (define (<name> <param1><param2>...)<body>)

How to evaluate:

  1. Create a lambda procedure with the given parameters and body.
  2. Bind it to the given name in the current frame.
  3. Return the function name as a symbol.
scm> (define (square x) (* x x))
square
scm> square
(lambda (x) (*x x))
scm> (square 4)
16
scm> (square 10)
100
Lambda Expressions

The lambda special form returns a lambda procedure. (lambda (<param1><param2>...)<body>)

How to evaluate:

  1. Create a lambda procedure with the given parameters and body.

    The body expression is evaluated when the lambda procedure is applied.

  2. Return the lambda procedure.

scm> (lambda (x) (* x x))
(lambda(x) (* x x) 5)
scm> (lambda(x) (* x x) 5)
25
scm> (define square (lambda (x) (* x x)))
square
scm> (square 4)
16

Two equivalent expressions:

(define (plus4 x) (+ x 4))

(define plus4 (lambda (x) (+ x 4)))

An operator can be a call expression too:

((lambda (x y z) (+ x y (square z))) 1 2 3)

Evaluates to the x + y + z^2 procedure.

Examples

Factorial

Recall the factorial function, which takes in an integer n and computes the product of all the integers from 1 to n.

Let’s try to write it in Scheme!

Scheme has no special form that allows for iteration, so we have to use recursion.

Ideas:

  1. Base case: if n is 0 or 1, just return 1
  2. Recursive case: Return the factorial of the previous number multiplied by n
  3. Use the if special form to capture our two cases:if <pred><if-true><if-false>)
(define (fact x)
    (if (<= x 1) 
        1
        (* x (fact (- x 1)))))

Combinations can be split across multiple lines.

No explicit return statement!

Counting up

Let’s write a function count-up that takes in an integer n and prints all the integers from 1 to n.

Ideas:

  1. We need to keep track of the current element, k. k starts at 1.
  2. Since we have to use recursion, we can write a helper function to keep track of k.
  3. Print k at the beginning of every call and only make a recursive call to print more numbers if k is less than n.
(define (count-up n)
  (define (counter k)
    (print k)
    (if (< k n)
        (counter (+ k 1))))
  (counter 1))

If there is more than one expression in the body, the function returns the value of the last expression.

Summary

  • Scheme programs consist only of expressions, all of which can be categorized into either atomic expressions or combinations.
  • Combinations are either call expressions or special form expressions, and they differ in the value of the operator.
  • Scheme call expressions are evaluated just like they are in Python, but each special form has its own rules of evaluation.
  • The special forms we learned today are if, define, and lambda.
  • Writing some procedures in Scheme will require recursion; there is no special form for iteration.
源码链接: https://pan.quark.cn/s/7b9e1590db2e 在本计划中,我们聚焦于一个基于数字逻辑的药片装瓶系统的构建,这构成了北京邮电大学(北邮)在小学期内向学生提供的一次课程设计课题。该系统致力于模拟实际药品包装的操作流程,借助电子操控和自动化技术达成药片的高效且精准的装瓶目标。以下是对该系统设计所涉及的关键知识领域的详尽阐述: 1. **数字逻辑**:数字逻辑是电子工程领域的核心学科,主要探究如何运用二进制数字进行信息的表征与处理。在此项目中,数字逻辑用于构建和实现系统的控制机制,诸如计数器、编码器、解码器、触发器等,旨在保障药片装瓶过程的精确调控。 2. **硬件电路构建**:系统可能整合微控制器、传感器、执行机构等硬件单元。例如,微控制器作为系统的心脏,负责接收输入信号,处理数据,并指挥执行机构执行药片装填。传感器负责监测药片的数量和瓶装进度,而执行机构如电机则负责实际完成装瓶动作。 3. **计数器**:在药片装瓶的操作过程中,计数器用于追踪已装入瓶子的药片总数,确保达到预设的剂量标准。这可能需要设计同步计数器或异步计数器,以实现精确计数并触发装瓶操作。 4. **编码与解码**:编码器将特定的信息(例如药片种类或剂量)转化为二进制编码,便于硬件设备进行处理;解码器则将这些编码解读为可执行的操作,如切换装瓶路径或启动封盖流程。 5. **触发器**:在系统中,触发器可用于在特定条件达成时启动或中止某个操作,例如当瓶子达到满载时关闭装填机制。 6. **传感器技术**:可能包含重量传感器、光电传感器或机械触碰开关,用于识别瓶子的存在、位置以及药片的数量。这些传感器的精确度直接关联到整个系统的性能水平。 7. **控制算法**...
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值