--
module SimpleGraphics where
import SOEGraphics
---------------------------------------------------------------
----- first the code from the lecture that talks about actions
data Bintree a = Lf a
| (Bintree a) :/\: (Bintree a)
leftmost :: a -> Bintree a -> Bintree a
leftmost new (Lf x) = Lf new
leftmost new (x :/\: y) = (leftmost new x) :/\: y
replace :: Eq a => a -> a -> Bintree a -> Bintree a
replace new old (Lf x) =
if x==old then Lf new else Lf x
replace new old (x :/\: y) =
(replace new old x) :/\: (replace new old y)
replace' :: Eq a => a -> a -> Bintree a -> Bintree a
replace' new old w =
let replace2 (Lf x) =
if x==old then (Lf new,True) else (Lf x,False)
replace2 (x :/\: y) =
(case (replace2 x,replace2 y) of
((a,False),(b,False)) -> (x :/\: y, False)
((a,True),(b,False)) -> (a :/\: y,True)
((a,False),(b,True)) -> (x :/\: b,True)
((a,True),(b,True)) -> (a :/\: b,True))
in fst(replace2 w)
ex2 :: IO ()
ex2 =
do { c1 <- getChar
; c2 <- getChar -- the newline
; putChar c1
; putChar c2
}
getLine' :: IO [Char]
getLine' =
do { c <- getChar -- get a char
; if c == '\n' -- if its newline
then return "" -- no-op action which
-- returns empty string
-- recursively
else do { l <- getLine' -- get a line
; return (c:l) -- no-op action
} -- to cons c to l
}
-- the word count example
wc (cc,w,lc) [] = (cc,w,lc)
wc (cc,w,lc) (' ' : xs) = wc (cc+1,w+1,lc) xs
wc (cc,w,lc) ('\t' : xs) = wc (cc+1,w+1,lc) xs
wc (cc,w,lc) ('\n' : xs) = wc (cc+1,w+1,lc+1) xs
wc (cc,w,lc) (x : xs) = wc (cc+1,w,lc) xs
ex7 =
do { name <- getLine
; z <- readFile name
; let (cc,w,lc) = wc (0,0,0) z
; putStr ("The file: "++ name ++
" has \n "++(show cc)++
" characters\n "++(show w)++
" words\n "++(show lc)++
" lines\n")
}
-----------------------------------------------------------------
----- Now the simple graphics code
main = putStr "hello world"
main0 =
runGraphics(
do { w <- openWindow "First window" (300,300)
; drawInWindow w (text (100,200) "hello world")
; k <- getKey w
; closeWindow w
} )
spaceClose :: Window -> IO ()
spaceClose w =
do { k <- getKey w
; if k == ' ' then closeWindow w
else spaceClose w
}
main1 =
runGraphics(
do { w <- openWindow "Second Program" (300,300)
; drawInWindow w (text (100,200) "hello Again")
; spaceClose w
} )
--