I am confused about when Haskell evaluates functions, compared to when it just returns the function itself. I was taught that pattern matching drives function evaluation, but then I don't understand why
f :: Int -> Int
f x = x+1
works. Does f add 1 to an integer, or does it return a function which adds 1 to an integer? Are these two the same thing? There is no pattern matching as far as I can tell, so I'm not sure why it gets evaluated.
Another question: suppose I want to make an 8x8 list that contains all 0's, except the first row contains the numbers 1,2,3,4,5,6,7,8 instead. Is there any way I could initialize it to all 0's first and then change the first row to [1..8]? I understand that it's not idiomatic to make sequential code like this, so is there a better way to do it, hopefully without using do
blocks?
Finally, I am also confused about the let
and where
syntax. Suppose that in the middle of a function definition, I say temp = x + 1
. How is this different from saying let temp = x + 1
or ...temp where temp = x + 1
? In each of these cases, does temp
have type Int
or Int -> Int
? Why do people use do
with let
so often?