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:class [2021/04/11 02:09]
lfa
pp:class [2021/04/11 22:15] (current)
lfa
Line 26: Line 26:
  
 Now let's check one more interesting concept, Haskell'​s class system. Now let's check one more interesting concept, Haskell'​s class system.
-If //data// can be considered the //class// from Java, then ''​class''​ could be considered the //​interface//​ from Java. 
  
 If you're defining a new //data Point//, you probably encountered some problems trying to print it in GHCI. If you're defining a new //data Point//, you probably encountered some problems trying to print it in GHCI.
Line 47: Line 46:
 </​code>​ </​code>​
  
-1. Add List and Student to the Show class. You can print them however you want. +1. Add List and Student to the Show class. You can print them however you want. If you aren't inspired today, you can use the following:​ 
-   <​note tip>If you aren't inspired today, you can use the following:​ +   ​* The lists can be the default style -> //​[3,​4,​1,​2,​]//​ 
-    * The lists can be the default style -> //​[3,​4,​1,​2,​]//​. +   ​* The student can be something like -> //​Studentul:​ ANDREI Alex-Bogdan = [8.5,​6.0,​8.7]//​
-    * The student can be something like -> //​Studentul:​ ANDREI Alex-Bogdan = [8.5,​6.0,​8.7]//​</​note>​+
  
 2. The default ''​==''​ that we get from //deriving Eq// will check if 2 objects are identical. For our data, you'll have to provide a custom '​=='​ such that: 2. The default ''​==''​ that we get from //deriving Eq// will check if 2 objects are identical. For our data, you'll have to provide a custom '​=='​ such that:
Line 88: Line 86:
 As you can see, now we can add our data types in the new class, but also Haskell'​s types. The only requirement is that the enrolled type must implement our method. As you can see, now we can add our data types in the new class, but also Haskell'​s types. The only requirement is that the enrolled type must implement our method.
  
-5. Create a class ''​Contains b a''​ that will require a ''​contains :: b -> a -> Bool''​ method which will return True if ''​a''​ is in ''​b''​.+5. Create a class ''​Contains b a''​ that will require a ''​contains :: b -> a -> Bool''​ method which will return True if ''​a''​ is in ''​b''​. Add ''​[a]'',​ ''​List a''​ and ''​BTree a''​ to this class.
 <note tip>Do we need any additional restrictions for a or b? You can still add restrictions:​ <note tip>Do we need any additional restrictions for a or b? You can still add restrictions:​
 <code haskell> <code haskell>
Line 94: Line 92:
 </​code></​note>​ </​code></​note>​
  
-6. Create the ''​class Size a''​ which will require the methods ''​size''​ and ''​uniqueSize''​+6. Create the ''​class Size a''​ which will require the methods ''​size''​ and ''​uniqueSize''​. Add ''​[a]'',​ ''​List a''​ and ''​BTree a''​ to this class.
      - //size// = the numbers of elements in **a**      - //size// = the numbers of elements in **a**
      - //​uniqueSize//​ = the number of uniqueElements in **a**      - //​uniqueSize//​ = the number of uniqueElements in **a**