diff options
author | Camil Staps | 2016-02-02 19:24:50 +0100 |
---|---|---|
committer | Camil Staps | 2016-02-02 19:24:50 +0100 |
commit | a7d7542dc646a5fd124ef71e71ce260889f1701b (patch) | |
tree | 04ed89503bbb3cc9933273a1326a53ca724c3492 /1415/fp2/week1 | |
parent | week6 camil: working positioning of lines by putting empties at left and righ... (diff) |
Diffstat (limited to '1415/fp2/week1')
28 files changed, 484 insertions, 0 deletions
diff --git a/1415/fp2/week1/camil/Galgje.icl b/1415/fp2/week1/camil/Galgje.icl new file mode 100644 index 0000000..75b09d0 --- /dev/null +++ b/1415/fp2/week1/camil/Galgje.icl @@ -0,0 +1,58 @@ +// Mart Lubbers s4109503, Camil Staps s4498062
+
+module Galgje
+
+import StdEnv, SimpleFileIO, RandomGetallen
+
+lexicon_file = "lexicon.txt"
+
+// Is a Char a member of a String
+isMemberString :: String Char -> Bool
+isMemberString "" c = False
+isMemberString s c = s.[0] == c || isMemberString (s % (1,size s - 1)) c
+
+// From the slides
+skip_nl :: String -> String
+skip_nl str = if (size str > 0 && str.[size str-1] == '\n') (str%(0,size str-2)) str
+
+// From a String and a List of guesses (Chars), return a String that shows dots for letters that were not guessed yet
+stripUnknown :: String [Char] -> String
+stripUnknown s g = toString [if (isMember c g) c '.' \\ c <- (fromString s)]
+
+// Get a random word from the lexicon file
+randomWord :: *env -> (Maybe String, *env) | FileSystem env
+randomWord env
+# (ss,env) = readLines lexicon_file env
+| ss == Nothing = (Nothing, env)
+# (seed,env) = getNewRandomSeed env
+| otherwise = (Just (skip_nl ((shuffle (fromJust ss) seed)!!0)), env)
+
+// word, guesses, mistakes left, stdio -> (win, new guesses, stdio)
+play :: String [Char] Int *File *env -> (Bool, [Char], *File, *env) | FileSystem env
+play w g n io world
+# io = io <<< stripUnknown w g <<< '\n'
+| stripUnknown w g == w = (True, g, io, world)
+# (round,world) = readFile ("round-" +++ (toString n) +++ ".txt") world
+| round == Nothing = abort "Couldn't get gallow"
+# io = io <<< (fromJust round)
+# io = io <<< "Guess (" <<< toString n <<< "): "
+# (ok,g`,io) = freadc io
+# (_,io) = freadline io // to read until the next \n
+| not ok = abort "Couldn't get guessed letter"
+| isMemberString w g` = play w [g`:g] n io world
+| n == 0 = (False, [g`:g], io, world)
+| otherwise = play w [g`:g] (n-1) io world
+
+Start :: *World -> *World
+Start world
+# (word,world) = randomWord world
+| word == Nothing = abort "Couldn't get random word"
+# word = fromJust word
+# (io,world) = stdio world
+# (win,g,io,world) = play word [] 5 io world
+# (lost,world) = readFile "round-lost.txt" world
+| lost == Nothing = abort "Couldn't get gallow"
+# io = if win (io <<< "You win!\n") (io <<< "You lose!\n" <<< fromJust lost)
+# (ok,world) = fclose io world
+| not ok = abort "Couldn't close stdio"
+| otherwise = world
diff --git a/1415/fp2/week1/camil/Random.dcl b/1415/fp2/week1/camil/Random.dcl new file mode 100644 index 0000000..47a7c18 --- /dev/null +++ b/1415/fp2/week1/camil/Random.dcl @@ -0,0 +1,19 @@ +definition module Random + + // Random number generator voor Linux gebruikers + // interface compatible met Random.dcl (helaas) + // -- mschool@science.ru.nl + +import StdFile + +:: RandomSeed + +// nullRandomSeed generates a fixed RandomSeed +nullRandomSeed :: RandomSeed + +// GetNewRandomSeed generates a good RandomSeed, using /dev/urandom +getNewRandomSeed :: !*env -> (!RandomSeed, !*env) | FileSystem env + +// Given a RandomSeed, Random generates a random number and a new RandomSeed. +random :: !RandomSeed -> .(!Int, !RandomSeed) + diff --git a/1415/fp2/week1/camil/Random.icl b/1415/fp2/week1/camil/Random.icl new file mode 100644 index 0000000..b6e0768 --- /dev/null +++ b/1415/fp2/week1/camil/Random.icl @@ -0,0 +1,20 @@ +implementation module Random + +import StdFile, StdList, StdMisc, StdArray, Random + +:: RandomSeed :== Int + +nullRandomSeed :: RandomSeed +nullRandomSeed = 0 + +getNewRandomSeed :: !*env -> (!RandomSeed, !*env) | FileSystem env +getNewRandomSeed env +# (ok, src, env) = sfopen "/dev/urandom" FReadData env +| not ok => abort "could not open /dev/urandom" +# (bytes, src) = sfreads src 4 + seed = foldl (\x y->(x<<8)+toInt y) 0 [c \\ c<-:bytes] +| otherwise => (seed, env) + +random :: !RandomSeed -> .(!Int, !RandomSeed) +random seed = (seed>>16 bitand 0xFFFF, seed*0x08088405+1) + diff --git a/1415/fp2/week1/camil/SimpleFileIO.dcl b/1415/fp2/week1/camil/SimpleFileIO.dcl new file mode 100644 index 0000000..1bd97da --- /dev/null +++ b/1415/fp2/week1/camil/SimpleFileIO.dcl @@ -0,0 +1,14 @@ +definition module SimpleFileIO
+
+import StdFile, StdOverloaded, StdMaybe
+
+// 1.
+readFile :: String *env -> (Maybe String, *env) | FileSystem env
+writeFile :: String String *env -> (Bool, *env) | FileSystem env
+
+// 2.
+readLines :: String *env -> (Maybe [String],*env) | FileSystem env
+writeLines :: String [String] *env -> (Bool, *env) | FileSystem env
+
+// 3.
+//mapFile :: String String (a -> b) *env -> (Bool, *env) | FileSystem env & ... a & ... b
diff --git a/1415/fp2/week1/camil/SimpleFileIO.icl b/1415/fp2/week1/camil/SimpleFileIO.icl new file mode 100644 index 0000000..b2a483a --- /dev/null +++ b/1415/fp2/week1/camil/SimpleFileIO.icl @@ -0,0 +1,39 @@ +implementation module SimpleFileIO
+
+import StdEnv, StdFile, StdOverloaded, StdMaybe
+
+// 1.
+readFile :: String *env -> (Maybe String, *env) | FileSystem env
+readFile s env
+# (ss, env) = readLines s env
+| ss == Nothing = (Nothing, env)
+| otherwise = (Just (foldl (+++) "" (fromJust ss)), env)
+
+writeFile :: String String *env -> (Bool, *env) | FileSystem env
+writeFile fn s env
+# (ok, outfile, env) = fopen fn FWriteText env
+| not ok = (False, env)
+# outfile = fwrites s outfile
+# (ok, env) = fclose outfile env
+| otherwise = (ok, env)
+
+// 2.
+readLines :: String *env -> (Maybe [String],*env) | FileSystem env
+readLines s env
+# (ok, infile, env) = sfopen s FReadText env
+| not ok = (Nothing, env)
+| otherwise = (Just (fst (readLines` infile)), env)
+where
+ readLines` :: File -> ([String], File)
+ readLines` file
+ | sfend file = ([], file)
+ # (line, file) = sfreadline file
+ # (ss, file) = readLines` file
+ | otherwise = ([line : ss], file)
+
+writeLines :: String [String] *env -> (Bool, *env) | FileSystem env
+writeLines fn ss env = writeFile fn (foldl (+++) "" [s +++ "\n" \\ s <- ss]) env
+
+// 3.
+//mapFile :: String String (a -> b) *env -> (Bool, *env) | FileSystem env & ... a & ... b
+
diff --git a/1415/fp2/week1/camil/StdMaybe.dcl b/1415/fp2/week1/camil/StdMaybe.dcl new file mode 100644 index 0000000..2403683 --- /dev/null +++ b/1415/fp2/week1/camil/StdMaybe.dcl @@ -0,0 +1,41 @@ +definition module StdMaybe + +// ******************************************************************************** +// Clean StdLib library module, version 1.0 +// ******************************************************************************** + +from StdFunc import :: St; +from StdOverloaded import class ==(..); + +:: Maybe x + = Just x + | Nothing + +isJust :: !(Maybe .x) -> Bool // case @1 of (Just _) -> True; _ -> False +isNothing :: !(Maybe .x) -> Bool // not o isJust +fromJust :: !(Maybe .x) -> .x // \(Just x) -> x + +// for possibly unique elements: +u_isJust :: !u:(Maybe .x) -> (!Bool, !u:Maybe .x) +u_isNothing :: !u:(Maybe .x) -> (!Bool, !u:Maybe .x) + +accMaybe :: .(St .x .a) !u:(Maybe .x) -> (!Maybe .a,!u:Maybe .x) +// accMaybe f (Just x) = (Just (fst (f x)),Just (snd (f x))) +// accMaybe f Nothing = (Nothing,Nothing) + +mapMaybe :: .(.x -> .y) !(Maybe .x) -> Maybe .y +// mapMaybe f (Just x) = Just (f x) +// mapMaybe f Nothing = Nothing + +instance == (Maybe x) | == x +// Nothing==Nothing +// Just a ==Just b <= a==b + +maybeToList :: !(Maybe .a) -> [.a]; +// returns list with no or one element + +listToMaybe :: ![.a] -> .Maybe .a; +// returns Just head of list if possible + +catMaybes :: ![Maybe .a] -> .[.a]; +// catMaybes ms = [ m \\ Just m <- ms ] diff --git a/1415/fp2/week1/camil/StdMaybe.icl b/1415/fp2/week1/camil/StdMaybe.icl new file mode 100644 index 0000000..4eed325 --- /dev/null +++ b/1415/fp2/week1/camil/StdMaybe.icl @@ -0,0 +1,65 @@ +implementation module StdMaybe + +// ******************************************************************************** +// Clean StdLib library module, version 1.0 +// ******************************************************************************** + +from StdFunc import :: St; +from StdOverloaded import class ==(..); + +:: Maybe x + = Just x + | Nothing + +isJust :: !(Maybe .x) -> Bool +isJust Nothing = False +isJust _ = True + +isNothing :: !(Maybe .x) -> Bool +isNothing Nothing = True +isNothing _ = False + +u_isJust :: !u:(Maybe .x) -> (!Bool, !u:Maybe .x) +u_isJust nothing=:Nothing + = (False, nothing) +u_isJust just + = (True, just) + +u_isNothing :: !u:(Maybe .x) -> (!Bool, !u:Maybe .x) +u_isNothing nothing=:Nothing + = (True, nothing) +u_isNothing just + = (False,just) + +fromJust :: !(Maybe .x) -> .x +fromJust (Just x) = x + +accMaybe :: .(St .x .a) !u:(Maybe .x) -> (!Maybe .a,!u:Maybe .x) +accMaybe f (Just x) + # (a,x) = f x + = (Just a,Just x) +accMaybe _ nothing + = (Nothing,nothing) + +mapMaybe :: .(.x -> .y) !(Maybe .x) -> Maybe .y +mapMaybe f (Just x) = Just (f x) +mapMaybe _ nothing = Nothing + +instance == (Maybe x) | == x where + (==) Nothing maybe = case maybe of + Nothing -> True + just -> False + (==) (Just a) maybe = case maybe of + Just b -> a==b + nothing -> False + +maybeToList :: !(Maybe .a) -> [.a]; +maybeToList Nothing = [] +maybeToList (Just a) = [a] + +listToMaybe :: ![.a] -> .Maybe .a; +listToMaybe [] = Nothing +listToMaybe [a:_] = Just a + +catMaybes :: ![Maybe .a] -> .[.a]; +catMaybes ms = [ m \\ Just m <- ms ] diff --git a/1415/fp2/week1/camil/lexicon.txt b/1415/fp2/week1/camil/lexicon.txt new file mode 100644 index 0000000..9c2ad4e --- /dev/null +++ b/1415/fp2/week1/camil/lexicon.txt @@ -0,0 +1,18 @@ +armada +balinese +bergens +cyprus +europeanen +guldensporenslag +hollandermop +jordaans +lagerhuis +luiker +mensenzoon +opperwezen +randstad +samaritaan +sovjettijd +thailand +vietnamese +zeeuws diff --git a/1415/fp2/week1/camil/round-0.txt b/1415/fp2/week1/camil/round-0.txt new file mode 100644 index 0000000..7236e17 --- /dev/null +++ b/1415/fp2/week1/camil/round-0.txt @@ -0,0 +1,7 @@ + ------ + | \ | + o \| + /O\ | + | + | +________ diff --git a/1415/fp2/week1/camil/round-1.txt b/1415/fp2/week1/camil/round-1.txt new file mode 100644 index 0000000..8694aad --- /dev/null +++ b/1415/fp2/week1/camil/round-1.txt @@ -0,0 +1,7 @@ + ------ + | \ | + o \| + | + | + | +________ diff --git a/1415/fp2/week1/camil/round-2.txt b/1415/fp2/week1/camil/round-2.txt new file mode 100644 index 0000000..d46d77c --- /dev/null +++ b/1415/fp2/week1/camil/round-2.txt @@ -0,0 +1,7 @@ + ------ + | \ | + \| + | + | + | +________ diff --git a/1415/fp2/week1/camil/round-3.txt b/1415/fp2/week1/camil/round-3.txt new file mode 100644 index 0000000..9bdd2ba --- /dev/null +++ b/1415/fp2/week1/camil/round-3.txt @@ -0,0 +1,7 @@ + ------ + \ | + \| + | + | + | +________ diff --git a/1415/fp2/week1/camil/round-4.txt b/1415/fp2/week1/camil/round-4.txt new file mode 100644 index 0000000..41ca216 --- /dev/null +++ b/1415/fp2/week1/camil/round-4.txt @@ -0,0 +1,7 @@ + + | + | + | + | + | +________ diff --git a/1415/fp2/week1/camil/round-5.txt b/1415/fp2/week1/camil/round-5.txt new file mode 100644 index 0000000..f29c0dd --- /dev/null +++ b/1415/fp2/week1/camil/round-5.txt @@ -0,0 +1,7 @@ + + + + + + +________ diff --git a/1415/fp2/week1/camil/round-lost.txt b/1415/fp2/week1/camil/round-lost.txt new file mode 100644 index 0000000..7ec2fa7 --- /dev/null +++ b/1415/fp2/week1/camil/round-lost.txt @@ -0,0 +1,7 @@ + ------ + | \ | + o \| + /O\ | + / \ | + | +________ diff --git a/1415/fp2/week1/camil/week1.tar.gz b/1415/fp2/week1/camil/week1.tar.gz Binary files differnew file mode 100644 index 0000000..3533265 --- /dev/null +++ b/1415/fp2/week1/camil/week1.tar.gz diff --git a/1415/fp2/week1/mart/Echo b/1415/fp2/week1/mart/Echo Binary files differnew file mode 100755 index 0000000..cf2fb79 --- /dev/null +++ b/1415/fp2/week1/mart/Echo diff --git a/1415/fp2/week1/mart/Echo.icl b/1415/fp2/week1/mart/Echo.icl new file mode 100644 index 0000000..30a6f4b --- /dev/null +++ b/1415/fp2/week1/mart/Echo.icl @@ -0,0 +1,11 @@ +module Echo
+
+import StdEnv
+
+
+Start :: *World -> *World
+Start world
+# (console, world) = stdio world
+# (line, console) = freadline console
+| not (fend console) = fwrites line
+| otherwise = world
diff --git a/1415/fp2/week1/mart/Galgje b/1415/fp2/week1/mart/Galgje Binary files differnew file mode 100755 index 0000000..d46de77 --- /dev/null +++ b/1415/fp2/week1/mart/Galgje diff --git a/1415/fp2/week1/mart/Galgje.icl b/1415/fp2/week1/mart/Galgje.icl new file mode 100644 index 0000000..e5106ee --- /dev/null +++ b/1415/fp2/week1/mart/Galgje.icl @@ -0,0 +1,13 @@ +module Galgje
+
+import StdEnv, Random
+
+//randomWord :: *env -> (Maybe String, *env) | FileSystem env
+//randomWord env
+//# (ss,env) = readLines lexicon_file env
+//| ss == Nothing = (Nothing, env)
+//# (seed,env) = getNewRandomSeed env
+//| otherwise = (Just (skip_nl ((shuffle (fromJust ss) seed)!!0)), env)
+
+Start :: *World -> *World
+Start world = world
diff --git a/1415/fp2/week1/mart/GalgjeWF.dcl b/1415/fp2/week1/mart/GalgjeWF.dcl new file mode 100644 index 0000000..a777b95 --- /dev/null +++ b/1415/fp2/week1/mart/GalgjeWF.dcl @@ -0,0 +1,5 @@ +definition module GalgjeWF
+
+import iTasks
+
+galgje :: [Workflow]
diff --git a/1415/fp2/week1/mart/Random.dcl b/1415/fp2/week1/mart/Random.dcl new file mode 100644 index 0000000..47a7c18 --- /dev/null +++ b/1415/fp2/week1/mart/Random.dcl @@ -0,0 +1,19 @@ +definition module Random + + // Random number generator voor Linux gebruikers + // interface compatible met Random.dcl (helaas) + // -- mschool@science.ru.nl + +import StdFile + +:: RandomSeed + +// nullRandomSeed generates a fixed RandomSeed +nullRandomSeed :: RandomSeed + +// GetNewRandomSeed generates a good RandomSeed, using /dev/urandom +getNewRandomSeed :: !*env -> (!RandomSeed, !*env) | FileSystem env + +// Given a RandomSeed, Random generates a random number and a new RandomSeed. +random :: !RandomSeed -> .(!Int, !RandomSeed) + diff --git a/1415/fp2/week1/mart/Random.icl b/1415/fp2/week1/mart/Random.icl new file mode 100644 index 0000000..b6e0768 --- /dev/null +++ b/1415/fp2/week1/mart/Random.icl @@ -0,0 +1,20 @@ +implementation module Random + +import StdFile, StdList, StdMisc, StdArray, Random + +:: RandomSeed :== Int + +nullRandomSeed :: RandomSeed +nullRandomSeed = 0 + +getNewRandomSeed :: !*env -> (!RandomSeed, !*env) | FileSystem env +getNewRandomSeed env +# (ok, src, env) = sfopen "/dev/urandom" FReadData env +| not ok => abort "could not open /dev/urandom" +# (bytes, src) = sfreads src 4 + seed = foldl (\x y->(x<<8)+toInt y) 0 [c \\ c<-:bytes] +| otherwise => (seed, env) + +random :: !RandomSeed -> .(!Int, !RandomSeed) +random seed = (seed>>16 bitand 0xFFFF, seed*0x08088405+1) + diff --git a/1415/fp2/week1/mart/RandomGetallen b/1415/fp2/week1/mart/RandomGetallen Binary files differnew file mode 100755 index 0000000..0482437 --- /dev/null +++ b/1415/fp2/week1/mart/RandomGetallen diff --git a/1415/fp2/week1/mart/RandomGetallen.dcl b/1415/fp2/week1/mart/RandomGetallen.dcl new file mode 100644 index 0000000..66a2c6c --- /dev/null +++ b/1415/fp2/week1/mart/RandomGetallen.dcl @@ -0,0 +1,7 @@ +definition module RandomGetallen
+
+import Random
+
+random_n :: Int RandomSeed -> ([Int],RandomSeed)
+random_inf :: RandomSeed -> [Int]
+//shuffle :: [a] RandomSeed -> [a]
diff --git a/1415/fp2/week1/mart/RandomGetallen.icl b/1415/fp2/week1/mart/RandomGetallen.icl new file mode 100644 index 0000000..b756c91 --- /dev/null +++ b/1415/fp2/week1/mart/RandomGetallen.icl @@ -0,0 +1,33 @@ +implementation module RandomGetallen
+
+import StdEnv, Random
+
+//Start :: *World -> ([Int],*World)
+//Start world
+//# (rs,world) = getNewRandomSeed world
+//= (shuffle [1..10] rs,world)
+
+
+Start = shuffle [1..10] nullRandomSeed
+
+random_n :: Int RandomSeed -> ([Int],RandomSeed)
+random_n n seed = seqList (repeatn n random) seed
+
+random_inf :: RandomSeed -> [Int]
+random_inf seed = iterateSt random seed
+
+iterateSt :: (s -> (a,s)) s -> [a]
+iterateSt f s = [a : iterateSt f s`]
+where
+ (a,s`) = f s
+
+shuffle :: [a] RandomSeed -> [a]
+shuffle xs seed = (perms xs) !! ((fst (random seed)) rem (fac (length xs)))
+
+fac :: Int -> Int
+fac 0 = 1
+fac n = n * fac (n-1)
+
+perms :: [a] -> [[a]]
+perms [] = [[]]
+perms xs = [[xs!!i : xs`] \\ i <- [0..length xs - 1] , xs` <- perms (take i xs ++ drop (i+1) xs)]
diff --git a/1415/fp2/week1/mart/SimpleFileIO.dcl b/1415/fp2/week1/mart/SimpleFileIO.dcl new file mode 100644 index 0000000..1bd97da --- /dev/null +++ b/1415/fp2/week1/mart/SimpleFileIO.dcl @@ -0,0 +1,14 @@ +definition module SimpleFileIO
+
+import StdFile, StdOverloaded, StdMaybe
+
+// 1.
+readFile :: String *env -> (Maybe String, *env) | FileSystem env
+writeFile :: String String *env -> (Bool, *env) | FileSystem env
+
+// 2.
+readLines :: String *env -> (Maybe [String],*env) | FileSystem env
+writeLines :: String [String] *env -> (Bool, *env) | FileSystem env
+
+// 3.
+//mapFile :: String String (a -> b) *env -> (Bool, *env) | FileSystem env & ... a & ... b
diff --git a/1415/fp2/week1/mart/SimpleFileIO.icl b/1415/fp2/week1/mart/SimpleFileIO.icl new file mode 100644 index 0000000..b2a483a --- /dev/null +++ b/1415/fp2/week1/mart/SimpleFileIO.icl @@ -0,0 +1,39 @@ +implementation module SimpleFileIO
+
+import StdEnv, StdFile, StdOverloaded, StdMaybe
+
+// 1.
+readFile :: String *env -> (Maybe String, *env) | FileSystem env
+readFile s env
+# (ss, env) = readLines s env
+| ss == Nothing = (Nothing, env)
+| otherwise = (Just (foldl (+++) "" (fromJust ss)), env)
+
+writeFile :: String String *env -> (Bool, *env) | FileSystem env
+writeFile fn s env
+# (ok, outfile, env) = fopen fn FWriteText env
+| not ok = (False, env)
+# outfile = fwrites s outfile
+# (ok, env) = fclose outfile env
+| otherwise = (ok, env)
+
+// 2.
+readLines :: String *env -> (Maybe [String],*env) | FileSystem env
+readLines s env
+# (ok, infile, env) = sfopen s FReadText env
+| not ok = (Nothing, env)
+| otherwise = (Just (fst (readLines` infile)), env)
+where
+ readLines` :: File -> ([String], File)
+ readLines` file
+ | sfend file = ([], file)
+ # (line, file) = sfreadline file
+ # (ss, file) = readLines` file
+ | otherwise = ([line : ss], file)
+
+writeLines :: String [String] *env -> (Bool, *env) | FileSystem env
+writeLines fn ss env = writeFile fn (foldl (+++) "" [s +++ "\n" \\ s <- ss]) env
+
+// 3.
+//mapFile :: String String (a -> b) *env -> (Bool, *env) | FileSystem env & ... a & ... b
+
|