0
votes

I'm new to Haskell, and I'm stuck with an example. I'd like to experiment with the Maybe type, but my code does not compile:

divide100By :: Int a -> Maybe a
divide100By x = case (Int x) of 
    Nothing -> Nothing
    Just x -> 100 / x

main = putStrLn ("Result: " ++ show (divide100By 5))

But I'm always getting the error:

Not in scope: data constructor 'Int'

I'm afraid I don't quite get the function definition in Haskell (the divide100By :: Int a -> Maybe a part)... Can someone please tell me what's wrong here?

1
Int does not take a type argument, so you want divide100By :: Int -> Maybe Int. Your function body also has at least 3 other problems... - Alec

1 Answers

4
votes

The integer type is Int, writing Int a means nothing since Int does not take arguments.

Also, Int x is not an expression, so you can't case on that.

Further, you use case with Nothing/Just x when you have a Maybe a type and you want to check what is its value. Here instead you want to construct a Maybe a type.

Try this instead:

divide100By :: Int -> Maybe Int
divide100By 0 = Nothing
divide100By x = Just (100 `div` x)

or

divide100By :: Int -> Maybe Int
divide100By y = case y of
   0 -> Nothing
   x -> Just (100 `div` x)