8.3. A custom boolean¶
We want to define a boolean type.
The standard library already has a type Bool
but we will make your own.
Note: this exercise is intended to be solved using both a Haskell source file and ghci.
My recommendation is to implement the code in a file (Something.hs
) then open ghci
and load the file with the :load FileName.hs
(this has autocompletion for the file name as well) command.
After that the types and functions you defined in the file will be in scope and you can play around with them.
Note: In ghci bindings must be created with let binding = expr
.
8.3.2. if
¶
Booleans are used for if
expressions.
Therefore we will define our own if
.
Since if
is a keyword in Haskell you can use a name like if_
or if'
or something else.
Your if
function should take three arguments.
- A
Boolean
(your custom boolean type) as condition. - A value which to return when the boolean is
Yes
. - A value which to return when the boolean is
No
.
8.3.3. Boolean operations¶
We also need to be able to define more complex interactions.
Implement a not'
, or'
and and'
operation which, as the names suggest, do boolean not, and and or. [4]
Finally play around some with the operations you have defined. Make sure they are indeed correct.
footnotes
[1] | Use the data keyword |
[2] | Use a case construct to match on the two Boolean constructors. |
[3] | Use a type variable for the two values. If you’re stuck think about what you know about the two values (do they perhaps have the same type? Aka the type variable needs to be the same) and what type would the return be? |
[4] | A nested case match should be useful here.
Alternatively you can match on both booleans simultaneously if you wrap them in a tuple or if you match them with the special function syntax on the arguments. |