summaryrefslogtreecommitdiff
path: root/reverse.icl
blob: f3c70820461aa18c0dbf6d5002f34621ce70acc8 (plain) (blame)
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
module reverse

//	A list containing n elements will be reversed n times.

//import StdEnv

(<) infix 4 :: !Int !Int -> Bool
(<) a b = code inline {
	ltI
}

(+) infixl 6 :: !Int !Int -> Int
(+) a b = code inline {
	addI
}

(-) infixl 6 :: !Int !Int -> Int
(-) a b = code inline {
	subI
}

last :: [a] -> a
last [x] = x
last [_:xs] = last xs

fromto :: !Int !Int -> [Int]
fromto a b
| b < a     = []
| otherwise = [a:fromto (a+1) b]

NrOfTimes :== 1000
	
//	Reversing a list of n integers n times.

MyReverse::Int -> Int
MyReverse n =  last (Rev_n n (fromto 1 n))
where
	Rev_n::Int [Int] -> [Int]
	Rev_n 1 list	=  Rev list []
	Rev_n n list	=  Rev_n (n - 1) (Rev list [])

	Rev::[Int] [Int] -> [Int]
	Rev [x:r]	list	=  Rev r [x : list]
	Rev []		list	=  list


//	The Start rule.

Start::Int
Start = MyReverse NrOfTimes