Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revision Previous revision
pp:syntax [2020/03/12 23:09]
pdmatei
pp:syntax [2020/03/12 23:18] (current)
pdmatei
Line 60: Line 60:
  
 Usually (although not mandatory), functions are defined in //prefix// form (as seen in the previous examples). Some Haskell functions are infix (e.g. ''​+'',​ ''​.''​ or ''​$''​). We can //turn// an infix function into a prefix one using parentheses. E.g. '':​t ($)''​ or ''​(+) 1 2''​. Similarly, we can //turn// an prefix function into an infix, using quasiquotes,​ e.g. ''​1 `f` 2''​ where ''​f x y = x + y''​. ​ Usually (although not mandatory), functions are defined in //prefix// form (as seen in the previous examples). Some Haskell functions are infix (e.g. ''​+'',​ ''​.''​ or ''​$''​). We can //turn// an infix function into a prefix one using parentheses. E.g. '':​t ($)''​ or ''​(+) 1 2''​. Similarly, we can //turn// an prefix function into an infix, using quasiquotes,​ e.g. ''​1 `f` 2''​ where ''​f x y = x + y''​. ​
 +
 +==== where ====
 +
 +Often we would like to define auxiliary functions whose scope (visibility) is limited to our function definition. We can do this using ''​where''​. We illustrate it on several examples:
 +
 +<code haskell>
 +f x y = (inc x) + y
 +          where inc v = v + 1
 +</​code>​
 +
 +<code haskell>
 +f x y = (inc x) + (dec y)
 +          where inc v = v + 1
 +                dec v = v - 1
 +</​code>​
 +
 +<code haskell>
 +f x y = (inc x) + (dec y)
 +          where inc v = (g v) + 1
 +                      where g 0 = 0
 +                            g _ = 1
 +                dec v = v - 1
 +</​code>​
 +
 +<code haskell>
 +f x y = (inc x) + (dec y)
 +          where inc v = (g v) + 1
 +                g 0 = 0
 +                g _ = 1
 +                dec v = v - 1
 +</​code>​
 +
 +The last two examples construct functions ''​f''​ with the same behaviour, however they are not identical. In the latter example, the function ''​g''​ is visible in the entire ''​where''​ body, thus other functions defined in the same scope could employ it.
  
 ===== Basic datatypes ====== ===== Basic datatypes ======