Typing/N00b question/My first haskell.

I'm trying to read in a value and convert it into a number for use in the ageJudge function. I've tried a number of different approaches but keep getting type conversion errors. This is literally my first Haskel program - so please don't laugh... module Main where ageJudge :: (Integral a) => a -> String ageJudge age | age >= 40 = "You're too old!" | age <= 30 = "Your too young" | otherwise = "Not in the programming age range.." main = do putStrLn "What is your name?" name <- getLine putStrLn ("Nice to meet you, " ++ name ++ "!") putStrLn "What is your age?" age <- getLine let inpIntegral = (read age)::Double -- let decission = ageJudge (read inpIntegral)::Integral putStrLn ("We decided!") -- putStrLn ("We decided:\n" ++ show(1) ++ "!") -- ageJudge inpIntegral

On Mon, 21 Nov 2011 11:38:03 +0100, bryan hunt
but keep getting type conversion errors.
module Main where
ageJudge :: (Integral a) => a -> String ageJudge age | age >= 40 = "You're too old!" | age <= 30 = "Your too young" | otherwise = "Not in the programming age range.."
main = do putStrLn "What is your name?" name <- getLine putStrLn ("Nice to meet you, " ++ name ++ "!") putStrLn "What is your age?" age <- getLine let inpIntegral = (read age)::Double -- let decission = ageJudge (read inpIntegral)::Integral putStrLn ("We decided!") -- putStrLn ("We decided:\n" ++ show(1) ++ "!") -- ageJudge inpIntegral
You use the type class Integral, which contains Int and Integer, for ageJudge; it is better to use Double, as you use this type for reading the age. Another option is, to use the function "round" to round the age. Note, that you cannot use Integral as a type. Regards, Henk-Jan van Tuyl -- http://Van.Tuyl.eu/ http://members.chello.nl/hjgtuyl/tourdemonad.html --

On Mon, Nov 21, 2011 at 11:38, bryan hunt
I'm trying to read in a value and convert it into a number for use in the ageJudge function. I've tried a number of different approaches but keep getting type conversion errors. This is literally my first Haskel program - so please don't laugh...
module Main where
ageJudge :: (Integral a) => a -> String
I'd just skip the type of 'ageJudge' and let the compiler choose itself; it then ends up being "ageJudge :: (Ord a, Num a) => a -> [Char]".
ageJudge age | age >= 40 = "You're too old!" | age <= 30 = "Your too young" | otherwise = "Not in the programming age range.."
main = do putStrLn "What is your name?" name <- getLine putStrLn ("Nice to meet you, " ++ name ++ "!") putStrLn "What is your age?" age <- getLine let inpIntegral = (read age)::Double
"Double" is not an "Integral", you most likely want to use "Int" or "Integer" here. But again, you can just drop the explicit type and let the compiler decide itself.
-- let decission = ageJudge (read inpIntegral)::Integral
"read" takes a String argument, so this line makes very little sense.
putStrLn ("We decided!") -- putStrLn ("We decided:\n" ++ show(1) ++ "!") -- ageJudge inpIntegral
/M -- Magnus Therning OpenPGP: 0xAB4DFBA4 email: magnus@therning.org jabber: magnus@therning.org twitter: magthe http://therning.org/magnus
participants (3)
-
bryan hunt
-
Henk-Jan van Tuyl
-
Magnus Therning