1
0
Fork 0
This commit is contained in:
Darcy Shen 2024-02-26 14:39:00 +08:00
parent 887ebf902f
commit e9f886c890
1 changed files with 342 additions and 5 deletions

View File

@ -1,13 +1,13 @@
<TeXmacs|2.1.2> <TeXmacs|2.1.2>
<style|<tuple|beamer|no-page-numbers|chinese>> <style|<tuple|beamer|no-page-numbers|chinese|python>>
<\body> <\body>
<\hide-preamble> <\hide-preamble>
<assign|dfn|<macro|x|<strong|<arg|x>>>> <assign|dfn|<macro|x|<strong|<arg|x>>>>
</hide-preamble> </hide-preamble>
<screens|<\shown> <screens|<\hidden>
<tit|\<#96F6\>\<#57FA\>\<#7840\>SICP\<#FF1A\>\<#7B2C\>3\<#8BFE\>> <tit|\<#96F6\>\<#57FA\>\<#7840\>SICP\<#FF1A\>\<#7B2C\>3\<#8BFE\>>
<\wide-tabular> <\wide-tabular>
@ -57,7 +57,7 @@
Tree Recursion Tree Recursion
</cell>>>> </cell>>>>
</wide-tabular> </wide-tabular>
</shown>|<\hidden> </hidden>|<\hidden>
<tit|\<#6761\>\<#4EF6\>\<#8868\>\<#8FBE\>\<#5F0F\>\<#548C\>\<#8C13\>\<#8BCD\>\V\<#77ED\>\<#8DEF\>\<#8FD0\>\<#7B97\>> <tit|\<#6761\>\<#4EF6\>\<#8868\>\<#8FBE\>\<#5F0F\>\<#548C\>\<#8C13\>\<#8BCD\>\V\<#77ED\>\<#8DEF\>\<#8FD0\>\<#7B97\>>
<\itemize> <\itemize>
@ -137,7 +137,329 @@
</folded> </folded>
\; \;
</hidden>> </hidden>|<\hidden>
<tit|\<#5FAA\>\<#73AF\>\<#FF1A\>\<#6C42\>\<#548C\>>
<paragraph|\<#6848\>\<#4F8B\>\<#5206\>\<#6790\>\<#FF1A\>\<#6C42\>\<#548C\>>
<\with|par-columns|2>
<\equation*>
<big|sum><rsub|i=1><rsup|n>i=1+2+\<cdots\>+n
</equation*>
\;
<\render-code>
result := 0
<algo-for|i \<leftarrow\> 1 <render-to> n|result := result + i>
</render-code>
</with>
<hrule>
<\with|par-columns|2>
<\session|scheme|default>
<\folded-io|Scheme] >
(define result 0)
<|folded-io>
0
</folded-io>
<\input|Scheme] >
(for (x (list 1 2 3 4))
\ \ (set! result (+ result x)))
</input>
<\folded-io|Scheme] >
result
<|folded-io>
10
</folded-io>
<\input|Scheme] >
\;
</input>
</session>
<\session|python|default>
<\input>
\<gtr\>\<gtr\>\<gtr\>\
<|input>
def sum_n(n):
\ \ \ \ result= 0
\ \ \ \ for i in range(n):
\ \ \ \ \ \ \ \ result= result + (i + 1)
\ \ \ \ return result
</input>
<\folded-io>
\<gtr\>\<gtr\>\<gtr\>\
<|folded-io>
sum_n(3)
<|folded-io>
6
</folded-io>
<\folded-io>
\<gtr\>\<gtr\>\<gtr\>\
<|folded-io>
list(range(3))
<|folded-io>
[0, 1, 2]
</folded-io>
<\input>
\<gtr\>\<gtr\>\<gtr\>\
<|input>
\;
</input>
</session>
</with>
<\exercise>
\<#4F7F\>\<#7528\>Scheme\<#5B9E\>\<#73B0\>range\<#3002\>
</exercise>
</hidden>|<\hidden>
<tit|\<#7EBF\>\<#6027\>\<#9012\>\<#5F52\>\<#FF1A\>\<#6C42\>\<#548C\>>
<\session|scheme|default>
<\unfolded-io|Scheme] >
(define (sum start end)
\ \ (cond ((\<gtr\> start end) 0)
\ \ \ \ \ \ \ \ ((= start end) end)
\ \ \ \ \ \ \ \ (else (+ start (sum (+ start 1) end)))))
\;
<|unfolded-io>
sum
</unfolded-io>
<\unfolded-io|Scheme] >
(sum 2 3)
<|unfolded-io>
5
</unfolded-io>
<\input|Scheme] >
\;
</input>
</session>
<\with|par-columns|2|par-left|1fn|par-right|4fn|par-columns-sep|0fn>
<\eqnarray*>
<tformat|<table|<row|<cell|<big|sum><rsup|100><rsub|i=1>i>|<cell|=>|<cell|1+<big|sum><rsup|100><rsub|i=2>i>>|<row|<cell|>|<cell|=>|<cell|1+<around*|(|2+<big|sum><rsub|i=3><rsup|100>i|)>>>|<row|<cell|>|<cell|=>|<cell|1+<around*|(|2+<around*|(|3+<big|sum><rsub|i=4><rsup|100>i|)>|)>>>|<row|<cell|>|<cell|=>|<cell|\<ldots\>>>|<row|<cell|>|<cell|=>|<cell|<with|math-display|false|1+<around*|(|2+\<cdots\>+<around*|(|99+<big|sum><rsub|i=100><rsup|100>i|)>|)>>>>|<row|<cell|>|<cell|=>|<cell|1+<around*|(|2+<around*|(|3+\<cdots\>+<around*|(|99+100|)>|)>|)>>>|<row|<cell|>|<cell|=>|<cell|1+<around*|(|2+<around*|(|3+\<cdots\>+<around*|(|98+199|)>|)>|)>>>|<row|<cell|>|<cell|=>|<cell|\<ldots\>>>|<row|<cell|>|<cell|=>|<cell|1+5049>>|<row|<cell|>|<cell|=>|<cell|5050>>>>
</eqnarray*>
</with>
\;
</hidden>|<\hidden>
<tit|\<#6811\>\<#5F62\>\<#9012\>\<#5F52\>\<#FF1A\>\<#6590\>\<#6CE2\>\<#90A3\>\<#5951\>\<#6570\>\<#5217\>>
<\folded>
<\equation*>
<with|mode|text|Fib><around*|(|n|)>=<around*|{|<tabular|<tformat|<cwith|1|-1|1|1|cell-halign|l>|<cwith|1|-1|2|2|cell-halign|l>|<table|<row|<cell|0>|<cell|<with|mode|text|if>
n=0,>>|<row|<cell|1>|<cell|<with|mode|text|if>
n=1,>>|<row|<cell|<with|mode|text|Fib><around*|(|n-1|)>+<with|mode|text|Fib><around*|(|n-2|)>>|<cell|<with|mode|text|otherwise>.>>>>>|\<nobracket\>>
</equation*>
<|folded>
<scm|<\with|par-mode|center>
<tree|fib 5|<tree|fib 4|<tree|fib 3|<tree|fib 2|<tree|fib
1|1>|<tree|fib 0|0>>>|<tree|fib 2|<tree|fib 1|1>|<tree|fib
0|0>>>|<tree|fib 3|<tree|fib 2|<tree|fib 1|1>|<tree|fib 0|0>>>>
</with>>
</folded>
<\session|scheme|default>
<\folded-io|Scheme] >
(define (fib n)
\ \ (cond ((= n 0) 0)
\ \ \ \ \ \ \ \ ((= n 1) 1)
\ \ \ \ \ \ \ \ (else (+ (fib (- n 1)) (fib (- n 2))))))
<|folded-io>
fib
</folded-io>
<\folded-io|Scheme] >
(fib 2)
<|folded-io>
1
</folded-io>
<\input|Scheme] >
\;
</input>
</session>
</hidden>|<\hidden>
<tit|\<#6811\>\<#5F62\>\<#9012\>\<#5F52\>\<rightarrow\>\<#8FED\>\<#4EE3\>>
<\equation*>
<with|mode|text|Fib><around*|(|n|)>=<around*|{|<tabular|<tformat|<cwith|1|-1|1|1|cell-halign|l>|<cwith|1|-1|2|2|cell-halign|l>|<table|<row|<cell|0>|<cell|<with|mode|text|if>
n=0,>>|<row|<cell|1>|<cell|<with|mode|text|if>
n=1,>>|<row|<cell|<with|mode|text|Fib><around*|(|n-1|)>+<with|mode|text|Fib><around*|(|n-2|)>>|<cell|<with|mode|text|otherwise>.>>>>>|\<nobracket\>>
</equation*>
<\equation*>
<tabular|<tformat|<table|<row|<cell|0>|<cell|1>|<cell|2>|<cell|3>|<cell|4>|<cell|5>|<cell|\<ldots\>>|<cell|n-2>|<cell|n-1>|<cell|n>>|<row|<cell|0>|<cell|1>|<cell|2>|<cell|3>|<cell|5>|<cell|8>|<cell|\<ldots\>>|<cell|f<around*|(|n-2|)>>|<cell|f<around*|(|n-1|)>>|<cell|f<around*|(|n-1|)>+f<around*|(|n-2|)>>>|<row|<cell|a<rsub|0>=0>|<cell|b<rsub|0>=1>|<cell|>|<cell|>|<cell|>|<cell|>|<cell|>|<cell|>|<cell|>|<cell|>>|<row|<cell|>|<cell|a<rsub|1>=1>|<cell|b<rsub|1>=2>|<cell|>|<cell|>|<cell|>|<cell|>|<cell|>|<cell|>|<cell|>>|<row|<cell|>|<cell|>|<cell|a<rsub|2>=2>|<cell|b<rsub|2>=3>|<cell|>|<cell|>|<cell|>|<cell|>|<cell|>|<cell|>>|<row|<cell|>|<cell|>|<cell|>|<cell|a<rsub|3>=b<rsub|2>>|<cell|b<rsub|3>=a<rsub|2>+b<rsub|2>>|<cell|>|<cell|>|<cell|>|<cell|>|<cell|>>>>>
</equation*>
<\with|par-columns|2>
<\algorithm>
<\math>
a<rsub|0>\<leftarrow\> 0
b <rsub|0>\<leftarrow\> 1
<\algo-for|i \<leftarrow\> 1 <render-to> n>
a<rsub|i> \<leftarrow\> b<rsub|i-1>
b <rsub|i>\<leftarrow\> a<rsub|i-1> + b<rsub|i-1>
</algo-for>
<render-result>b<rsub|n>
</math>
</algorithm>
<\algorithm>
a \<leftarrow\> 0
b <math|\<leftarrow\>> 1
<\algo-for|i \<leftarrow\> 1 <render-to> n>
old_b \<leftarrow\> b
b \<leftarrow\> a + b
a \<leftarrow\> old_b
</algo-for>
<render-result> b
</algorithm>
</with>
\;
\;
</hidden>|<\hidden>
<tit|\<#5206\>\<#6CBB\>\<#6CD5\>\<#FF1A\>\<#627E\>\<#96F6\>\<#95EE\>\<#9898\>>
<\itemize>
<item>\<#65E0\>\<#9650\>\<#591A\>\<#4E00\>\<#5143\>\<#7EB8\>\<#5E01\>
<item>\<#65E0\>\<#9650\>\<#591A\>\<#4E94\>\<#5143\>\<#7EB8\>\<#5E01\>
<item>\<#65E0\>\<#9650\>\<#591A\>\<#5341\>\<#5143\>\<#7EB8\>\<#5E01\>
<item>\<#65E0\>\<#9650\>\<#591A\>\<#4E94\>\<#5341\>\<#5143\>\<#7EB8\>\<#5E01\>
</itemize>
<\folded-documentation>
<\question>
\<yen\>6\<#6709\>\<#51E0\>\<#79CD\>\<#627E\>\<#96F6\>\<#65B9\>\<#5F0F\>\<#FF1F\>
</question>
<|folded-documentation>
\<yen\>6=6\<times\>\<yen\>1, \<yen\>6=1\<times\>\<yen\>5+1
</folded-documentation>
<\folded-documentation>
<\question>
\<yen\>16\<#6709\>\<#51E0\>\<#79CD\>\<#627E\>\<#96F6\>\<#65B9\>\<#5F0F\>\<#FF1F\>
</question>
<|folded-documentation>
\;
</folded-documentation>
<\folded-documentation>
<\question>
\<#5BF9\>\<#4E8E\>\<yen\>n\<#FF0C\>\<#4E00\>\<#5171\>\<#6709\>\<#591A\>\<#5C11\>\<#79CD\>\<#627E\>\<#96F6\>\<#65B9\>\<#5F0F\>\<#FF1F\>
</question>
<|folded-documentation>
<\equation*>
f<around*|(|n,<around*|[|1,5,10,50|]>|)>=f<around*|(|n,<around*|(|1|)>|)>+f<around*|(|n,<around*|(|5|)>|)>+f<around*|(|n,<around*|(|10|)>|)>+f<around*|(|n,<around*|(|50|)>|)>+f<around*|(|n,<around*|(|1,5|)>|)>+f<around*|(|n,<around*|(|1,10|)>|)>+f<around*|(|n,<around*|(|1,50|)>|)>+f<around*|(|n,<around*|(|5,10|)>|)>+f<around*|(|n,<around*|(|5,50|)>|)>+f<around*|(|n,<around*|(|10,50|)>|)>+f<around*|(|n,<around*|(|1,5,10|)>|)>+f<around*|(|n,<around*|(|1,5,50|)>|)>+f<around*|(|n,<around*|(|5,10,50|)>|)>+f<around*|(|n,<around*|(|1,5,10,50|)>|)>
</equation*>
<\equation*>
f<around*|(|n,<around*|[|1,5,10,50|]>|)>=f<around*|(|n,<around*|[|5,10,50|]>|)>+f<around*|(|n-1,<around*|[|1,5,10,50|]>|)>
</equation*>
</folded-documentation>
</hidden>|<\shown>
<tit|\<#5206\>\<#6CBB\>\<#6CD5\>\<#FF1A\>\<#627E\>\<#96F6\>\<#95EE\>\<#9898\>>
<\equation*>
f<around*|(|n,<around*|[|1,5,10,50|]>|)>=<choice|<tformat|<table|<row|<cell|f<around*|(|n,<around*|[|5,10,50|]>|)>+f<around*|(|n-1,<around*|[|1,5,10,50|]>|)>>|<cell|,n\<geqslant\>2>>|<row|<cell|1>|<cell|,n\<longequal\>1>>|<row|<cell|1>|<cell|,n=0>>>>>
</equation*>
<\session|scheme|default>
<\unfolded-io|Scheme] >
(define (\<#53D6\>\<#53EF\>\<#7528\>\<#6700\>\<#5C0F\>\<#9762\>\<#503C\>
n)
\ \ (cond ((= n 4) 1)
\ \ \ \ \ \ \ \ ((= n 3) 5)
\ \ \ \ \ \ \ \ ((= n 2) 10)
\ \ \ \ \ \ \ \ ((= n 1) 50)
\ \ \ \ \ \ \ \ (else 0)))
<|unfolded-io>
\<#53D6\>\<#53EF\>\<#7528\>\<#6700\>\<#5C0F\>\<#9762\>\<#503C\>
</unfolded-io>
<\unfolded-io|Scheme] >
(define (\<#627E\>\<#96F6\>\<#65B9\>\<#6CD5\>\<#6570\> n k)
\ \ (if (or (\<less\> n 2) (\<less\>= k 1))
\ \ \ \ \ \ (if (and (\<gtr\>= n (\<#53D6\>\<#53EF\>\<#7528\>\<#6700\>\<#5C0F\>\<#9762\>\<#503C\>
k)) (= (% n (\<#53D6\>\<#53EF\>\<#7528\>\<#6700\>\<#5C0F\>\<#9762\>\<#503C\>
k)) 0))
\ \ \ \ \ \ \ \ \ \ 1
\ \ \ \ \ \ \ \ \ \ 0)
\ \ \ \ \ \ (+ (\<#627E\>\<#96F6\>\<#65B9\>\<#6CD5\>\<#6570\> n (- k
1)) (\<#627E\>\<#96F6\>\<#65B9\>\<#6CD5\>\<#6570\> (- n
(\<#53D6\>\<#53EF\>\<#7528\>\<#6700\>\<#5C0F\>\<#9762\>\<#503C\> k))
k))))
<|unfolded-io>
\<#627E\>\<#96F6\>\<#65B9\>\<#6CD5\>\<#6570\>
</unfolded-io>
<\unfolded-io|Scheme] >
(\<#627E\>\<#96F6\>\<#65B9\>\<#6CD5\>\<#6570\> 50 1)
<|unfolded-io>
<errput|unbound variable % in (% n ((symbol
"å\\x8f;\\x96;å\\x8f;¯ç\\x94;¨æ\\x9c;\\x80;å°\\x8f;é\\x9d;¢å\\x80;¼")
k))>
</unfolded-io>
<\unfolded-io|Scheme] >
(/ 10 5)
<|unfolded-io>
2
</unfolded-io>
<\input|Scheme] >
\;
</input>
</session>
\;
</shown>>
</body> </body>
<\initial> <\initial>
@ -154,5 +476,20 @@
<\references> <\references>
<\collection> <\collection>
<associate|auto-1|<tuple|1|?>> <associate|auto-1|<tuple|1|?>>
<associate|auto-2|<tuple|1|?>>
</collection> </collection>
</references> </references>
<\auxiliary>
<\collection>
<\associate|toc>
<with|par-left|<quote|4tab>|\<#6848\>\<#4F8B\>\<#5206\>\<#6790\>\<#FF1A\>\<#5B9E\>\<#73B0\>\<#6C42\>\<#7EDD\>\<#5BF9\>\<#503C\>\<#7684\>\<#591A\>\<#79CD\>\<#65B9\>\<#5F0F\>
<datoms|<macro|x|<repeat|<arg|x>|<with|font-series|medium|<with|font-size|1|<space|0.2fn>.<space|0.2fn>>>>>|<htab|5mm>>
<no-break><pageref|auto-1>>
<with|par-left|<quote|3tab>|\<#6848\>\<#4F8B\>\<#5206\>\<#6790\>\<#FF1A\>\<#6C42\>\<#548C\>
<datoms|<macro|x|<repeat|<arg|x>|<with|font-series|medium|<with|font-size|1|<space|0.2fn>.<space|0.2fn>>>>>|<htab|5mm>>
<no-break><pageref|auto-2>>
</associate>
</collection>
</auxiliary>