<table cellspacing="0" cellpadding="0" border="0" ><tr><td valign="top" style="font: inherit;">This example compiles fine without --make. What's the difference?<br><br>Michael<br><br>==================<br><br>{- Author: Jeff Newbern<br> Maintainer: Jeff Newbern <jnewbern@nomaware.com><br> Time-stamp: <Wed Jul 2 18:11:36 2003><br> License: GPL<br>-}<br><br>{- DESCRIPTION<br><br>Example 13 - Using the List monad<br><br>Usage: Compile the code and execute the resulting program<br> with various arguments. Each argument will produce<br> a list of possible parses of the argument as a decimal<br> number, a hexadecimal number or a word.<br><br>Try: ./ex13 34 f3 bean<br> ./ex13 food f00d<br> ./ex13 beef 10
"n!"<br> ./ex13 "why?" "(punctuation)"<br><br>-}<br><br>import Monad<br>import System<br>import Char<br><br>-- we can parse three different types of terms<br>data Parsed = Digit Integer | Hex Integer | Word String deriving Show<br><br>-- attempts to add a character to the parsed representation of a hex digit<br>parseHexDigit :: Parsed -> Char -> [Parsed]<br>parseHexDigit (Hex n) c = if isHexDigit c then<br> return (Hex ((n*16) + (toInteger (digitToInt c))))<br> else<br> mzero<br>parseHexDigit
_ _ = mzero<br><br>-- attempts to add a character to the parsed representation of a decimal digit<br>parseDigit :: Parsed -> Char -> [Parsed]<br>parseDigit (Digit n) c = if isDigit c then<br> return (Digit ((n*10) + (toInteger (digitToInt c))))<br> else<br> mzero<br>parseDigit _ _ = mzero<br> <br>-- attempts to add a character to the parsed representation of a
word<br>parseWord :: Parsed -> Char -> [Parsed]<br>parseWord (Word s) c = if isAlpha c then<br> return (Word (s ++ [c]))<br> else<br> mzero<br>parseWord _ _ = mzero<br><br>-- tries to parse the digit as a hex value, a decimal value and a word<br>-- the result is a list of possible parses<br>parse :: Parsed -> Char -> [Parsed]<br>parse p c = (parseHexDigit p c) `mplus` (parseDigit p c) `mplus` (parseWord p c)<br><br>-- parse an entire String and return a list of the possible parsed
values<br>parseArg :: String -> [Parsed]<br>parseArg s = do init <- (return (Hex 0)) `mplus` (return (Digit 0)) `mplus` (return (Word ""))<br> foldM parse init s<br><br>-- show the original string and all possible parses for the string<br>showResult :: String -> IO ()<br>showResult s = do putStr s<br> putStr ": "<br> print (parseArg s)<br><br><br>-- prints possible parsed values for command-line arguments<br>main :: IO ()<br>main = do args <- getArgs<br> mapM_ showResult args<br><br>-- END OF FILE<br><br><br>--- On <b>Fri, 5/15/09, Lennart Augustsson <i><lennart.augustsson@gmail.com></i></b>
wrote:<br><blockquote style="border-left: 2px solid rgb(16, 16, 255); margin-left: 5px; padding-left: 5px;"><br>From: Lennart Augustsson <lennart.augustsson@gmail.com><br>Subject: Re: [Haskell-cafe] Example code won't compile<br>To: "michael rice" <nowgate@yahoo.com><br>Cc: "haskell-cafe@haskell.org" <haskell-cafe@haskell.org><br>Date: Friday, May 15, 2009, 7:25 PM<br><br><div id="yiv1295544675"><div>--make<br><br> -- Lennart (iPhone)</div><div><br><br></div></div></blockquote></td></tr></table><br>