Permalink
Switch branches/tags
Nothing to show
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
70 lines (58 sloc) 1.49 KB

练习15. List列表和map散列

内容有点多,慢慢打字吧,列表和散列分别是两种不同的数据结构,Map散列 是没有顺序的,插入数据的顺序不会被保存。

// List and Map

l := List clone

l := list(0, 1, 2, 3, 4, 5)

l println

l at(1) println

a := l append(6) println // 把6添加到列表的末尾
l println
a := l remove(6) println // 把6从列表中移除
"\n" print
b := l select(>0.5) println
b := l map(>0.5) println
b := l select(isOdd) println
b := l map(isOdd) println
b := l select(i, x, x isOdd) println
b := l map(i, x, x isOdd) println
b := l map(*2) println
l println
"\n" print
c := l atPut(0, -1) println
c := l atPut(0, 0) println
c := l atInsert(6, 6) println
c := l atInsert(7, "hello wolrd") println
c := l remove("hello wolrd") println
"\n" print	
d := l foreach(print)
"\n" print
d := l foreach(v, (v .. " ") print)
"\n" print
d := l foreach(i, v, write(i, ":", v, ","))

运行之后,应该可以看到

> io ex15.io
list(0, 1, 2, 3, 4, 5)
1
list(0, 1, 2, 3, 4, 5, 6)
list(0, 1, 2, 3, 4, 5, 6)
list(0, 1, 2, 3, 4, 5)

list(1, 2, 3, 4, 5)
list(false, true, true, true, true, true)
list(1, 3, 5)
list(false, true, false, true, false, true)
list(1, 3, 5)
list(false, true, false, true, false, true)
list(0, 2, 4, 6, 8, 10)
list(0, 1, 2, 3, 4, 5)

list(-1, 1, 2, 3, 4, 5)
list(0, 1, 2, 3, 4, 5)
list(0, 1, 2, 3, 4, 5, 6)
list(0, 1, 2, 3, 4, 5, 6, hello wolrd)
list(0, 1, 2, 3, 4, 5, 6)

0123456
0 1 2 3 4 5 6 
0:0,1:1,2:2,3:3,4:4,5:5,6:6,