[Haskell-cafe] Question about data
I've created a simple type declaration: data MathExpression = Float | Add MathExpression MathExpression | Subtract MathExpression MathExpression | Multiply MathExpression MathExpression | Divide MathExpression MathExpression deriving (Show) Now how do I create an instance of MathExpression which is just a Float? This doesn't work: *Main let pi = 3.14 :: MathExpression interactive:1:10: No instance for (Fractional MathExpression) arising from the literal `3.14' Possible fix: add an instance declaration for (Fractional MathExpression) In the expression: 3.14 :: MathExpression In an equation for `pi': pi = 3.14 :: MathExpression ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
[Haskell-cafe] Couldn't match expected type `Integer' against inferred type `Int'
Why am I getting this error: Couldn't match expected type `Integer' against inferred type `Int' In the expression: foldl step 0 xs In the definition of `asInt_foldAux': asInt_foldAux xs = foldl step 0 xs where step acc '.' = error cannot handle decimal numbers step acc x = acc * 10 + digitToInt x for this code? import Data.Char (digitToInt) asInt_fold :: String - Integer asInt_fold ('-':xs) = -(asInt_foldAux xs) asInt_fold xs = asInt_foldAux xs asInt_foldAux :: String - Integer asInt_foldAux xs = foldl step 0 xs where step acc '.' = error cannot handle decimal numbers step acc x = acc * 10 + digitToInt x Note that I'm using Int, rather than Integer, to avoid silent overflow errors. ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
Re: [Haskell-cafe] Couldn't match expected type `Integer' against inferred type `Int'
On Aug 8, 2011, at 7:59 PM, Ivan Lazar Miljenovic wrote: On 9 August 2011 10:49, Paul Reiners paul.rein...@gmail.com wrote: Why am I getting this error: Couldn't match expected type `Integer' against inferred type `Int' In the expression: foldl step 0 xs In the definition of `asInt_foldAux': asInt_foldAux xs = foldl step 0 xs where step acc '.' = error cannot handle decimal numbers step acc x = acc * 10 + digitToInt x for this code? import Data.Char (digitToInt) asInt_fold :: String - Integer asInt_fold ('-':xs) = -(asInt_foldAux xs) asInt_fold xs = asInt_foldAux xs asInt_foldAux :: String - Integer asInt_foldAux xs = foldl step 0 xs where step acc '.' = error cannot handle decimal numbers step acc x = acc * 10 + digitToInt x digitToInt returns an Int; as such the result of asInt_foldAux is an Int, but you've specified in its type signature that it should be returning an Integer. So is there something like digitToInteger? Or can I somehow cast the Int to an Integer? Note that I'm using Int, rather than Integer, to avoid silent overflow errors. Should that be the other way round? Yes, it should be the other way round. ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
[Haskell-cafe] Binding of an integer variable
I have a question about the following GHCi interaction: Prelude let x = 23 Prelude :show bindings x :: Integer = _ What is the meaning of the underscore in the third line? Why doesn't it say this, instead? x :: Integer = 23 ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
[Haskell-cafe] Input redirection in WinGHCi
I know I can do the following from a command prompt: $ runghc WC quux.txt How do I do this in WinGHCi? I know I have to first load the file like this: Prelude :load WC But then what? This doesn't work: *Main WC quux.txt interactive:1:1: Not in scope: data constructor `WC' interactive:1:6: Not in scope: `quux' interactive:1:11: Not in scope: `txt' ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe