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:20] bogdan.deac |
pp:2023:haskell:l09 [2023/05/02 22:15] (current) bogdan.deac |
||
---|---|---|---|
Line 53: | 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> | ||
Line 66: | 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 104: | 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 163: | 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 |