summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorCamil Staps2016-11-10 10:54:07 +0000
committerCamil Staps2016-11-10 10:54:07 +0000
commit792a1840b61b2875ec8977eff579f15e23159713 (patch)
tree70ae73d251ba5db4d8d0505089b3658d7a8e357f
parentFix pascal.icl (diff)
Made reverse, revtwice and sieve compile
-rw-r--r--reverse.icl28
-rw-r--r--revtwice.icl26
-rw-r--r--sieve.icl31
3 files changed, 78 insertions, 7 deletions
diff --git a/reverse.icl b/reverse.icl
index e2200b5..f3c7082 100644
--- a/reverse.icl
+++ b/reverse.icl
@@ -2,14 +2,38 @@ module reverse
// A list containing n elements will be reversed n times.
-import StdEnv
+//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 [1..n])
+MyReverse n = last (Rev_n n (fromto 1 n))
where
Rev_n::Int [Int] -> [Int]
Rev_n 1 list = Rev list []
diff --git a/revtwice.icl b/revtwice.icl
index e5186d4..31379ec 100644
--- a/revtwice.icl
+++ b/revtwice.icl
@@ -3,13 +3,33 @@ module revtwice
/*
Reversing a list a number of times using Twice.
-Increase stack size to 1m and heap size to 2m to run this program.
+Increase stack size to 1m and heap size to 4m to run this program.
A list containing 25 integers is reversed 65536 times by means
of four applications of the higher order function Twice.
*/
-import StdInt, StdEnum
+(<) 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
+}
+
+fromto :: !Int !Int -> [Int]
+fromto a b
+| b < a = []
+| otherwise = [a:fromto (a+1) b]
+
+//import StdInt, StdEnum
Revv:: [Int] -> [Int]
Revv l = Rev l []
@@ -22,5 +42,5 @@ Twice::(x -> x) x -> x
Twice f x = f (f x)
Start::[Int]
-Start = Twice Twice Twice Twice Revv [1..25]
+Start = Twice Twice Twice Twice Revv (fromto 1 25)
diff --git a/sieve.icl b/sieve.icl
index 5e1681e..326a5d5 100644
--- a/sieve.icl
+++ b/sieve.icl
@@ -2,13 +2,40 @@ module sieve
// The standard Sieve of Eratosthenes.
-import StdEnv
+//import StdEnv
+
+(+) infixl 6 :: !Int !Int -> Int
+(+) a b = code inline {
+ addI
+}
+
+(-) infixl 6 :: !Int !Int -> Int
+(-) a b = code inline {
+ subI
+}
+
+(==) infix 4 :: !Int !Int -> Bool
+(==) a b = code inline {
+ eqI
+}
+
+(rem) infix 7 :: !Int !Int -> Int
+(rem) a b = code inline {
+ remI
+}
+
+fro :: !Int -> [Int]
+fro i = [i:fro (i+1)]
+
+take :: Int [a] -> [a]
+take 0 _ = []
+take n [x:xs] = [x:take (n-1) xs]
NrOfPrimes :== 1000
// The sieve algorithm: generate an infinite list of all primes.
-Start = take NrOfPrimes (sieve [2..])
+Start = take NrOfPrimes (sieve (fro 2))
where
sieve [prime:rest] = [prime : sieve (filter prime rest)]