
On 12/22/05, Daniel Carrera
S Koray Can wrote:
As a newbie... I agree that a newbie should be able to write this fairly early on:
main = do x <- getLine() putStrLn ("The answer is " ++ show(fib(read(x))))
I'd agree for some definition of 'early'. I'll elaborate: [snip]
The above code snippet contains typeclasses (show, read, monadic IO, lists), syntactic sugar (do, <-). When you say a 'newbie' should be able to write that early on, I'd interpret that as 'a newbie should be able to regurgitate this early on'
Well, I'm a newbie, and I wrote it. I have "enough" understanding to generate that code, even if I don't understand it all. This is what I know:
* x is a string, fib wants an int, and "read" turns a string into a number. * "The answer is " is a string so you need ++. ++ expects a string, and "show" turns a number into a string.
Actually, it's a bit more than that (but still not harder than a newbie would be able to grasp in the first chapter). 'read' convertes a string into *any* readable value. So 'read "(4,1.23,'c')"' would convert a string into type '(Integer,Double,Char)'. Likewise 'show' converts any showable value to a string. This include numbers, but also includes a host of other values. /S -- Sebastian Sylvan +46(0)736-818655 UIN: 44640862