CS107 Programming Paradigms (Stanford University) by Jerry Cain (22-27)学习笔记(二)

2014-11-24 07:49:52 · 作者: · 浏览: 2
unary-map cdr other-list ) ) ) ) ) ) )


map 底层是依靠cons来帮助它分配空间的. cons调用时Scheme会自动生成一个cons区域.

当打印输出完成后, 之前cons分配得到的cons内存区域就会被回收.

例子:

>( define x '(1 2 3) ) // 变量x与链表( 1 2 3 )关联起来了.

>( define y (cdr x) )

>( define x '(4 5) ) // 这样一来结点结点 1 就无变量指向了. 某些系统会给每个结点设一个计数器, 当计数器为0时, 由垃圾回收器自动回收.

Scheme的垃圾回收机制:

所有变量(如: x, y)放在Symbal map集中, 系统自动生成的链表结点放在mast cons set集中, 每个单元结点都可以被单独释放. 然后根据define的定义将变量与链表结点关联起来.

系统会根据算法, 在需要的时候对Symbal map中的所有变量作一次搜索. 对mast cons set中与之有关联的结点作上标记. 然后令mast cons set收回其集中所有未做标记的结点.

//-----------------------------------------------------------------

ML 是Scheme的增强版

Harkll

Erlang 优势: 并行处理




Lecture24
C语言是由写unix的人发明的, 目的是使写操作系统更容易.
C++在70年代末~80年代初开始出现.
Python从2000年开始流行.
循环的一轮被称为一次迭代, 所谓用迭代来代替递归, 就是用循环来代替递归. 反过来, 递归也可以用来替换迭代. 如scheme语言所示.
"hello there".statswith("...")
"hello there".capitalize()
"Hello There".istitle()
"[ ]" 表列表且此列表可变.
//--------------------------------
smallnum = [ 1, 2, 3, 4, 5, 6]
len( smallnum )
smallnum[ -2 ]
smallnum[ len( smallnum ) - 1 ]
smallnum[ 1 : 3 ]
smallnum[ 0 : 3 ]
smallnum[ 0 : 0 ]
smallnum[ -2 : ]
smallnum[ 1 : -1 ]
smallnum[ 1 : 0 ]
smallnum[ 1 : ]
"hello there"[ 4 : 7 ]
//--------------------------------
seq = [ 0, 1, 2, 3, 5, 6, 7, 8]
seq[ 4 : 4 ] = [ 4 ]
seq
//--------------------------------
seq = [ 5, 9, 2, 0 ]
seq
seq[ 0 ] = 14
seq
//--------------------------------
seq = ( 1, 2, 3, 4 )
seq[ 1 ] = 14
//--------------------------------
seq = [ "a", "b", "c"]
seq.append( "d" )
seq
//----------------------------------------------
在名为 divisors.py 的文件中作如下定义:
def getherDivisors( number ) :
divisors = []
for div in range( 1, number+1 ) :
if number % div == 0:
divisors.append( div )
return divisors
每一行的缩进靠制表符设定.
三重引号对表注释字符串将出现在多行中.
在其它模块中引用函数getherDivisors的方式有两种.
方法1:
>>> import divisors
>>> divisors.getherDivisors( 24 )
方法2:
>>> from divisors import getherDivisors
>>> getherDivisors( 24 )
//--------------------------------------------------
字典
>>> student = {} // 大括号对描述一个字典常量的所有内容.
>>> student
{}
>>> student["name"] = "Linda"
...
>>> student["gpa"] = 3.56
>>> student
...
>>> student["gpa"]
...
>>> student["gpa"] = 4.5
...
Python中的所有对象( 如: 列表, 字典等 )都是通过引用传递的.
>>> student["friends"] = ["aa", "bb", "cc"]
>>> student
...
>>> student["ideas"] = {}
>>> student
...
>>> playground = { }
>>> playground[ 4 ] = "think"
>>> playground
...



Lecture25

字典是Python的基础

grammar = { ' ' : [ [ 'This', ' ', 'is here.' ] ],

'' : [ [ 'computer' ],

[ 'car' ],

[ 'assignment' ] ] };

在Scheme中可以用序列化的对象结构来表达数据.

本例中:

grammar在内存中的字典有两个key分别是 start 和 object. 它们各自分别映射到一个list的列表. start对应的列表长度为1, object对应的列表长度为3.


import sys

from random import choice, seed

def expand( symbol )

if symbol.startswith( '<' ): // '<' 表尖括号

definitions = grammar[ symbol ]

expansion = choice( definitions ) // 实际上以一个整数或一个列表作为参数, choice是内置函数, 用以获得随机数. 若输入n, 则choice返回一个介于0与n之间的随机数 // 若输入一个列表, 则choice等概率地随机选取一个此列表的元素, 并返回此元素.

map( expand, expansion ) // 用递归而非迭代的方法来得到所有值.

else

sys.out.write(symbol)

运行:

>>> seed()

>>> expand( ' ' )


若将grammar作为参数则上例可改为

def expand( symbol, grammar )

if symbol.startswith( '<' ):

definitions = grammar[ symbol ]

expansion = choice( definitions )

map( expand, expansion ) // map 只能映射带一个参数的一元函数.

// lambdas 实际上在Python中也存在, 此句也可表达为:

map( lambdas, item: expand ( item, grammar ), expansion )

else

sys.out.write(symbol)

运行:

>>> seed()

>>> expand( ' ', grammar )

//---------------------------------------------------------------

Python中的各