[Haskell-beginners] Typing/N00b question/My first haskell.
Magnus Therning
magnus at therning.org
Mon Nov 21 11:59:00 CET 2011
On Mon, Nov 21, 2011 at 11:38, bryan hunt <sentimental.bryan at gmail.com> wrote:
>
>
> 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 at therning.org jabber: magnus at therning.org
twitter: magthe http://therning.org/magnus
More information about the Beginners
mailing list