Differences

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

Link to this comparison view

Both sides previous revision Previous revision
Next revision
Previous revision
pp:l04x [2021/04/05 11:14]
dmihai
pp:l04x [2021/04/05 14:51] (current)
alexandru.poboranu add a Context in the last 2 ex
Line 25: Line 25:
 </​code>​ </​code>​
  
-3. Write a function ''​insertSort''​ that makes use of ''​insert''​ to implement [[|https://​en.wikipedia.org/​wiki/​Insertion_sort|insertion sort]] on a list. +3. Write a function ''​insertSort''​ that makes use of ''​insert''​ to implement [[https://​en.wikipedia.org/​wiki/​Insertion_sort|insertion sort]] on a list. 
  
 We now have a sorting function. However, it is unsatisfactory,​ because it can only sort numbers in an ascending order. We cannot specify any other criteria (e.g. descending order, by number of prime factors, by number of digits). We will expand the functionality by employing the ''​Ordering''​ data type and an auxiliary "​comparator function"​ which takes two integers and returns an ''​Ordering''​. We now have a sorting function. However, it is unsatisfactory,​ because it can only sort numbers in an ascending order. We cannot specify any other criteria (e.g. descending order, by number of prime factors, by number of digits). We will expand the functionality by employing the ''​Ordering''​ data type and an auxiliary "​comparator function"​ which takes two integers and returns an ''​Ordering''​.
Line 44: Line 44:
  
 <code haskell> <code haskell>
 +pointToPair :: Point -> (Float, Float)
 pointToPair (Point x y) = (x, y) pointToPair (Point x y) = (x, y)
 </​code>​ </​code>​
Line 52: Line 53:
  
 <note tip> <note tip>
-If you ask ''​ghci''​ to print a ''​Point''​ (e.g. by simply typing ''​Point 2.0 3.1''​ at the prompt) it will throw an error, as it doesn'​t know how to that. You can use the following definition to obtain a "​default"​ representation.+If you ask ''​ghci''​ to print a ''​Point''​ (e.g. by simply typing ''​Point 2.0 3.1''​ at the prompt) it will throw an error, as it doesn'​t know how to do that. You can use the following definition to obtain a "​default"​ representation.
  
 <code haskell> <code haskell>
Line 67: Line 68:
 </​code>​ </​code>​
  
-Here, we say that a natural number is either the number zero, or the successor of another natural number (see [Peano axioms|https://​en.wikipedia.org/​wiki/​Peano_axioms]]). The number three would be ''​Succ (Succ (Succ Zero)))''​.+Here, we say that a natural number is either the number zero, or the successor of another natural number (see [[https://​en.wikipedia.org/​wiki/​Peano_axioms|Peano axioms]]). The number three would be ''​Succ (Succ (Succ Zero)))''​.
  
 8. Write a function ''​add''​ which takes two ''​Natural''​ numbers and returns their sum (remember that you can do pattern matching to check the structure of a number). 8. Write a function ''​add''​ which takes two ''​Natural''​ numbers and returns their sum (remember that you can do pattern matching to check the structure of a number).
Line 93: Line 94:
 </​code>​ </​code>​
  
-In the above definition, we have mostly replaced ''​Int''​ with ''​a''​. ''​a''​ is a **type variable**; it can stand for any type (what makes it a type variable is that it starts with a lowercase letter; it could as well be ''​b''​ or ''​something''​). We can now construct lists of any type. However, remember that all elements in a list must be of the //same// type; we can't combine integers and strings. That is why ''​a''​ also follows the data type name, on the left hand side of the ''​='': ​to ensure that all ''​a''​s ​on the right hand side refer to the **same** type.+In the above definition, we have mostly replaced ''​Int''​ with ''​a''​. ''​a''​ is a **type variable**; it can stand for any type (what makes it a type variable is that it starts with a lowercase letter; it could as well be ''​b''​ or ''​something''​). We can now construct lists of any type. However, remember that all elements in a list must be of the //same// type; we can't combine integers and strings. That is why ''​a''​ also follows the data type name, on the left hand side of the ''​='': ​if we have a ''​List''​ which contains ''​Int''​s, then that is a ''​List Int''​.
  
 10. Write a function ''​myLength''​ which takes a ''​List a''​ and returns its length. Notice how the type of the lists elements doesn'​t matter (and remember pattern matching!). 10. Write a function ''​myLength''​ which takes a ''​List a''​ and returns its length. Notice how the type of the lists elements doesn'​t matter (and remember pattern matching!).
Line 152: Line 153:
 21. Write a function ''​search''​ which takes a ''​Context''​ and a variable name and returns the value from that context (it is guaranteed, that exactly one value exists for each variable on which the function is called; you don't need to account for any edge-case). 21. Write a function ''​search''​ which takes a ''​Context''​ and a variable name and returns the value from that context (it is guaranteed, that exactly one value exists for each variable on which the function is called; you don't need to account for any edge-case).
  
-22. Write a function ''​evalA''​ which takes an ''​AExpr''​ and returns a single integer representing its evaluated value.+22. Write a function ''​evalA''​ which takes a ''​Context''​ and an ''​AExpr''​ and returns a single integer representing its evaluated value.
  
-23. Write a function ''​evalB''​ which takes a ''​BExpr''​ and returns a single boolean representing its evaluated value.+23. Write a function ''​evalB''​ which takes a ''​Context''​ and a ''​BExpr''​ and returns a single boolean representing its evaluated value.