Very Quick Question On "where" vs. "letrec", "letfn", "labels", etc.

This is (I believe) a simple, trivial question. Is there any difference between Haskell style "where" clauses on function definitions and using similar "letrec", "letfn" or "labels" forms in other languages. We can restrict the definitions within these forms to plain function definitions, for the sake of argument.

So where we might have (forgive any syntactic faux pas).

list-len Nil = 0
list-len _:xs = list-len' 1 xs
    list-len' acc Nil = acc
    list-len' acc _:xs = list-len' (acc + 1) xs

Can we always substitute an equivalent (using a hypothetical labels form, for the sake of argument):

list-len xs =
      list-len' acc Nil = acc
      list-len' acc _:ys = list-len' (acc + 1) ys
      case xs of
        Nil = 0
        _::zs = list-len' 1 zs

I'm just curious if these "where" clauses are naught more than syntactic surgar for other forms more familiar to me. Thanks much in advance, and no, this is *not* a homework assignment (I'm 45 years old today).

- Scott.

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.

'where' is not an expression

"A where clause is only allowed at the top level of a set of equations or case expression. The same properties and constraints on bindings in let expressions apply to those in where clauses." [1]

The multi-line function definitions are syntactic sugar for case expressions, in Haskell. A where clause could essentially be reduced to a 'let' binding for a case expression, though I believe there are a few differences in how you can annotate types.

'let' in Haskell is equivalent to 'letrec' in various other languages. and conveniently allows functions... i.e. I use 'let f x = ... in (do something possibly using f)' on a regular basis.