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 优势: 并行处理
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 = { '