Differences
This shows you the differences between two versions of the page.
Both sides previous revision Previous revision Next revision | Previous revision | ||
pp:class [2021/04/11 02:02] lfa |
pp:class [2021/04/11 22:15] (current) lfa |
||
---|---|---|---|
Line 2: | Line 2: | ||
=== Default Classes === | === Default Classes === | ||
+ | |||
+ | Today you can use the provided {{:pp:lab6_skel.zip|}}. | ||
Last time we've learned how to define our new data types in Haskell. The simpler ones were ''Ordering'' (some "constants"), ''Point Float Float'', and ''Student String String [Float]''. | Last time we've learned how to define our new data types in Haskell. The simpler ones were ''Ordering'' (some "constants"), ''Point Float Float'', and ''Student String String [Float]''. | ||
Line 24: | 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 46: | Line 47: | ||
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: | 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: | ||
- | * 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]// |
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 85: | 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''. Do we need any additional restrictions for a or b? You can still add restrictions: | + | 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: | ||
<code haskell> | <code haskell> | ||
class (SomeClass a) => Contains b a where ... | class (SomeClass a) => Contains b a where ... | ||
- | </code> | + | </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** |