forked from JuliaLang/julia
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlist.jl
92 lines (78 loc) · 1.61 KB
/
list.jl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
import Base.length, Base.map, Base.show
abstract List{T}
type Nil{T} <: List{T}
end
type Cons{T} <: List{T}
head::T
tail::List{T}
end
cons{T}(h, t::List{T}) = Cons{T}(h, t)
nil(T) = Nil{T}()
nil() = nil(Any)
head(x::Cons) = x.head
tail(x::Cons) = x.tail
function show{T}(io::IO, l::List{T})
if isa(l,Nil)
if is(T,Any)
print(io, "nil()")
else
print(io, "nil(", T, ")")
end
else
print(io, "list(")
while true
show(io, head(l))
l = tail(l)
if isa(l,Cons)
print(io, ", ")
else
break
end
end
print(io, ")")
end
end
list() = nil()
function list(elts...)
l = nil()
for i=length(elts):-1:1
l = cons(elts[i],l)
end
return l
end
function list{T}(elts::T...)
l = nil(T)
for i=length(elts):-1:1
l = cons(elts[i],l)
end
return l
end
function list{T}(elts::List{T}...)
l = nil(List{T})
for i=length(elts):-1:1
l = cons(elts[i],l)
end
return l
end
length(l::Nil) = 0
length(l::Cons) = 1 + length(tail(l))
map(f::Base.Callable, l::Nil) = l
map(f::Base.Callable, l::Cons) = cons(f(head(l)), map(f, tail(l)))
copylist(l::Nil) = l
copylist(l::Cons) = cons(head(l), copylist(tail(l)))
function append2(a, b)
if isa(a,Nil)
b
else
cons(head(a), append2(tail(a), b))
end
end
append(lst::List) = lst
function append(lst::List, lsts...)
n = length(lsts)
l = lsts[n]
for i = (n-1):-1:1
l = append2(lsts[i], l)
end
return append2(lst, l)
end