Permalink
Browse files

Post-examination rearrangement of exercise directories

git-svn-id: https://slps.svn.sourceforge.net/svnroot/slps@773 ab42f6e0-554d-0410-b580-99e487e6eeb2
  • Loading branch information...
grammarware committed Feb 18, 2010
1 parent c93b024 commit aa963450707c5c00535c8a878bffbb6419b26a53
@@ -1,36 +1,35 @@
*/hutton - examples from Graham Hutton's "Programming in Haskell"
haskell1 - simple exercises solved in Haskell
fixed - fixed point computation by iteration in Haskell
hutton - examples from Graham Hutton's "Programming in Haskell"
haskell1 - simple exercises solved in Haskell
fixed - fixed point computation by iteration in Haskell
shapes - the Shapes Problem (object encoding) in Prolog
ccs - the Calculus of Communicating Systems in Prolog (JobShop)
clp - Constraint Logic Programming in Prolog (palindrom dates)
while1 - a DCG parser for a fragment of While
while2 - a DCG parser for While
while3 - big step semantics for While (+ a DCG parser)
while4 - small step semantics for While (+ a DCG parser)
while5 - typed While in Prolog
while6 - While evaluation in Haskell (incomplete)
while7 - While evaluation in Haskell (complete, big step operational)
while8 - While execution in Haskell (complete, denotational)
ewhile - Extended While execution in Haskell (complete, denotational)
while1 - a DCG parser for a fragment of While
while2 - a DCG parser for While
while3 - big step semantics for While (+ a DCG parser)
while4 - small step semantics for While (+ a DCG parser)
while5 - typed While in Prolog
while6 - While evaluation in Haskell (incomplete)
while7 - While evaluation in Haskell (complete, big step operational)
while8 - While execution in Haskell (complete, denotational)
ewhile - Extended While execution in Haskell (complete, denotational)
while_sign - sign detection in While (uses Nielson07 topic)
xml1 - a DCG parser for XML subset (elements only)
xml2 - a DCG parser for XML subset (elements and attributes)
xml1 - a DCG parser for XML subset (elements only)
xml2 - a DCG parser for XML subset (elements and attributes)
b1 - abstract syntax for B in Prolog
b2 - parsing B in Haskell
b3 - folding over expressions for B in Haskell
nb1 - abstract syntax for NB in Prolog
nb2 - semantics and types for NB in Prolog
nb3 - parsing and folding NB in Haskell
nb4 - Expression Problem with NB in Haskell
b1 - abstract syntax for B in Prolog
b2 - parsing B in Haskell
b3 - folding over expressions for B in Haskell
nb1 - abstract syntax for NB in Prolog
nb2 - semantics and types for NB in Prolog
nb3 - parsing and folding NB in Haskell
nb4 - Expression Problem with NB in Haskell
lambda1 - lambda calculus abstract and concrete syntax in Prolog
lambda2 - lambda calculus with Church numbers
lambda3 - lambda calculus abstract syntax, free variables, substitution, evaluation
lambda4 - untyped lambda calculus with alpha conversion & fixed point combinator
lambda5 - typed lambda calculus with alpha conversion in Prolog
lambda6 - typed lambda calculus with alpha conversion & fixed point operator
shapes - the Shapes Problem (object encoding) in Prolog
ccs - the Calculus of Communicating Systems in Prolog (JobShop)
clp - Constraint Logic Programming in Prolog (palindrom dates)
lambda1 - lambda calculus abstract and concrete syntax in Prolog
lambda2 - lambda calculus with Church numbers
lambda3 - lambda calculus abstract syntax, free variables, substitution, evaluation
lambda4 - untyped lambda calculus with alpha conversion & fixed point combinator
lambda5 - typed lambda calculus with alpha conversion in Prolog
lambda6 - typed lambda calculus with alpha conversion & fixed point operator
@@ -1,4 +1,4 @@
test:
runhaskell -ihutton:. parseB.hs
runhaskell -ihutton:../hutton parseB.hs
@@ -1,5 +1,5 @@
test:
runhaskell -ihutton:. Test.hs
runhaskell -ihutton:../hutton Test.hs
clean:
rm -f *~ *.hi *.o
@@ -1,126 +0,0 @@
Functional parsing library from chapter 8 of Programming in Haskell,
Graham Hutton, Cambridge University Press, 2007.
> module Parsing where
>
> import Char
> import Monad
>
> infixr 5 +++
The monad of parsers
--------------------
> newtype Parser a = P (String -> [(a,String)])
>
> instance Monad Parser where
> return v = P (\inp -> [(v,inp)])
> p >>= f = P (\inp -> case parse p inp of
> [] -> []
> [(v,out)] -> parse (f v) out)
>
> instance MonadPlus Parser where
> mzero = P (\inp -> [])
> p `mplus` q = P (\inp -> case parse p inp of
> [] -> parse q inp
> [(v,out)] -> [(v,out)])
Basic parsers
-------------
> failure :: Parser a
> failure = mzero
>
> item :: Parser Char
> item = P (\inp -> case inp of
> [] -> []
> (x:xs) -> [(x,xs)])
>
> parse :: Parser a -> String -> [(a,String)]
> parse (P p) inp = p inp
Choice
------
> (+++) :: Parser a -> Parser a -> Parser a
> p +++ q = p `mplus` q
Derived primitives
------------------
> sat :: (Char -> Bool) -> Parser Char
> sat p = do x <- item
> if p x then return x else failure
>
> digit :: Parser Char
> digit = sat isDigit
>
> lower :: Parser Char
> lower = sat isLower
>
> upper :: Parser Char
> upper = sat isUpper
>
> letter :: Parser Char
> letter = sat isAlpha
>
> alphanum :: Parser Char
> alphanum = sat isAlphaNum
>
> char :: Char -> Parser Char
> char x = sat (== x)
>
> string :: String -> Parser String
> string [] = return []
> string (x:xs) = do char x
> string xs
> return (x:xs)
>
> many :: Parser a -> Parser [a]
> many p = many1 p +++ return []
>
> many1 :: Parser a -> Parser [a]
> many1 p = do v <- p
> vs <- many p
> return (v:vs)
>
> ident :: Parser String
> ident = do x <- lower
> xs <- many alphanum
> return (x:xs)
>
> nat :: Parser Int
> nat = do xs <- many1 digit
> return (read xs)
>
> int :: Parser Int
> int = do char '-'
> n <- nat
> return (-n)
> +++ nat
>
> space :: Parser ()
> space = do many (sat isSpace)
> return ()
Ignoring spacing
----------------
> token :: Parser a -> Parser a
> token p = do space
> v <- p
> space
> return v
>
> identifier :: Parser String
> identifier = token ident
>
> natural :: Parser Int
> natural = token nat
>
> integer :: Parser Int
> integer = token int
>
> symbol :: String -> Parser String
> symbol xs = token (string xs)
@@ -1,7 +0,0 @@
These files were donwloaded from:
http://www.cs.nott.ac.uk/~gmh/book.html
The accompnay this book:
Programming in Haskell
Graham Hutton, University of Nottingham
Cambridge University Press, 2007
@@ -1,142 +0,0 @@
Calculator example from section 9.6 of Programming in Haskell,
Graham Hutton, Cambridge University Press, 2007.
Note: the definition for getCh in this example works with the
Glasgow Haskell Compiler, but may not work with some Haskell
systems, such as Hugs. Moreover, the use of control characters
may not work on some systems, such as WinHugs.
> import Parsing
> import System.IO
Parser for expressions
----------------------
> expr :: Parser Int
> expr = do t <- term
> do symbol "+"
> e <- expr
> return (t + e)
> +++ do symbol "-"
> e <- expr
> return (t - e)
> +++ return t
>
> term :: Parser Int
> term = do f <- factor
> do symbol "*"
> t <- term
> return (f * t)
> +++ do symbol "/"
> t <- term
> return (f `div` t)
> +++ return f
>
> factor :: Parser Int
> factor = do symbol "("
> e <- expr
> symbol ")"
> return e
> +++ integer
Derived primitives
------------------
> getCh :: IO Char
> getCh = do hSetEcho stdin False
> c <- getChar
> hSetEcho stdin True
> return c
>
> beep :: IO ()
> beep = putStr "\BEL"
>
> cls :: IO ()
> cls = putStr "\ESC[2J"
>
> type Pos = (Int,Int)
>
> goto :: Pos -> IO ()
> goto (x,y) = putStr ("\ESC[" ++ show y ++ ";" ++ show x ++ "H")
>
> writeat :: Pos -> String -> IO ()
> writeat p xs = do goto p
> putStr xs
>
> seqn :: [IO a] -> IO ()
> seqn [] = return ()
> seqn (a:as) = do a
> seqn as
The calculator
--------------
> box :: [String]
> box = ["+---------------+",
> "| |",
> "+---+---+---+---+",
> "| q | c | d | = |",
> "+---+---+---+---+",
> "| 1 | 2 | 3 | + |",
> "+---+---+---+---+",
> "| 4 | 5 | 6 | - |",
> "+---+---+---+---+",
> "| 7 | 8 | 9 | * |",
> "+---+---+---+---+",
> "| 0 | ( | ) | / |",
> "+---+---+---+---+"]
>
> buttons :: String
> buttons = standard ++ extra
> where
> standard = "qcd=123+456-789*0()/"
> extra = "QCD \ESC\BS\DEL\n"
>
>
> showbox :: IO ()
> showbox = seqn [writeat (1,y) xs | (y,xs) <- zip [1..13] box]
>
> display xs = do writeat (3,2) " "
> writeat (3,2) (reverse (take 13 (reverse xs)))
>
> calc :: String -> IO ()
> calc xs = do display xs
> c <- getCh
> if elem c buttons then
> process c xs
> else
> do beep
> calc xs
>
> process :: Char -> String -> IO ()
> process c xs
> | elem c "qQ\ESC" = quit
> | elem c "dD\BS\DEL" = delete xs
> | elem c "=\n" = eval xs
> | elem c "cC" = clear
> | otherwise = press c xs
>
> quit :: IO ()
> quit = goto (1,14)
>
> delete :: String -> IO ()
> delete "" = calc ""
> delete xs = calc (init xs)
>
> eval :: String -> IO ()
> eval xs = case parse expr xs of
> [(n,"")] -> calc (show n)
> _ -> do beep
> calc xs
>
> clear :: IO ()
> clear = calc ""
>
> press :: Char -> String -> IO ()
> press c xs = calc (xs ++ [c])
>
> run :: IO ()
> run = do cls
> showbox
> clear
Oops, something went wrong.

0 comments on commit aa96345

Please sign in to comment.