Reputation: 181
I am having trouble understanding the full scope of the 'in' keyword in haskell. It was my understanding that you could use it to pass arguments to a function. However, I am having trouble understanding how the applies to a function similar to that shown below:
foo xs a =
case xs of
[] -> (a,[])
y:ys ->
let (n,ns)=foo ys a in
if y>0 then (1+n,y:ns)
else (n,ns)
How does 'in' apply to the equation if it is supplying parameters that foo does not take?
Upvotes: 11
Views: 10171
Reputation: 1467
in
goes along with let
to name one or more local expressions in a pure function.
So, to use a simpler example,
foo =
let greeting = "hello" in
print (greeting ++ " world")
would print "hello world".
But you could also use lots of let
s:
foo =
let greeting = "hello"
greetee = "world" in
print (greeting ++ " " ++ greetee)
And you just need one in
before you use the names you just defined.
And these names definitely don't have to be used as parameters to a function -- they can be just about anything, even functions themselves!
foo =
let f = print in
f "hello world"
In your example, the let
expression is defining two names, n
and ns
, which are used in the body of the function.
Upvotes: 16