RE: Beginner: error when using multiple where stmts in hugs98

| I've been trying some examples in functional programming. Most things | work fine, but I have trouble with expressions with 'where' clauses | that define more then one local definition. | ... | For example: | ... | mydiff f = f' | where f' x = ( f (x+h) - f x) / h | h = 0.0001 The second line in the where clause (your attempt to define local variable h) will be treated as if it were part of the first definition (for the variable f'). Why? Because that's what the layout/indentation in your code specifies. In fact, to a Haskell compiler, your input looks a lot like the following, clearly incorrect definition: mydiff f = f' where f' x = ( f (x+h) - f x) / h h = 0.0001 If you want h to be defined as a local variable at the same level as f', then you should make sure the two definitions start with the same indentation: mydiff f = f' where f' x = ( f (x+h) - f x) / h h = 0.0001 The indentation you've actually used suggests that you might have intended the definition of h to be local to f'. In that case, you should add an extra "where" keyword: mydiff f = f' where f' x = ( f (x+h) - f x) / h where h = 0.0001 All the best, Mark

Mark and Win-Jan
| I've been trying some examples in functional programming. Most things | work fine, but I have trouble with expressions with 'where' clauses | that define more then one local definition. | ... | For example: | ... | mydiff f = f' | where f' x = ( f (x+h) - f x) / h | h = 0.0001
The second line in the where clause (your attempt to define local variable h) will be treated as if it were part of the first definition (for the variable f'). Why? Because that's what the layout/indentation in your code specifies. In fact, to a Haskell compiler, your input looks a lot like the following, clearly incorrect definition:
mydiff f = f' where f' x = ( f (x+h) - f x) / h h = 0.0001
this explanation is very good but the real confusion comes from the fact that probably there were some TAB characters in the original program. In the text editor used for typing the program the tabs stops were 4 spaces apart as they are usually nowadays because otherwise programs become too wide... But the Haskell report (section B.3) specifies that the TAB characters are 8 characters apart. This is a small point indeed compared to existential polymorphic typing problems, but as author the Haskell Emacs mode, I have been asked quite a few times about the explanation of this kind of behavior. Concrete suggestions about this any one? Guy Lapalme PS: of course, one can specify that tabs insert spaces but then one must be cautious to untabify existing programs.

On Fri, 1 Dec 2000, Guy Lapalme wrote:
this explanation is very good but the real confusion comes from the fact that probably there were some TAB characters in the original program. In the text editor used for typing the program the tabs stops were 4 spaces apart as they are usually nowadays because otherwise programs become too wide...
[...]
Concrete suggestions about this any one?
Nothing which has to with Haskell, but: I think it's a bad idea to expand tabstops to something other then the "standard" (historic?) positions, i.e. 8 spaces apart. Nearly every editor can handle indentation independent of the TAB character, for example indentation in vi (^T) uses the parameter "shiftwidth" for indentation. Then, *leading* spaces are compressed to tabstops, if appropriate. For example, this line has been indented three times with ^T, where shiftwidth is 4. The line should start with a tabstop, followed by 4 spaces. Regarding to the layout rule, many people think that it's a horror from a sound syntax lover's point of view. Some years ago, when I wrote a Haskell parser for fun, I shared this opinion. However, although the layout rule is a nightmare to implement, it makes also Haskell programs not only easy to read, but sometimes to look even beautyful (wrt to programs in other languages). Bye, Kili ps: apropos beauty of programs: does anyone here ever heared of the language called "J"? -- Nunja! Wenn man erst einmal anfängt zu denken, dann ist es wie eine Sucht. Man kommt nicht mehr los davon. [WoKo in dag°, 28.11.2000]
participants (3)
-
Guy Lapalme
-
Mark P Jones
-
Matthias Kilian