From a7d7542dc646a5fd124ef71e71ce260889f1701b Mon Sep 17 00:00:00 2001 From: Camil Staps Date: Tue, 2 Feb 2016 19:24:50 +0100 Subject: Moved to 1415 directory --- 1415/fp2/week1/camil/Galgje.icl | 58 ++++++++++++++++++++++++++++++ 1415/fp2/week1/camil/Random.dcl | 19 ++++++++++ 1415/fp2/week1/camil/Random.icl | 20 +++++++++++ 1415/fp2/week1/camil/SimpleFileIO.dcl | 14 ++++++++ 1415/fp2/week1/camil/SimpleFileIO.icl | 39 ++++++++++++++++++++ 1415/fp2/week1/camil/StdMaybe.dcl | 41 +++++++++++++++++++++ 1415/fp2/week1/camil/StdMaybe.icl | 65 ++++++++++++++++++++++++++++++++++ 1415/fp2/week1/camil/lexicon.txt | 18 ++++++++++ 1415/fp2/week1/camil/round-0.txt | 7 ++++ 1415/fp2/week1/camil/round-1.txt | 7 ++++ 1415/fp2/week1/camil/round-2.txt | 7 ++++ 1415/fp2/week1/camil/round-3.txt | 7 ++++ 1415/fp2/week1/camil/round-4.txt | 7 ++++ 1415/fp2/week1/camil/round-5.txt | 7 ++++ 1415/fp2/week1/camil/round-lost.txt | 7 ++++ 1415/fp2/week1/camil/week1.tar.gz | Bin 0 -> 3450 bytes 16 files changed, 323 insertions(+) create mode 100644 1415/fp2/week1/camil/Galgje.icl create mode 100644 1415/fp2/week1/camil/Random.dcl create mode 100644 1415/fp2/week1/camil/Random.icl create mode 100644 1415/fp2/week1/camil/SimpleFileIO.dcl create mode 100644 1415/fp2/week1/camil/SimpleFileIO.icl create mode 100644 1415/fp2/week1/camil/StdMaybe.dcl create mode 100644 1415/fp2/week1/camil/StdMaybe.icl create mode 100644 1415/fp2/week1/camil/lexicon.txt create mode 100644 1415/fp2/week1/camil/round-0.txt create mode 100644 1415/fp2/week1/camil/round-1.txt create mode 100644 1415/fp2/week1/camil/round-2.txt create mode 100644 1415/fp2/week1/camil/round-3.txt create mode 100644 1415/fp2/week1/camil/round-4.txt create mode 100644 1415/fp2/week1/camil/round-5.txt create mode 100644 1415/fp2/week1/camil/round-lost.txt create mode 100644 1415/fp2/week1/camil/week1.tar.gz (limited to '1415/fp2/week1/camil') 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 new file mode 100644 index 0000000..3533265 Binary files /dev/null and b/1415/fp2/week1/camil/week1.tar.gz differ -- cgit v1.2.3