
Hi I'm trying to write a simple program to get user input and execute a function based on the input. The program follows: import IO main = do putStrLn "1) f1" putStrLn "2) f2" choice <- getLine case choice of 1 -> do putStrLn "f1" 2 -> do putStrLn "f2" _ -> do putStrLn "" When compiled with ghc I get the following error: test.hs:8: No instance for (Num String) arising from the pattern `2' at test.hs:8 In a case alternative: 2 -> do putStrLn "f2" In the case expression: case choice of 1 -> do putStrLn "f1" 2 -> do putStrLn "f2" _ -> do putStrLn "" Please can someone explain. Thanks

sashan
When compiled with ghc I get the following error: test.hs:8: No instance for (Num String) arising from the pattern `2' at test.hs:8 In a case alternative: 2 -> do putStrLn "f2" In the case expression: case choice of 1 -> do putStrLn "f1" 2 -> do putStrLn "f2" _ -> do putStrLn ""
Please can someone explain.
The compiler already did: No instance for (Num String) In other words, a String is not a Num(ber), and you are treating it as one (the variable 'choice' in the case clause). -kzm -- If I haven't seen further, it is by standing in the footprints of giants

On Tue, 6 May 2003, sashan wrote: (snip)
choice <- getLine case choice of 1 -> do putStrLn "f1"
(snip) getLine returns a String, yet the 1 is your case statement is a number. Try, case (read choice) of instead. "read" complements "show": it is a handy function that turns strings into what you want. (The "Read" typeclass is relevant.) -- Mark
participants (3)
-
ketil@ii.uib.no
-
Mark Carroll
-
sashan