Skip to content

Commit 67d7875

Browse files
authored
Add concept exercise for booleans (#331)
* Add concept exercise for booleans * Fix format of markdown file hints.md * Move return comments on new line in Annalyn instructions * annalyn: update .meta/config.json * annalyn: update .meta/design.md prerequisites * booleans: add a .meta/config.json * config.json: reorder fields to match docs * config.json: add booleans concept and annalyn concept exercise
1 parent 893544d commit 67d7875

File tree

14 files changed

+891
-29
lines changed

14 files changed

+891
-29
lines changed
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"blurb": "Learn how to use booleans in an Elm program",
3+
"authors": [
4+
"mpizenberg"
5+
],
6+
"contributors": [
7+
"ceddlyburge"
8+
]
9+
}

concepts/booleans/about.md

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
# About
2+
3+
The `Bool` type is one of the building blocks of the language.
4+
It aims at describing whether something is true or false, with the respective `True` and `False` values.
5+
The name `Bool` is short for Boolean, which is a field of mathematics related to logical thinking, named after the work of George Boole.
6+
7+
## Boolean operators
8+
9+
There are two functions and two operators to manipulate values of type `Bool`.
10+
11+
- `not : Bool -> Bool`
12+
This function transforms `True` into `False` and `False` into `True`.
13+
- `(&&) : Bool -> Bool -> Bool`
14+
This operator called "AND" takes two booleans and returns `True` if they are both `True`.
15+
For example, `True && True` returns `True` but `True && False` returns `False`.
16+
- `(||) : Bool -> Bool -> Bool`
17+
This operator called "OR" takes two booleans and returns `True` if at least one of them is `True`.
18+
For example, `False || False` returns `False` but `True || False` returns `True`.
19+
- `xor : Bool -> Bool -> Bool`
20+
This function (not operator) called "XOR" takes two booleans and returns `True` if and only if one is `True` and the other is `False`.
21+
22+
The `&&` and `||` operators do not have the same priority.
23+
The priority of `&&` is higher than the one of `||`, just like with multiplication `*` and addition `+`.
24+
As a consequence, the expression `True || False && False` is equivalent to `True || (False && False)` which is different from `(True || False) && False`.
25+
While the former returns `True` the latter returns `False`.
26+
27+
## Conditional branching
28+
29+
Booleans are extremely useful to make branches in your code that only get executed if certain conditions are met.
30+
In Elm, this is done with an `if-then-else` expression, used as follows.
31+
32+
```elm
33+
if someCondition then
34+
doSomething
35+
else
36+
doSomethingElse
37+
```
38+
39+
The value `someCondition` can be any expression that returns a `Bool`.
40+
If that boolean value is `True`, the code will execute `doSomething`.
41+
Otherwise it is `False` and the code will execute `doSomethingElse`.
42+
The `else` branch is mandatory, contrary to some other programming languages.
43+
44+
If you need to chain conditions, you can just chain the `if-then-else` expressions.
45+
46+
```elm
47+
if someCondition then
48+
doSomething
49+
else if someOtherCondition then
50+
doAnotherThing
51+
else
52+
doSomethingElse
53+
```

concepts/booleans/introduction.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# Introduction
2+
3+
The `Bool` type is one of the building blocks of the language.
4+
It aims at describing whether something is true or false, with the respective `True` and `False` values.
5+
6+
Booleans can be combined with the `&&` "AND" and the `||` "OR" operators, and negated with the `not` function.
7+
8+
Booleans are also extremely useful to make branches in your code that only get executed if certain conditions are met.
9+
In Elm, this is done with an `if-then-else` expression, used as follows.
10+
11+
```elm
12+
if someCondition then
13+
doSomething
14+
else
15+
doSomethingElse
16+
```

concepts/booleans/links.json

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
[
2+
{
3+
"url": "https://package.elm-lang.org/packages/elm/core/latest/Basics#Bool",
4+
"description": "Boolean operators"
5+
},
6+
{
7+
"url": "https://elm-lang.org/docs/syntax#conditionals",
8+
"description": "Syntax for conditional branches"
9+
}
10+
]

config.json

Lines changed: 43 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,41 @@
22
"language": "Elm",
33
"slug": "elm",
44
"active": true,
5+
"blurb": "Elm is a friendly functional language for the Web",
6+
"version": 3,
7+
"online_editor": {
8+
"indent_style": "space",
9+
"indent_size": 4
10+
},
511
"status": {
612
"concept_exercises": false,
713
"test_runner": true,
814
"representer": true,
915
"analyzer": false
1016
},
11-
"blurb": "Elm is a friendly functional language for the Web",
12-
"version": 3,
13-
"online_editor": {
14-
"indent_style": "space",
15-
"indent_size": 4
17+
"files": {
18+
"solution": [
19+
"src/%{pascal_slug}.elm"
20+
],
21+
"test": [
22+
"tests/Tests.elm"
23+
],
24+
"example": [
25+
".meta/src/%{pascal_slug}.example.elm"
26+
],
27+
"exemplar": [
28+
".meta/Exemplar.elm"
29+
]
1630
},
31+
"tags": [
32+
"paradigm/functional",
33+
"typing/static",
34+
"typing/strong",
35+
"execution_mode/compiled",
36+
"platform/web",
37+
"used_for/frontends",
38+
"used_for/web_development"
39+
],
1740
"key_features": [
1841
{
1942
"title": "For the Web",
@@ -46,15 +69,6 @@
4669
"icon": "features-functional"
4770
}
4871
],
49-
"tags": [
50-
"paradigm/functional",
51-
"typing/static",
52-
"typing/strong",
53-
"execution_mode/compiled",
54-
"platform/web",
55-
"used_for/frontends",
56-
"used_for/web_development"
57-
],
5872
"concepts": [
5973
{
6074
"uuid": "3e6e4a21-cc66-4c48-857b-90e1ca5298f9",
@@ -65,22 +79,13 @@
6579
"uuid": "e850acd6-63f3-427e-a883-222e0bd89a59",
6680
"slug": "basics-2",
6781
"name": "Basics 2"
82+
},
83+
{
84+
"uuid": "2f7f1b64-5ee5-4600-a81d-87a2c79815a5",
85+
"slug": "booleans",
86+
"name": "Booleans"
6887
}
6988
],
70-
"files": {
71-
"solution": [
72-
"src/%{pascal_slug}.elm"
73-
],
74-
"test": [
75-
"tests/Tests.elm"
76-
],
77-
"example": [
78-
".meta/src/%{pascal_slug}.example.elm"
79-
],
80-
"exemplar": [
81-
".meta/Exemplar.elm"
82-
]
83-
},
8489
"exercises": {
8590
"concept": [
8691
{
@@ -102,8 +107,17 @@
102107
],
103108
"prerequisites": [ "basics-1" ],
104109
"status": "wip"
110+
},
111+
{
112+
"slug": "annalyns-infiltration",
113+
"name": "Annalyn's Infiltration",
114+
"uuid": "9f501a73-14e3-4ada-8359-fe051530554a",
115+
"concepts": [
116+
"booleans"
117+
],
118+
"prerequisites": [ "basics-2" ],
119+
"status": "wip"
105120
}
106-
107121
],
108122
"practice": [
109123
{
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# Hints
2+
3+
## General
4+
5+
- Boolean functions and operators are described in the [Elm documentation of the Basics module][bool].
6+
7+
[bool]: https://package.elm-lang.org/packages/elm/core/latest/Basics#Bool
8+
9+
## 5. Count the damage that a stealth attack could do
10+
11+
- Conditional examples are provided in the [Elm syntax guide][syntax].
12+
13+
[syntax]: https://elm-lang.org/docs/syntax#conditionals
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
# Instructions
2+
3+
In this exercise, you'll be implementing the quest logic for a new RPG game a friend is developing. The game's main character is Annalyn, a brave girl with a fierce and loyal pet dog. Unfortunately, disaster strikes, as her best friend was kidnapped while searching for berries in the forest. Annalyn will try to find and free her best friend, optionally taking her dog with her on this quest.
4+
5+
After some time spent following her best friend's trail, she finds the camp in which her best friend is imprisoned. It turns out there are two kidnappers: a mighty knight and a cunning archer.
6+
7+
Having found the kidnappers, Annalyn considers which of the following actions she can engage in:
8+
9+
- Fast attack: a fast attack can be made if the knight is sleeping, as it takes time for him to get his armor on, so he will be vulnerable.
10+
- Stealth attack: if Annalyn reaches the sleeping knight without being detected, she deals 12 damage with a stealth attack. Otherwise, she deals only 7 damage to the knight.
11+
- Spy: the group can be spied upon if at least one of them is awake. Otherwise, spying is a waste of time.
12+
- Signal prisoner: the prisoner can be signalled using bird sounds if the prisoner is awake and the archer is sleeping, as archers are trained in bird signaling so they could intercept the message.
13+
- Free prisoner: if the prisoner is awake and the other two characters are sleeping, a sneaky entry into the camp can free the prisoner. This won't work if the prisoner is sleeping, as the prisoner will be startled by the sudden appearance of her friend and the knight and archer will be awoken. The prisoner can also be freed if the archer is sleeping and Annalyn has her pet dog with her, as the knight will be scared by the dog and will withdraw, and the archer can't equip his bow fast enough to prevent the prisoner from being freed.
14+
15+
You have five tasks: to implement the logic for determining if the above actions are available based on the state of the three characters found in the forest and whether Annalyn's pet dog is present or not.
16+
17+
## 1. Check if a fast attack can be made
18+
19+
Implement the `canFastAttack` function that takes a boolean value that indicates if the knight is awake. This function returns `True` if a fast attack can be made based on the state of the knight. Otherwise, returns `False`:
20+
21+
```elm
22+
knightIsAwake = True
23+
24+
canFastAttack knightIsAwake
25+
--> False
26+
```
27+
28+
## 2. Check if the group can be spied upon
29+
30+
Implement the `canSpy` function that takes three boolean values, indicating if the knight, archer and the prisoner, respectively, are awake. The function returns `True` if the group can be spied upon, based on the state of the three characters. Otherwise, returns `False`:
31+
32+
```elm
33+
knightIsAwake = False
34+
archerIsAwake = True
35+
prisonerIsAwake = False
36+
37+
canSpy knightIsAwake archerIsAwake prisonerIsAwake
38+
--> True
39+
```
40+
41+
## 3. Check if the prisoner can be signalled
42+
43+
Implement the `canSignalPrisoner` function that takes two boolean values, indicating if the archer and the prisoner, respectively, are awake. The function returns `True` if the prisoner can be signalled, based on the state of the two characters. Otherwise, returns `False`:
44+
45+
```elm
46+
archerIsAwake = False
47+
prisonerIsAwake = True
48+
49+
canSignalPrisoner archerIsAwake prisonerIsAwake
50+
--> True
51+
```
52+
53+
## 4. Check if the prisoner can be freed
54+
55+
Implement the `canFreePrisoner` function that takes four boolean values. The first three parameters indicate if the knight, archer and the prisoner, respectively, are awake. The last parameter indicates if Annalyn's pet dog is present. The function returns `True` if the prisoner can be freed based on the state of the three characters and Annalyn's pet dog presence. Otherwise, it returns `False`:
56+
57+
```elm
58+
knightIsAwake = False
59+
archerIsAwake = True
60+
prisonerIsAwake = False
61+
petDogIsPresent = False
62+
63+
canFreePrisoner knightIsAwake archerIsAwake prisonerIsAwake petDogIsPresent
64+
--> False
65+
```
66+
67+
## 5. Count the damage that a stealth attack could do
68+
69+
Implement the `stealthAttackDamage` function that takes a boolean value indicating if Annalyn was detected. This function returns 7 if she was detected, 12 otherwise.
70+
71+
```elm
72+
annalynIsDetected = True
73+
74+
stealthAttackDamage annalynIsDetected
75+
--> 7
76+
```
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
# Introduction
2+
3+
The `Bool` type is one of the building blocks of the language.
4+
It aims at describing whether something is true or false, with the respective `True` and `False` values.
5+
The name `Bool` is short for Boolean, which is a field of mathematics related to logical thinking, named after the work of George Boole.
6+
7+
## Boolean operators
8+
9+
There are two functions and two operators to manipulate values of type `Bool`.
10+
11+
- `not : Bool -> Bool`
12+
This function transforms `True` into `False` and `False` into `True`.
13+
- `(&&) : Bool -> Bool -> Bool`
14+
This operator called "AND" takes two booleans and returns `True` if they are both `True`.
15+
For example, `True && True` returns `True` but `True && False` returns `False`.
16+
- `(||) : Bool -> Bool -> Bool`
17+
This operator called "OR" takes two booleans and returns `True` if at least one of them is `True`.
18+
For example, `False || False` returns `False` but `True || False` returns `True`.
19+
- `xor : Bool -> Bool -> Bool`
20+
This function (not operator) called "XOR" takes two booleans and returns `True` if and only if one is `True` and the other is `False`.
21+
22+
The `&&` and `||` operators do not have the same priority.
23+
The priority of `&&` is higher than the one of `||`, just like with multiplication `*` and addition `+`.
24+
As a consequence, the expression `True || False && False` is equivalent to `True || (False && False)` which is different from `(True || False) && False`.
25+
While the former returns `True` the latter returns `False`.
26+
27+
## Conditional branching
28+
29+
Booleans are extremely useful to make branches in your code that only get executed if certain conditions are met.
30+
In Elm, this is done with an `if-then-else` expression, used as follows.
31+
32+
```elm
33+
if someCondition then
34+
doSomething
35+
else
36+
doSomethingElse
37+
```
38+
39+
The value `someCondition` can be any expression that returns a `Bool`.
40+
If that boolean value is `True`, the code will execute `doSomething`.
41+
Otherwise it is `False` and the code will execute `doSomethingElse`.
42+
The `else` branch is mandatory, contrary to some other programming languages.
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
module AnnalynsInfiltration exposing (canFastAttack, canFreePrisoner, canSignalPrisoner, canSpy, stealthAttackDamage)
2+
3+
4+
canFastAttack : Bool -> Bool
5+
canFastAttack knightIsAwake =
6+
not knightIsAwake
7+
8+
9+
canSpy : Bool -> Bool -> Bool -> Bool
10+
canSpy knightIsAwake archerIsAwake prisonerIsAwake =
11+
knightIsAwake || archerIsAwake || prisonerIsAwake
12+
13+
14+
canSignalPrisoner : Bool -> Bool -> Bool
15+
canSignalPrisoner archerIsAwake prisonerIsAwake =
16+
not archerIsAwake && prisonerIsAwake
17+
18+
19+
canFreePrisoner : Bool -> Bool -> Bool -> Bool -> Bool
20+
canFreePrisoner knightIsAwake archerIsAwake prisonerIsAwake petDogIsPresent =
21+
not knightIsAwake && not archerIsAwake && prisonerIsAwake || not archerIsAwake && petDogIsPresent
22+
23+
24+
stealthAttackDamage : Bool -> Int
25+
stealthAttackDamage annalynIsDetected =
26+
if annalynIsDetected then
27+
7
28+
29+
else
30+
12
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
{
2+
"blurb": "Learn usage of Booleans while helping Annalyn rescue her friend",
3+
"authors": [
4+
"mpizenberg"
5+
],
6+
"contributors": [
7+
"ceddlyburge"
8+
],
9+
"forked_from": ["fsharp/AnnalynsInfiltration"],
10+
"files": {
11+
"solution": ["src/AnnalynsInfiltration.elm"],
12+
"test": ["tests/Tests.elm"],
13+
"exemplar": [".meta/Exemplar.elm"]
14+
}
15+
}

0 commit comments

Comments
 (0)