Craft3e 0.2.0.2 → 0.2.0.3
raw patch · 7 files changed
+116/−22 lines, 7 filesPVP: major bump suggested
API removals or changes: PVP suggests a major version bump
API changes (from Hackage documentation)
- Chapter12: (<**>) :: RegExp -> RegExp -> RegExp
- Chapter12: (<*>) :: RegExp -> RegExp -> RegExp
+ Chapter12: (<+++>) :: RegExp -> RegExp -> RegExp
+ Chapter12: (<++>) :: RegExp -> RegExp -> RegExp
+ Chapter12: Chr :: Char -> RE
+ Chapter12: Epsilon :: RE
+ Chapter12: Or :: RE -> RE -> RE
+ Chapter12: Seq :: RE -> RE -> RE
+ Chapter12: Star :: RE -> RE
+ Chapter12: data RE
+ Chapter12: instance GHC.Classes.Eq Chapter12.RE
+ Chapter12: instance GHC.Show.Show Chapter12.RE
+ Chapter12: match :: RE -> RegExp
+ Chapter12: simplify :: RE -> RE
+ ParseLib: instance Control.Monad.Fail.MonadFail (ParseLib.SParse a)
+ Pictures: render :: Picture -> IO ()
+ Test: (^^^) :: Integer -> Integer -> Integer
+ Test: fac :: Integer -> Integer
+ Test: fib :: Integer -> Integer
+ Test: fibP :: Integer -> (Integer, Integer)
+ Test: howManyEqual :: Integer -> Integer -> Integer -> Integer
+ Test: maxThreeOccurs :: Integer -> Integer -> Integer -> (Integer, Integer)
+ Test: pow :: Integer -> Integer
+ Test: sumFun :: (Integer -> Integer) -> Integer -> Integer
- Chapter12: epsilon :: String -> Bool
+ Chapter12: epsilon :: RegExp
Files
- Chapter12.hs +41/−8
- Chapter19/ParseLib.hs +4/−1
- Craft3e.cabal +2/−4
- ParseLib.hs +4/−1
- Pictures.hs +5/−4
- Test.hs +60/−0
- svgOut.xml +0/−4
Chapter12.hs view
@@ -53,10 +53,11 @@ type RegExp = String -> Bool -char :: Char -> RegExp +epsilon :: RegExp epsilon = (=="") +char :: Char -> RegExp char ch = (==[ch]) (|||) :: RegExp -> RegExp -> RegExp@@ -64,27 +65,59 @@ e1 ||| e2 = \x -> e1 x || e2 x -(<*>) :: RegExp -> RegExp -> RegExp+(<++>) :: RegExp -> RegExp -> RegExp -e1 <*> e2 =+e1 <++> e2 = \x -> or [ e1 y && e2 z | (y,z) <- splits x ] -(<**>) :: RegExp -> RegExp -> RegExp+(<+++>) :: RegExp -> RegExp -> RegExp -e1 <**> e2 =+e1 <+++> e2 = \x -> or [ e1 y && e2 z | (y,z) <- fsplits x ] +splits :: [a] -> [([a], [a])]+ splits xs = [splitAt n xs | n<-[0..len]] where len = length xs star :: RegExp -> RegExp -star p = epsilon ||| (p <**> star p)--- epsilon ||| (p <*> star p)+star p = epsilon ||| (p <+++> star p)+-- epsilon ||| (p <++> star p) -- is OK as long as p can't have epsilon match -fsplits xs = tail (splits xs)+fsplits :: [a] -> [([a], [a])]++fsplits = tail . splits++-- Regular expressions as data++data RE =+ Epsilon |+ Chr Char |+ Or RE RE |+ Seq RE RE |+ Star RE+ deriving (Show,Eq)++-- Interpreting REs as functions i.e. RegExps++match :: RE -> RegExp+match Epsilon = epsilon+match (Chr c) = char c+match (Or e f) = match e ||| match f+match (Seq e f) = match e <++> match f+match (Star e) = star (match e)++-- Simplifying REs …++simplify :: RE -> RE+simplify (Seq Epsilon e) = e+simplify (Seq e Epsilon) = e+simplify (Star (Star e)) = Star e+ -- can add more cases here …+simplify x = x -- -- Case studies: functions as data
Chapter19/ParseLib.hs view
@@ -123,8 +123,11 @@ instance Monad (SParse a) where return x = SParse (succeed x)- (SParse pr) >>= f + (SParse pr) >>= f = SParse (\st -> concat [ sparse (f a) rest | (a,rest) <- pr st ])++instance MonadFail (SParse a) where+ fail s = SParse none instance Applicative (SParse a) where pure = return
Craft3e.cabal view
@@ -1,6 +1,6 @@ name: Craft3e-version: 0.2.0.2+version: 0.2.0.3 license: MIT license-file: LICENSE copyright: (c) Addison Wesley@@ -46,6 +46,7 @@ open-browser >= 0.1.0.0 && < 0.5.0.0 exposed-modules:+ Chapter1 Chapter10 Chapter11@@ -113,9 +114,6 @@ RandomGen ServerState TopLevelServe -- other-modules:- hs-source-dirs: . ./Calculator ./Chapter15 ./Chapter16 ./Chapter19 ./Simulation ./Chapter20
ParseLib.hs view
@@ -123,8 +123,11 @@ instance Monad (SParse a) where return x = SParse (succeed x)- (SParse pr) >>= f + (SParse pr) >>= f = SParse (\st -> concat [ sparse (f a) rest | (a,rest) <- pr st ])++instance MonadFail (SParse a) where+ fail s = SParse none instance Applicative (SParse a) where pure = return
Pictures.hs view
@@ -62,6 +62,7 @@ printPicture = putStr . concat . map (++"\n") +render = printPicture -- Transformations of pictures. -- ^^^^^^^^^^^^^^^^^^^^^^^^^^^^@@ -70,7 +71,7 @@ flipV :: Picture -> Picture -flipV = map reverse+flipV pic = map reverse pic -- Reflection in a horizontal mirror. @@ -98,14 +99,14 @@ beside :: Picture -> Picture -> Picture -beside = zipWith (++)+beside pic1 pic2 = zipWith (++) pic1 pic2 -- Superimose one picture above another. Assume the pictures to be the same -- size. The individual characters are combined using the combine function. superimpose :: Picture -> Picture -> Picture -superimpose = zipWith (zipWith combine)+superimpose pic1 pic2 = zipWith (zipWith combine) pic1 pic2 -- For the result to be '.' both components have to the '.'; otherwise -- get the '#' character.@@ -121,7 +122,7 @@ invertColour :: Picture -> Picture -invertColour = map (map invert)+invertColour pic = map (map invert) pic -- ... which works by making the result '.' unless the input is '.'.
Test.hs view
@@ -16,3 +16,63 @@ pic2 :: Picture pic2 = pic1 `above` invert pic1++howManyEqual :: Integer -> Integer -> Integer -> Integer++howManyEqual x y z + | x==y && y==z = 3+ | x==y || y==z || z==x = 2+ | otherwise = 0+++(^^^) :: Integer -> Integer -> Integer+x ^^^ y + | x>= y = x+ | otherwise = y++fac :: Integer -> Integer++fac 0 = 1+fac n + | n>0 = n * fac (n-1)+ | otherwise = 0++++maxThreeOccurs :: Integer -> Integer -> Integer -> (Integer,Integer)++maxThreeOccurs x y z =+ (theMax,occurs)+ where+ theMax = max (max x y) z+ occurs = eq x + eq y + eq z+ eq w = if w==theMax then 1 else 0++pow :: Integer -> Integer++pow n + | n==0 = 1+ | n>0 = 2 * pow (n-1) + | otherwise = 0 ++sumFun :: (Integer -> Integer) -> Integer -> Integer+ +sumFun f n + | n==0 = f 0+ | n>0 = sumFun f (n-1) + f n+ | otherwise = 0 +++fibP :: Integer -> (Integer,Integer)++fibP 0 = (0,1)+fibP n = (v,u+v)+ where+ (u,v) = fibP (n-1)+++fib :: Integer -> Integer++fib 0 = 0+fib 1 = 1+fib n = fib (n-2) + fib (n-1)
svgOut.xml view
@@ -9,9 +9,5 @@ <image x="150" y="0" width="150" height="200" xlink:href="blk_horse_head.jpg" transform="translate(450,0) scale(-1,1)" filter="url(#negative)"/> - <image x="0" y="200" width="150" height="200" xlink:href="blk_horse_head.jpg" filter="url(#negative)"/>-- <image x="150" y="200" width="150" height="200" xlink:href="blk_horse_head.jpg" transform="translate(450,0) scale(-1,1)" />- </svg>