From 4e074b282a8236dcc3599f76ce85d7e52a24d408 Mon Sep 17 00:00:00 2001 From: Camil Staps Date: Mon, 7 Nov 2016 22:36:40 +0000 Subject: Adapted more examples to be standalone --- hamming.icl | 30 ++++++++++++++++++++++- invperm.icl | 12 +++++++++- lqueen.icl | 79 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++- pascal.icl | 52 +++++++++++++++++++++++++++++++++++++++- squeen.icl | 79 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++- 5 files changed, 247 insertions(+), 5 deletions(-) diff --git a/hamming.icl b/hamming.icl index e623d7b..2544b04 100644 --- a/hamming.icl +++ b/hamming.icl @@ -11,7 +11,35 @@ having only 2, 3 and 5 as prime factors. Result: Run the program with the Show Constructors option on (Application options) */ -import StdEnv +(<) infix 4 :: !Int !Int -> Bool +(<) a b = code inline { + ltI +} + +(-) infixl 6 :: !Int !Int -> Int +(-) a b = code inline { + subI +} + +(*) infixl 7 :: !Int !Int -> Int +(*) a b = code inline { + mulI +} + +(==) infix 4 :: !Int !Int -> Bool +(==) a b = code inline { + eqI +} + +map :: (a -> b) [a] -> [b] +map f [] = [] +map f [x:xs] = [f x:map f xs] + +take :: Int [a] -> [a] +take 0 _ = [] +take n [x:xs] = [x:take (n-1) xs] + +//import StdEnv /* Ham (the Hamming function) returns an infinite list of numbers having two, three and five as only prime factors by recursively diff --git a/invperm.icl b/invperm.icl index 5494aa6..4c36b6c 100644 --- a/invperm.icl +++ b/invperm.icl @@ -11,7 +11,17 @@ behavior is however quadratic (in the size of the permutation). Run the program with the Show Constructors option on (Application options) */ -import StdInt, StdMisc +(<) infix 4 :: !Int !Int -> Bool +(<) a b = code inline { + ltI +} + +(+) infixl 6 :: !Int !Int -> Int +(+) a b = code inline { + addI +} + +//import StdInt, StdMisc /* A permutation (Perm) is represented as a list of integers. The resulting inverse permutation (TPerm) is built up as a list of diff --git a/lqueen.icl b/lqueen.icl index f2762a2..cb134c6 100644 --- a/lqueen.icl +++ b/lqueen.icl @@ -2,7 +2,84 @@ module lqueen // The Queens Problem, slow version. -import StdEnv +(&&) infixr 3 :: !Bool Bool -> Bool +(&&) a b + = code { + push_b 0 + jmp_false l1 + pop_b 1 + jsr_eval 0 + pushB_a 0 + pop_a 1 + .d 0 1 b + rtn + :l1 + pop_a 1 + .d 0 1 b + rtn + } + +(||) infixr 2 :: !Bool Bool -> Bool +(||) a b + = code { + push_b 0 + jmp_true l2 + pop_b 1 + jsr_eval 0 + pushB_a 0 + pop_a 1 + .d 0 1 b + rtn + :l2 + pop_a 1 + .d 0 1 b + rtn + } + +not :: !Bool -> Bool +not True = False +not False = True + +(<) infix 4 :: !Int !Int -> Bool +(<) a b = code inline { + ltI +} + +(>) a b :== b < a + +(+) infixl 6 :: !Int !Int -> Int +(+) a b = code inline { + addI +} + +inc :: !Int -> Int +inc a = a + 1 + +(-) infixl 6 :: !Int !Int -> Int +(-) a b = code inline { + subI +} + + +(==) infix 4 :: !Int !Int -> Bool +(==) a b = code inline { + eqI +} + +(<>) infix 4 :: !Int !Int -> Bool +(<>) a b = not (a == b) + +length :: [a] -> Int +length xs = len 0 xs +where + len :: Int [a] -> Int + len i [] = i + len i [_:xs] = len (i+1) xs + +hd :: [a] -> a +hd [x:xs] = x + +//import StdEnv BoardSize :== 8 // The size of the chessboard. diff --git a/pascal.icl b/pascal.icl index 74e15b5..f1aafbc 100644 --- a/pascal.icl +++ b/pascal.icl @@ -16,7 +16,57 @@ Run the program using the "Basic Values Only" option (Application options). Use a non-proportional font for the output (e.g. Monaco 9). */ -import StdEnv +import _SystemArray + +(+) infixl 6 :: !Int !Int -> Int +(+) a b = code inline { + addI +} + +(-) infixl 6 :: !Int !Int -> Int +(-) a b = code inline { + subI +} + +(/) infixl 7 :: !Int !Int -> Int +(/) a b = code inline { + divI +} + +inc :: !Int -> Int +inc a = a + 1 + +take :: Int [a] -> [a] +take 0 _ = [] +take n [x:xs] = [x:take (n-1) xs] + +(+++) infixr 5 :: !{#Char} !{#Char} -> {#Char} +(+++) a b = code inline { + .d 2 0 + jsr catAC + .o 1 0 +} + +repeat :: a -> [a] +repeat x = cons +where cons = [x:cons] + +repeatn :: !.Int a -> [a] +repeatn n x = take n (repeat x) + +spaces :: !.Int -> [Char] +spaces n = repeatn n ' ' + +class toString a :: a -> String + +instance toString [Char] where toString a = {c \\ c <- a} +instance toString Int where toString a = code inline { + .d 0 1 i + jsr ItoAC + .o 1 0 +} + +//import StdEnv /* A row of the triangle is represented by a list of integers, the triangle as a list of rows: diff --git a/squeen.icl b/squeen.icl index cd67c16..205bffd 100644 --- a/squeen.icl +++ b/squeen.icl @@ -19,7 +19,84 @@ programmer that are only 40% slower than this solution (lqueen.icl). */ -import StdEnv +(&&) infixr 3 :: !Bool Bool -> Bool +(&&) a b + = code { + push_b 0 + jmp_false l1 + pop_b 1 + jsr_eval 0 + pushB_a 0 + pop_a 1 + .d 0 1 b + rtn + :l1 + pop_a 1 + .d 0 1 b + rtn + } + +(||) infixr 2 :: !Bool Bool -> Bool +(||) a b + = code { + push_b 0 + jmp_true l2 + pop_b 1 + jsr_eval 0 + pushB_a 0 + pop_a 1 + .d 0 1 b + rtn + :l2 + pop_a 1 + .d 0 1 b + rtn + } + +not :: !Bool -> Bool +not True = False +not False = True + +(<) infix 4 :: !Int !Int -> Bool +(<) a b = code inline { + ltI +} + +(>) a b :== b < a + +(+) infixl 6 :: !Int !Int -> Int +(+) a b = code inline { + addI +} + +inc :: !Int -> Int +inc a = a + 1 + +(-) infixl 6 :: !Int !Int -> Int +(-) a b = code inline { + subI +} + + +(==) infix 4 :: !Int !Int -> Bool +(==) a b = code inline { + eqI +} + +(<>) infix 4 :: !Int !Int -> Bool +(<>) a b = not (a == b) + +length :: [a] -> Int +length xs = len 0 xs +where + len :: Int [a] -> Int + len i [] = i + len i [_:xs] = len (i+1) xs + +hd :: [a] -> a +hd [x:xs] = x + +//import StdEnv BoardSize :== 8 // The size of the chessboard. -- cgit v1.2.3