On 10/1/05, justebelmont@argentina.com <justebelmont@argentina.com> wrote:
Hi there folks, I'm trying to learn some functional programming in
Haskell,
well, at the College, but they don't teach anything, lol, so I have a very simple question about type creation.
This is a new type that contains the int and the new infinity number
data NatInf = Infinity | Num Int
At this point every seems to be ok, but when I load the file in Hugs,
and
then write
Num 5 (or Infinity)
the following error appears:
ERROR – Cannot find "show" function for: *** Expression : Num 5 *** Of type: : NatInf
This means that you have not yet specified how you want your new data type to be converted to a string (using the function "show" in the Show type class). Either you write your own implementation instance Show NatInf where show (Num x) = ... show (Infinity) = ... Or you can simply derive a standard one by typing "deriving Show" at the end of your data declaration. data NatInf = Infinity | Num Int deriving Show /S -- Sebastian Sylvan +46(0)736-818655 UIN: 44640862
Hi, I've read through the documentation on Alex abit, but since I'm not the sharpest tool in the shed I'm not really seeing the obvious way to take the output file of Alex and make a program that will print out the list of tokens scanned from an input file. I had to do something like this this past week for a class, but that was in Java so I wanted to try it in Haskell as well but I'm stuck at the moment.
Which wrapper are you using? I just recently did this for the monad wrapper. It looks a little like this: data Token = EOF | .... alexEOF = return EOF lexAll :: Alex [Token] lexAll = do x <- alexMonadScan case x of EOF -> [] _ -> lexAll >>= return . (:) x lexString :: String -> [Token] lexString str = runAlex str lexAll The basic and posn wrappers have the "alexScanTokens" function which does essentially the same thing. Just slap on hGetContents and show and you're done. Creighton Hogg wrote:
Hi, I've read through the documentation on Alex abit, but since I'm not the sharpest tool in the shed I'm not really seeing the obvious way to take the output file of Alex and make a program that will print out the list of tokens scanned from an input file. I had to do something like this this past week for a class, but that was in Java so I wanted to try it in Haskell as well but I'm stuck at the moment. _______________________________________________ Haskell mailing list Haskell@haskell.org http://www.haskell.org/mailman/listinfo/haskell
participants (4)
-
Creighton Hogg -
justebelmont@argentina.com -
robert dockins -
Sebastian Sylvan