sicp

SICP学习笔记(1.2.1 ~ 1.2.2)

 ̄綄美尐妖づ 提交于 2020-01-27 20:12:42
SICP学习笔记(1.2.1 ~ 1.2.2) 周银辉 1, 递归过程 和 递归计算过程 在学习SICP前我还没注意过有这样的一个区分,因为我们始终停留在语法表面上的“递归过程(recursive procedure)”,而没有去理解其实质是否是“递归计算过程(recursive process)”。 递归过程:从语法书写层面上而言的,在过程的定义中,其直接或间接地引用该过程本身。 比如 F{F}或者 F{G{F}} 递归计算过程:从实际运算层面上而言的,一个在语法上按照递归过程书写的函数其在运算时可能是按照递归的方式也可能是按照迭代的方式进行的。这取决于解释器或编译器本身是否有对“递归”进行优化,比如Scheme解释器是“严格尾递归”的, 而C#之类的,即便在语法形式上是"尾递归"的,但其仍然不能被编译成迭代计算过程,当然,你可以使用for,while等. 要检测出一个递归过程在计算时到底是按照递归方式还是按照迭代方式进行的,非常容易,只要将其进行深度递归或无限次递归,如果Stack Overflow了,那么是按照递归方式计算的,仅仅是一个死循环似的不停计算但没有栈溢出,那么是按照迭代方式计算的。一般而言函数式编程语言(Scheme,Haskell等), 会按照迭代方式进行计算;命令式编程语言(C++,C#,Java等)会按照递归方式进行计算。 2,迭代计算过程 和 递归计算过程

Lisp和SICP

一笑奈何 提交于 2020-01-18 12:48:09
大概不少programmer都看过《黑客与画家》,作者用了整整一章的篇幅讨论Lisp的强大。我自然就会手痒痒。 几个月前,几天内攻城略地搞定了Python,用的方法便是用Py重写之前开发的类库,这样就能很快熟悉语法,培养语感。喜上眉梢的我,也尝试将同样的策略用在Lisp上,我开始查看它的语法和函数,比如如何定义类和函数,如何赋值等等。 但我慢慢发现,Lisp几乎都不需要学语法,就是括号和几个基本过程,无非就是lambda, define, let,cons,car,cdr等等。虽然语法很快就学完了,但却很难用它写出以前Python或C#风格的命令式代码。连赋值都变得如此诡异,而且有数不清的方言,定义类时,每种方言都不一样!函数名都超长,变量甚至会用问号感叹号等平时根本不敢想的符号。 在尝试写Lisp一天之后,我无奈的放弃了,它的思路和我之前所有的观念都很不相同! 后来我看了《计算机程序的构造与解释》(SICP). 这本书名气显然没有《编程珠玑》甚至《编程之美》名气大。但它的目录很对我的胃口,流模式,惰性求值,元语言抽象,听着是不是就很有逼格呢?于是我迫不及待的踏上这片土地,一边看一边做习题。 之后发生的事情超出了我的想象,这仅仅是一本MIT的大一新生的入门课程,其思维深度,习题难度和广度都超过了我的想象。怪不得国外有那么多大神,因为人家看过了这样牛逼的教材!它的函数求导和积分的方法

max-lisp-eval-depth to find sqrt-iter

北慕城南 提交于 2020-01-16 11:12:10
问题 I am working on SICP's exercise 1.6 which rewrite the demonstration case #+begin_src emacs-lisp :session sicp :results output (defun sqrt(x) (sqrt-iter 1.0 x)) (defun sqrt-iter(guess x) (if (good-enough-p guess x) guess (sqrt-iter (improve guess x) x))) (defun good-enough-p(guess x) (< (abs (- (square guess) x)) 0.001)) (defun improve(guess x) (average guess (/ x guess))) (defun average(x y) (/ (+ x y) 2)) #+end_src It works and get the output #+begin_src emacs-lisp :session sicp :lexical t

Prolog: How can I implement the sum of squares of two largest numbers out of three?

半腔热情 提交于 2020-01-15 06:18:32
问题 Exercise 1.3 of the book Structure and Interpretation of Computer Programs asks the following: Define a procedure that takes three numbers as arguments and returns the sum of the squares of the two larger numbers. I'm learning Prolog. Here's the function I tried to implement: square(X, Y) :- Y is X * X. squareTwoLargest(X, Y, Z, R) :- R is square(L1) + square(L2), L1 = max(X, Y), L2 = max(min(X, Y), Z). However, when I run it, it gives the following error: ERROR: is/2: Arguments are not

Prolog: How can I implement the sum of squares of two largest numbers out of three?

痞子三分冷 提交于 2020-01-15 06:17:08
问题 Exercise 1.3 of the book Structure and Interpretation of Computer Programs asks the following: Define a procedure that takes three numbers as arguments and returns the sum of the squares of the two larger numbers. I'm learning Prolog. Here's the function I tried to implement: square(X, Y) :- Y is X * X. squareTwoLargest(X, Y, Z, R) :- R is square(L1) + square(L2), L1 = max(X, Y), L2 = max(min(X, Y), Z). However, when I run it, it gives the following error: ERROR: is/2: Arguments are not

What is fixed point?

孤街浪徒 提交于 2020-01-04 04:12:24
问题 I'm rewatching some of the earlier lectures on SICP. The notion of a fixed-point is a bit confusing to me. The fixed-point procedure: should I be thinking about it this way, "it's the way to find a fixed-point of a given function." So given f(2) = 2 ? Also why is it stated in this lecture, that a new function y that maps to x / y is a fixed point? 回答1: According to Fixed point (mathematics) on Wikipedia: In mathematics, a fixed point (sometimes shortened to fixpoint, also known as an

Implement SICP evaluator using Racket

妖精的绣舞 提交于 2020-01-03 03:25:23
问题 I'm working on metacircular evaluator of 4.1.4 Running the Evaluator as a Program , building which with Racket: #lang racket (require (combine-in rnrs/base-6 rnrs/mutable-pairs-6)) (define (evaluate exp) (cond ; ... ((definition? exp) (display exp) (display " is a definition\n")) ; ... (else (display exp) (display " is something else\n")))) (define (definition? exp) (tagged-list? exp 'define)) (define (tagged-list? exp tag) (if (pair? exp) (eq? (car exp) tag) false)) (define (driver-loop)

How do I include files in DrScheme?

雨燕双飞 提交于 2020-01-02 08:53:11
问题 I'm using DrScheme to work through SICP, and I've noticed that certain procedures (for example, square ) get used over and over. I'd like to put these in a separate file so that I can include them in other programs without having to rewrite them every time, but I can't seem to figure out how to do this. I've tried: (load filename) (load (filename)) (load ~/path-to-directory/filename) (require filename) (require ~/path-to-directory/filename) (require path-from-root/filename) None of these

Using trace to display a procedure in racket

北城以北 提交于 2020-01-02 04:46:06
问题 I've been working through the last few exercises ch 1 of SICP, where several of the exercises uses higher-order functions. Currently I'm trying to debug a problem in my solution to 1.45, which is raising an arity mismatch. The function which is raising the error is the result of twice applying an averaging operation to a fixed-point function solver. It would make my debugging efforts a lot easier if I could just dump some sort of representation of procedures, given that the procedure has been

Is there any difference between closure in Scheme and usual closure in other languages?

僤鯓⒐⒋嵵緔 提交于 2020-01-02 03:53:06
问题 I'm studying SICP right now. And I found the definition of closure in SICP is (maybe) different from closure definition in other languages. Here's what SICP says: The ability to create pairs whose elements are pairs is the essence of list structure's importance as a representational tool. We refer to this ability as the closure property of cons. In general, an operation for combining data objects satisfies the closure property if the results of combining things with that operation can