Declaration vs. expression style
From HaskellWiki
(Difference between revisions)
(taken table from "History of Haskell") 
(Add link to Let vs. Where and History of Haskell pages) 

(3 intermediate revisions by 2 users not shown)  
Line 2:  Line 2:  
which are both supported by Haskell mainly because several language designers preferred these different styles. 
which are both supported by Haskell mainly because several language designers preferred these different styles. 

+  In the '''declaration style''' you formulate an algorithm in terms of several equations that shall be satisfied.<br> 

+  In the '''expression style''' you compose big expressions from small expressions. 

+  
+  == Comparison == 

+  
+  As illustration for the two styles, Simon Peyton Jones give two implementations of the Prelude function <hask>filter</hask>: 

+  <haskell> 

+  filter :: (a > Bool) > [a] > [a] 

+  </haskell> 

+  
+  === Declaration style === 

+  
+  <haskell> 

+  filter p [] = [] 

+  filter p (x:xs) 

+   p x = x : rest 

+   otherwise = rest 

+  where 

+  rest = filter p xs 

+  </haskell> 

+  
+  === Expression style === 

+  
+  <haskell> 

+  filter = 

+  \p > \ xs > 

+  case xs of 

+  [] > [] 

+  (x:xs) > 

+  let rest = filter p xs 

+  in if p x 

+  then x : rest 

+  else rest 

+  </haskell> 

+  
+  == Syntactic elements == 

There are characteristic elements of both styles. 
There are characteristic elements of both styles. 

−  { 
+  { cellpadding="4" cellspacing="0" border="3" style="color:blue" 
−   Declaration style   Expressionstyle  
+  ! style="color:black" colspan="2"Declaration style 
−   
+  ! style="color:black" colspan="2"Expressionstyle 
−   <hask>where</hask> clause   <hask>let</hask> expression  
+   style="color:black" 
−   
+   colspan="2" <hask>where</hask> clause 
+   colspan="2" <hask>let</hask> expression 

+   style="color:black" 

 Function arguments on left hand side:  <hask>f x = x*x</hask>  [[Lambda abstraction]]:  <hask>f = \x > x*x</hask> 
 Function arguments on left hand side:  <hask>f x = x*x</hask>  [[Lambda abstraction]]:  <hask>f = \x > x*x</hask> 

−   
+   style="color:black" 
 [[Pattern matching]] in function definitions:  <hask>f [] = 0</hask>  <hask>case</hask> expression:  <hask>f xs = case xs of [] > 0</hask> 
 [[Pattern matching]] in function definitions:  <hask>f [] = 0</hask>  <hask>case</hask> expression:  <hask>f xs = case xs of [] > 0</hask> 

−   
+   style="color:black" 
 [[Guard]]s on function definitions:  <hask>f [x]  x>0 = 'a'</hask>  <hask>if</hask> expression:  <hask>f [x] = if x>0 then 'a' else ...</hask> 
 [[Guard]]s on function definitions:  <hask>f [x]  x>0 = 'a'</hask>  <hask>if</hask> expression:  <hask>f [x] = if x>0 then 'a' else ...</hask> 

} 
} 

== See also == 
== See also == 

−  +  * [[Let vs. Where]] 

−  * http://research.microsoft.com/~simonpj/papers/historyofhaskell/ 
+  * [[History of Haskell]] (in section 4.4) 
[[Category:Style]] 
[[Category:Style]] 
Latest revision as of 00:41, 23 August 2008
There are two main styles of writing functional programs, which are both supported by Haskell mainly because several language designers preferred these different styles.
In the declaration style you formulate an algorithm in terms of several equations that shall be satisfied.
In the expression style you compose big expressions from small expressions.
Contents 
[edit] 1 Comparison
As illustration for the two styles, Simon Peyton Jones give two implementations of the Prelude functionfilter
filter :: (a > Bool) > [a] > [a]
[edit] 1.1 Declaration style
filter p [] = [] filter p (x:xs)  p x = x : rest  otherwise = rest where rest = filter p xs
[edit] 1.2 Expression style
filter = \p > \ xs > case xs of [] > [] (x:xs) > let rest = filter p xs in if p x then x : rest else rest
[edit] 2 Syntactic elements
There are characteristic elements of both styles.
Declaration style  Expressionstyle  

where 
let  
Function arguments on left hand side:  f x = x*x 
Lambda abstraction:  f = \x > x*x 
Pattern matching in function definitions:  f [] = 0 
case 
f xs = case xs of [] > 0 
Guards on function definitions:  f [x]  x>0 = 'a' 
if 
f [x] = if x>0 then 'a' else ... 
[edit] 3 See also
 Let vs. Where
 History of Haskell (in section 4.4)