Skip to content
This repository was archived by the owner on Dec 21, 2018. It is now read-only.

Commit 1c12559

Browse files
Merge pull request #11 from TechforgoodCAST/syntax-workshop
Syntax workshop
2 parents a0adea6 + 964c03d commit 1c12559

File tree

6 files changed

+790
-0
lines changed

6 files changed

+790
-0
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
elm-stuff
2+
elm.js

part1/syntax-workshop/Main.elm

Lines changed: 274 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,274 @@
1+
module Main exposing (..)
2+
3+
import Html exposing (..)
4+
import Html.Attributes exposing (..)
5+
6+
7+
-- write a function that takes two strings and concats them together
8+
9+
10+
stringConcat s1 s2 =
11+
s1 ++ s2
12+
13+
14+
15+
--
16+
-- write a function that takes a string and reverses it (take a look at Elm's list module)
17+
--
18+
-- stringReverse = ?
19+
--
20+
-- add adds two numbers together
21+
--
22+
-- add = ?
23+
--
24+
-- write a function that squares the Int given to it
25+
--
26+
-- square = ?
27+
--
28+
-- multiply takes two numbers and multiplies them together
29+
--
30+
-- multiply = ?
31+
--
32+
-- write a function that adds 1 to the argument given to it, reusing the add function you wrote
33+
--
34+
-- plusOne = ?
35+
--
36+
-- double multiplies its argument by 2, write it using multiply from earlier
37+
--
38+
--
39+
-- double = ?
40+
--
41+
-- isTeenage takes an age (a number) and returns True if it is a teenager, false if not
42+
--
43+
-- isTeenage = ?
44+
--
45+
-- write a function that concats two lists together
46+
--
47+
-- listConcat = ?
48+
--
49+
-- write a function that returns the length of a list
50+
--
51+
-- listLength = ?
52+
--
53+
-- write a function which takes a Int and returns a string which says whether the Int was
54+
-- 'negative', 'positive' or 'zero'
55+
--
56+
-- negativeOrPositive = ?
57+
--
58+
-- write a function that returns the absolute value of a Int using `if-then-else`
59+
--
60+
-- myAbs = ?
61+
--
62+
-- use a case statement to write a function that takes numbers 0-5 and turns them into strings.
63+
-- make sure you have a fallback case too
64+
--
65+
-- digitToString = ?
66+
--
67+
-- isMultipleof3 does what it says
68+
--
69+
-- isMultipleof3 = ?
70+
--
71+
-- write the myAbs again, this time using a case statement
72+
--
73+
-- myOtherAbs = ?
74+
--
75+
-- come up with a function that takes a Int and returns 'fizz' if it is a multiple of 3, 'buzz'
76+
-- if it is a multiple of 5, or 'fizzbuzz if it is a multiple of both'
77+
--
78+
-- fizzBuzz = ?
79+
--
80+
-- rewrite capitalizeWord using a let in expression, setting intermediate variables firstLetter and otherLetters
81+
--
82+
83+
84+
capitalizeWord word =
85+
String.toUpper (String.left 1 word) ++ String.dropLeft 1 word
86+
87+
88+
89+
-- myCapitalizeWord = ?
90+
--
91+
-- fst takes a tuple and returns the first element
92+
--
93+
-- fst = ?
94+
--
95+
-- snd takes a tuple and return the second element
96+
--
97+
-- snd = ?
98+
--
99+
-- area takes a tuple with height as first element and width as second and returns the area
100+
--
101+
-- area = ?
102+
--
103+
-- rewrite area using `let-in` expression assigning intermediate variables to width and height
104+
--
105+
-- area1 = ?
106+
--
107+
-- rewrite area using pattern matching
108+
--
109+
-- area2 = ?
110+
--
111+
-- write a function that takes an number and a list and splits the list at the number given and
112+
-- and returns the two lists in a tuple
113+
--
114+
-- splitList = ?
115+
--
116+
-- use pattern matching and a case statement to write a function that takes a list and returns a
117+
-- string informing you how many elements a list has
118+
-- the return values should be "empty list", "one element", "two elements", "many elements"
119+
--
120+
-- listDescritption = ?
121+
--
122+
123+
124+
steppenwolf =
125+
{ name = "Steppenwolf"
126+
, author = "Hermann Hesse"
127+
, pages = 237
128+
}
129+
130+
131+
siddharta =
132+
{ name = "Siddharta"
133+
, author = "Herman Hesse"
134+
, pages = 150
135+
}
136+
137+
138+
island =
139+
{ name = "Island"
140+
, author = "Aldous Huxley"
141+
, pages = 258
142+
}
143+
144+
145+
doorsOfPerception =
146+
{ name = "Doors of Perception"
147+
, author = "Aldous Huxley"
148+
, pages = 101
149+
}
150+
151+
152+
aliceInWonderland =
153+
{ name = "Alice in Wonderland"
154+
, author = "Lewis Carrol"
155+
, pages = 287
156+
}
157+
158+
159+
160+
-- write a function that takes one of the above records and returns the name field
161+
-- name = ?
162+
--
163+
-- write a function that takes a record and returns the number of pages (this time use pattern matching)
164+
--
165+
-- pages = ?
166+
--
167+
-- over150 takes a record and returns True if it has over 250 pages, false if not
168+
-- over150 = ?
169+
--
170+
-- updatePages takes a number and a record and updates a records page field
171+
--
172+
-- updatePages = ?
173+
-- updateNameAndAuthor takes a two strings and a record and updates the name and author fields
174+
--
175+
-- updateNameAndAuthor = ?
176+
--
177+
-- write a function that takes a string, reverses, uppercases and then concats "CHOCOLATE " to the
178+
-- beginning of it (use your stringReverse and stringConcat functions)
179+
--
180+
-- pipePractice = ?
181+
--
182+
-- write a function that takes a number adds 1 to it, doubles it, then squares it and then checks
183+
-- if it is a teenage age. Use plusOne, double, square and isTeenage
184+
--
185+
--pipePractice1 = ?
186+
--
187+
-- TESTS
188+
189+
190+
testSpec : List ( Bool, String )
191+
testSpec =
192+
[ ( stringConcat "hello" "world" == "helloworld", "stringConcat" )
193+
-- , ( stringReverse "stressed" == "desserts", "stringReverse" )
194+
-- , ( add 4 5 == 9, "add" )
195+
-- , ( square 3 == 9, "square" )
196+
-- , ( multiply 4 5 == 20, "mulitply" )
197+
-- , ( plusOne 4 == 5, "plusOne" )
198+
-- , ( double 20 == 40, "double" )
199+
-- , ( isTeenage 13 == True, "isTeenage" )
200+
-- , ( isTeenage 20 == False, "isTeenage" )
201+
-- , ( listConcat [ 1, 2, 3 ] [ 4, 5, 6 ] == [ 1, 2, 3, 4, 5, 6 ], "listConcat" )
202+
-- , ( listLength [ 'a', 'b', 'c' ] == 3, "listLength" )
203+
-- , ( negativeOrPositive -1 == "n is negative", "negativeOrPositive" )
204+
-- , ( negativeOrPositive 1 == "n is positive", "negativeOrPositive" )
205+
-- , ( negativeOrPositive 0 == "n is zero", "negativeOrPositive" )
206+
-- , ( myAbs -2 == 2, "abs" )
207+
-- , ( myAbs 2 == 2, "abs1" )
208+
-- , ( digitToString 3 == "three", "digitToString" )
209+
-- , ( digitToString 5 == "five", "digitToString" )
210+
-- , ( isMultipleof3 5 == False, "isMultipleof3" )
211+
-- , ( isMultipleof3 6 == True, "isMultipleof3" )
212+
-- , ( myOtherAbs -2 == 2, "abs" )
213+
-- , ( myOtherAbs 2 == 2, "abs" )
214+
-- , ( fizzBuzz 15 == "fizzbuzz", "fizzBuzz" )
215+
-- , ( fizzBuzz 12 == "fizz", "fizzBuzz" )
216+
-- , ( fizzBuzz 10 == "buzz", "fizzBuzz" )
217+
-- , ( myCapitalizeWord "elm" == "Elm", "myCapitalizeWord" )
218+
-- , ( fst ( 4, 5 ) == 4, "fst" )
219+
-- , ( snd ( "react", "elm" ) == "elm", "elm" )
220+
-- , ( area ( 4, 5 ) == 20, "area" )
221+
-- , ( area1 ( 4, 5 ) == 20, "area1" )
222+
-- , ( area2 ( 4, 5 ) == 20, "area2" )
223+
-- , ( splitList 2 [ 1, 2, 3, 4 ] == ( [ 1, 2 ], [ 3, 4 ] ), "splitList" )
224+
-- , ( listDescritption [] == "empty list", "listDescritption" )
225+
-- , ( listDescritption [ 1 ] == "one element", "listDescritption" )
226+
-- , ( listDescritption [ 1, 2 ] == "two elements", "listDescritption" )
227+
-- , ( listDescritption [ 1, 2, 3, 4, 5 ] == "many elements", "listDescritption" )
228+
-- , ( name island == "Island", "name" )
229+
-- , ( pages doorsOfPerception == 101, "pages" )
230+
-- , ( over150 siddharta == False, "over150" )
231+
-- , ( updatePages 151 siddharta == { siddharta | pages = 151 }, "updatePages" )
232+
-- , ( updateNameAndAuthor "Der Steppenwolf" "Hermann Karl Hesse" steppenwolf
233+
-- == { steppenwolf
234+
-- | name = "Der Steppenwolf"
235+
-- , author = "Hermann Karl Hesse"
236+
-- }
237+
-- , "updateNameAndAuthor"
238+
-- )
239+
-- , ( pipePractice "stressed" == "CHOCOLATE DESSERTS", "pipePractice" )
240+
-- , ( pipePractice1 1 == True, "pipePractice1" )
241+
-- , ( pipePractice1 2 == False, "pipePractice1" )
242+
]
243+
244+
245+
testOutputToString : a -> Html msg
246+
testOutputToString x =
247+
x
248+
|> toString
249+
|> text
250+
|> List.singleton
251+
|> li []
252+
253+
254+
testsPassed : List ( Bool, String ) -> Bool
255+
testsPassed l =
256+
l
257+
|> List.map (\( b, _ ) -> b)
258+
|> List.all (\b -> b == True)
259+
260+
261+
testSummary : Bool -> String
262+
testSummary b =
263+
if b then
264+
"All tests passed :)"
265+
else
266+
"Uh oh, some tests failed"
267+
268+
269+
main : Html msg
270+
main =
271+
div []
272+
[ div [] [ ol [] (List.map testOutputToString testSpec) ]
273+
, h2 [ class "ml2 blue" ] [ text <| testSummary <| testsPassed testSpec ]
274+
]

part1/syntax-workshop/README.md

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
# Part 1
2+
3+
## Goals
4+
5+
* Get used to Elm syntax
6+
* Introduction to new Elm datatypes
7+
* Introduction to type signatures
8+
9+
## Elm syntax && datatypes
10+
11+
1. Read through https://guide.elm-lang.org/core_language.html
12+
13+
2. Complete exercises in `Main.elm`, keeping this page open for reference - http://elm-lang.org/docs/syntax
14+
15+
N.B. There are a lot of exercises and it might start to get a little tedious without any context. However these are the building blocks of the language and once you've worked through the exercises you'll have a solid foundation that will allow us to move on to the juicy bits 🍹
16+
17+
## Type signatures
18+
19+
1. Read through https://guide.elm-lang.org/types/ up to the `Type Aliases`
20+
21+
2. Add type signatures to your solutions in `Main.elm`
22+
23+
## Tips
24+
25+
* You will probably have a hard time getting your code to compile at first. This can be frustrating, however if you keep at it the compiler will soon become your best friend
26+
27+
* If you don't understand what a function is meant to do, have a look at the test case for it near the bottom of the file
28+
29+
30+
## Installation
31+
32+
If you haven't already
33+
34+
```bash
35+
npm i -g elm elm-live
36+
```
37+
38+
```bash
39+
elm-package install
40+
```
41+
42+
(Answer `y` when prompted.)
43+
44+
45+
## Building
46+
47+
```bash
48+
elm-live Main.elm --open --pushstate --output=elm.js
49+
```

0 commit comments

Comments
 (0)