Re: [Haskell-cafe] Example code won't compile

2009-05-15 Thread michael rice
OK. Thanks, everyone.

Michael

--- On Fri, 5/15/09, Krzysztof Skrzętnicki  wrote:

From: Krzysztof Skrzętnicki 
Subject: Re: [Haskell-cafe] Example code won't compile
To: "michael rice" 
Cc: "Lennart Augustsson" , 
"haskell-cafe@haskell.org" 
Date: Friday, May 15, 2009, 9:55 PM

The one important line is
> import Control.Monad.Error
It adds dependancy on mtl package, which is not used by default
(contrary to 'base' package, which includes Monad, System and IO
modules). With --make GHC adds it automatically. Therefore
$ ghc -package mtl ex14.hs
compiles fine. I'd recommend using --make. In rare occasions when
there is a namespace clash between packages one can simply hide
offending packages or specify preferred ones.

Best regards

Christopher Skrzętnicki



  ___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] Example code won't compile

2009-05-15 Thread Krzysztof Skrzętnicki
The one important line is
> import Control.Monad.Error
It adds dependancy on mtl package, which is not used by default
(contrary to 'base' package, which includes Monad, System and IO
modules). With --make GHC adds it automatically. Therefore
$ ghc -package mtl ex14.hs
compiles fine. I'd recommend using --make. In rare occasions when
there is a namespace clash between packages one can simply hide
offending packages or specify preferred ones.

Best regards

Christopher Skrzętnicki
___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] Example code won't compile

2009-05-15 Thread michael rice
This example compiles fine without --make. What's the difference?

Michael

==

{- Author: Jeff Newbern
   Maintainer: Jeff Newbern 
   Time-stamp: 
   License:    GPL
-}

{- DESCRIPTION

Example 13 - Using the List monad

Usage: Compile the code and execute the resulting program
   with various arguments.  Each argument will produce
   a list of possible parses of the argument as a decimal
   number, a hexadecimal number or a word.

Try: ./ex13 34 f3 bean
 ./ex13 food f00d
 ./ex13 beef 10 "n!"
 ./ex13 "why?" "(punctuation)"

-}

import Monad
import System
import Char

-- we can parse three different types of terms
data Parsed = Digit Integer | Hex Integer | Word String deriving Show

-- attempts to add a character to the parsed representation of a hex digit
parseHexDigit :: Parsed -> Char -> [Parsed]
parseHexDigit (Hex n) c = if isHexDigit c then
    return (Hex ((n*16) + (toInteger (digitToInt c
          else
    mzero
parseHexDigit _   _ = mzero

-- attempts to add a character to the parsed representation of a decimal digit
parseDigit :: Parsed -> Char -> [Parsed]
parseDigit (Digit n) c = if isDigit c then
   return (Digit ((n*10) + (toInteger (digitToInt c
 else
   mzero
parseDigit _ _ = mzero
           
-- attempts to add a character to the parsed representation of a word
parseWord :: Parsed -> Char -> [Parsed]
parseWord (Word s) c = if isAlpha c then
 return (Word (s ++ [c]))
   else
 mzero
parseWord _    _ = mzero

-- tries to parse the digit as a hex value, a decimal value and a word
-- the result is a list of possible parses
parse :: Parsed -> Char -> [Parsed]
parse p c = (parseHexDigit p c) `mplus` (parseDigit p c) `mplus` (parseWord p c)

-- parse an entire String and return a list of the possible parsed values
parseArg :: String -> [Parsed]
parseArg s = do init <- (return (Hex 0)) `mplus` (return (Digit 0)) `mplus` 
(return (Word ""))
    foldM parse init s

-- show the original string and all possible parses for the string
showResult :: String -> IO ()
showResult s = do putStr s
  putStr ": "
  print (parseArg s)


-- prints possible parsed values for command-line arguments
main :: IO ()
main = do args <- getArgs
  mapM_ showResult args

-- END OF FILE


--- On Fri, 5/15/09, Lennart Augustsson  wrote:

From: Lennart Augustsson 
Subject: Re: [Haskell-cafe] Example code won't compile
To: "michael rice" 
Cc: "haskell-cafe@haskell.org" 
Date: Friday, May 15, 2009, 7:25 PM

--make

   -- Lennart (iPhone)




  ___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] Example code won't compile

2009-05-15 Thread Don Stewart
nowgate:
> Why won't this code compile?
> [mich...@localhost ~]$ ghc ex14.hs -o ex14

Missing --make

I'd also add -O2, but I'm like that.

-- Don
___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


[Haskell-cafe] Example code won't compile

2009-05-15 Thread michael rice
Why won't this code compile?

Michael

===

{- Author: Jeff Newbern
   Maintainer: Jeff Newbern 
   Time-stamp: 
   License:    GPL
-}

{- DESCRIPTION

Example 14 - Using the IO monad

Usage: Compile the code to produce a poor replacement for
   the standard Unix "tr" command.

Try: cat file | ./ex14 "aeiou" "X"
 cat file | ./ex14 " " "_"
 ./ex14 "abc"
-}

import Monad
import System
import IO
import Control.Monad.Error

-- translate char in set1 to corresponding char in set2
translate :: String -> String -> Char -> Char
translate [] _  c = c
translate (x:xs) [] c = if x == c then ' ' else translate xs []  c
translate (x:xs) [y]    c = if x == c then  y  else translate xs [y] c
translate (x:xs) (y:ys) c = if x == c then  y  else translate xs ys  c

-- translate an entire string
translateString :: String -> String -> String -> String
translateString set1 set2 str = map (translate set1 set2) str

usage :: IOError -> IO ()
usage e = do putStrLn "Usage: ex14 set1 set2"
 putStrLn "Translates characters in set1 on stdin to the 
corresponding"
 putStrLn "characters from set2 and writes the translation to 
stdout."

-- translates stdin to stdout based on commandline arguments
main :: IO ()
main = (do [set1,set2] <- getArgs
   contents    <- hGetContents stdin
   putStr $ translateString set1 set2 contents)
   `catchError` usage

-- END OF FILE

=

[mich...@localhost ~]$ ghc ex14.hs -o ex14
ex14.o: In function `rF8_info':
(.text+0x48): undefined reference to 
`mtlzm1zi1zi0zi2_ControlziMonadziError_zdf17_closure'
ex14.o: In function `sGp_info':
(.text+0x861): undefined reference to 
`mtlzm1zi1zi0zi2_ControlziMonadziError_zdf17_closure'
ex14.o: In function `sGp_info':
(.text+0x91d): undefined reference to 
`__stginit_mtlzm1zi1zi0zi2_ControlziMonadziError_'
ex14.o: In function `rF8_info':
(.text+0x50): undefined reference to 
`mtlzm1zi1zi0zi2_ControlziMonadziErrorziClass_zdp1MonadError_info'
ex14.o: In function `sGp_info':
(.text+0x869): undefined reference to 
`mtlzm1zi1zi0zi2_ControlziMonadziErrorziClass_catchError_info'
ex14.o: In function `rF8_srt':
(.data+0x0): undefined reference to 
`mtlzm1zi1zi0zi2_ControlziMonadziError_zdf17_closure'
ex14.o: In function `Main_main_srt':
(.data+0x6c): undefined reference to 
`mtlzm1zi1zi0zi2_ControlziMonadziError_zdf17_closure'
collect2: ld returned 1 exit status
[mich...@localhost ~]$ 




  ___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe