0

The traditional (Scheme, Pascal) way to structure code is this:

declare outer function
  declare inner function
    body of inner function
  body of outer function

The where clause in Haskell moves the inner function from the beginning to the end.

declare outer function
  body outer function
  where
    declare inner function
      body of inner function

I would say the tradional way is bottom-up: the source code begins with the code executed last. While the second way is top-down: the source begins with the code executed first.

Are the two ways to write code equivalent and just a matter of taste, or are there (from the software engineering point of view) differences, which are significant?

5
  • 1
    A Haskell function is one expression, so the first form isn't valid Haskell, and iirc "executed first" isn't meaningful either
    – Caleth
    Commented Dec 9, 2022 at 9:05
  • Declaration before use is required if you have a single pass compiler
    – Caleth
    Commented Dec 9, 2022 at 9:07
  • @Caleth: yes, the first form is not how Haskell programs are structured - exactly what the OP wrote. And your second comment looks like a good start for an answer.
    – Doc Brown
    Commented Dec 9, 2022 at 10:34
  • 4
    You can do either - let..in for subfunctions first, where for subfunctions last. Commented Dec 9, 2022 at 10:44
  • 3
    @SebastianRedl found it: wiki.haskell.org/Let_vs._Where
    – ceving
    Commented Dec 9, 2022 at 11:01

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.