3
votes

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?

1
(1) Nitpicking: in is not a standalone keyword; it is always accompanied by let: let {- definitions -} in {- expression that uses the definitions -}. (2) In your case, the let-expression defines n and ns, in terms of foo, ys and a.duplode
@duplode That's not nitpicking, that's the answer. You should post it as such.Sebastian Redl
@SebastianRedl I can't write a full answer in a satisfactory way right now. I will do it later if other answers do not cover it.duplode

1 Answers

9
votes

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 lets:

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.