Differences
This shows you the differences between two versions of the page.
Both sides previous revision Previous revision Next revision | Previous revision | ||
pp:2023:haskell:l09 [2023/04/30 12:17] bogdan.deac |
pp:2023:haskell:l09 [2023/05/02 22:15] (current) bogdan.deac |
||
---|---|---|---|
Line 46: | Line 46: | ||
</code> | </code> | ||
- | Type Classes are also an important concept in haskell. They are very similar in concept with Java interfaces (we implement certain behaviours through them). For example, we might want to convert an entity to a string to print it. This could be done by implementing the Show class. Take a look at its [[implementation | https://hackage.haskell.org/package/base-4.18.0.0/docs/src/GHC.Show.html#Show]]. Notice the '{-# MINIMAL showsPrec | show #-}'. That tells us that it's enough to implement one of those functions to have a complete implementation. Let's implement show for ''Maybe a'' | + | |
+ | Typeclasses are an important concept in haskell. They are very similar in concept to Java interfaces (we implement certain behaviours through them). For example, we might want to convert an entity to a string to print it. This could be done by implementing the Show class. Take a look at its [[https://hackage.haskell.org/package/base-4.18.0.0/docs/src/GHC.Show.html#Show | implementation]]. Notice the ''{-# MINIMAL showsPrec | show #-}''. That tells us that it's enough to implement one of those functions to have a complete implementation. Let's implement show for ''Maybe a'' | ||
<code haskell> | <code haskell> | ||
Line 52: | Line 53: | ||
instance Show a => Show (Maybe a) where | instance Show a => Show (Maybe a) where | ||
show Nothing = "Nothing" | show Nothing = "Nothing" | ||
- | Show (Just x) = "Just" ++ show x | + | show (Just x) = "Just " ++ show x |
</code> | </code> | ||
- | In summary, typeclasses allow us to abstract certain behaviours. Some important ones we'll use in this lab are ''Eq''(to specifiy whether 2 things are equal or different) or ''Ord'', to be able to compare types. | + | In summary, typeclasses allow us to abstract certain behaviours. Some important ones we'll use in this lab are ''Eq''(to specifiy whether 2 things are equal or different) or ''Ord'', to be able to compare type instances. |
After the lab, you can take a look at some more cool stuff related to data types in haskell and functional types in general \\ | After the lab, you can take a look at some more cool stuff related to data types in haskell and functional types in general \\ | ||
Line 65: | Line 66: | ||
**9.1.1** Let's implement a ''ChessResult'' data type. In chess, a game result can be either a Win, a Draw or a Loss. | **9.1.1** Let's implement a ''ChessResult'' data type. In chess, a game result can be either a Win, a Draw or a Loss. | ||
<code haskell> | <code haskell> | ||
- | data ChessResult = ??? deriving Show | + | data ChessResult = ??? |
</code> | </code> | ||
- | **9.1.2** We want to be able to print a ''ChessResult''. For this, we could do ''deriving Show'' to let GHC derive the implementation, but let's do it ourselves. | + | **9.1.2** We want to be able to show a ''ChessResult''. For this, we could do ''deriving Show'' to let GHC derive the implementation, but let's do it ourselves. |
<code haskell> | <code haskell> | ||
instance Show ChessResult where | instance Show ChessResult where | ||
Line 103: | Line 104: | ||
We also want to model a chess tournament. We'll represent it as a binary tree. | We also want to model a chess tournament. We'll represent it as a binary tree. | ||
<code Haskell> | <code Haskell> | ||
- | data player = Player { ??? } | + | data Player = Player { ??? } |
data Tree a = ??? deriving Show | data Tree a = ??? deriving Show | ||
type Tournament = Tree Player | type Tournament = Tree Player | ||
Line 162: | Line 163: | ||
</code> | </code> | ||
- | **9.2.8** Carolina wants to see how her beloved Romeo did in his first tournament. However, he sometimes goes to the 'chess' club instead of playing, so he may have not been present at all. Implement the function ''findPlayerScoreByName'', to find out how well he did | + | **9.2.8** Carolina wants to see how her beloved Romeo did. However, he sometimes goes to the 'chess' club instead of playing, so he may have not been present at all. Implement the function ''findPlayerScoreByName'', which returns ''Just score'' if he was there or ''Nothing'' if he wasn't |
<code haskell> | <code haskell> | ||
findPlayerScoreByName :: String -> Tournament -> Maybe Float | findPlayerScoreByName :: String -> Tournament -> Maybe Float |