[Haskell-beginners] Cannot compile "where" in a function

Trung Quang Nguyen trungnq97 at gmail.com
Sun Dec 16 18:08:20 CET 2012


Hi there,

I'm trying to implement Reverse Polish notation calculator from
learnyouhaskell.com. I want to add one more operator "/", but I cannot
compile. I comment out the line --func (x:y:ys) "/" = (y / x):ys, thing
goes well again. Any body knows the cause of this problem?

--rpn.hs
import System.Environment
import Data.List

solveRPN :: (Num a, Read a) => String -> a
solveRPN = head . foldl func [] . words
    where
        func (x:y:ys) "+" = (y + x):ys
        func (x:y:ys) "-" = (y - x):ys
        func (x:y:ys) "*" = (y * x):ys
        --func (x:y:ys) "/" = (y / x):ys
        func xs numStr = read numStr:xs

main = do
    (x:_) <- getArgs
    putStrLn $ show $ solveRPN x

I got this:
~/w/r/s/haskell> ghc --make rpn
[1 of 1] Compiling Main             ( rpn.hs, rpn.o )

rpn.hs:5:25:
    Could not deduce (Fractional a) arising from a use of `func'
    from the context (Num a, Read a)
      bound by the type signature for
                 solveRPN :: (Num a, Read a) => String -> a
      at rpn.hs:(5,1)-(11,39)
    Possible fix:
      add (Fractional a) to the context of
        the type signature for solveRPN :: (Num a, Read a) => String -> a
    In the first argument of `foldl', namely `func'
    In the first argument of `(.)', namely `foldl func []'
    In the second argument of `(.)', namely `foldl func [] . words'

Thanks in advance!:)
/Trung
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.haskell.org/pipermail/beginners/attachments/20121216/c667b24d/attachment.htm>


More information about the Beginners mailing list