-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlanguageBasicsLecture.hs
More file actions
87 lines (73 loc) · 2.63 KB
/
languageBasicsLecture.hs
File metadata and controls
87 lines (73 loc) · 2.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
module Revision where
birthdayMessage :: Int -> String
birthdayMessage age =
if age>100
then "Happy Birthday from the Queen!"
else if age<20
then "Happy Birthday, you're " ++ (show (abs age)) --abs, is like modulus |-3| => 3
else "Happy Birthday, another year older."
birthdayMessage' :: Int -> String
birthdayMessage' age
|age>100 = "Happy Birthday from the Queen!"
|age<20 = "Happy Birthday, you're " ++ (show (abs age))
|otherwise = "Happy Birthday, another year older."
-------------------------------------------------------------------------------------------
--WHERE
--where allows local definitions after the main body of the function.
--these can be local variables or local functions
hoodMessage :: String -> String
hoodMessage name = greetBlid ++ " , just a reminder yo... " ++ reminder
where --must indent lesser variables/functions properly
greetBlid = "Welcome to da hood " ++ name -- local variable definition
reminder = "talk shit. get hit." --local variable definition
--let and in are always used together
hoodMessage' :: String -> String
hoodMessage' name =
let
greetBlid = "Welcome to da hood " ++ name -- local variable definition
reminder = "talk shit. get hit." --local variable definition
in
greetBlid ++ " , just a reminder yo... " ++ reminder
-------------------------------------------------------------------------------------------
sumDiff :: Int -> Int -> Int
sumDiff num1 num2 =
let
s = num1+num2--finds sum
d = abs(num1-num2)--finds positive difference
in
(s*s) + (d*d)
sumDiff' :: Int -> Int -> Int
sumDiff' num1 num2 = (sum*sum) + (diff*diff)
where
sum = num1 + num2
diff = abs(num1-num2)
-------------------------------------------------------------------------------------------
--case-of
review :: String -> String
review bandName =
case bandName of
"Rise Against" -> "Revolutionary"
"Vance Joy" -> "Nostalgic"
"Shakira" -> "She can dance!"
_ -> "Not in our review options. :("
type Count = Int
type Sound = String
speak :: Count -> Sound
speak number =
case number of
1 -> "One"
2 -> "Two"
3 -> "Three"
4 -> "Four"
_ -> "Cant yet speak that number."
-------------------------------------------------------------------------------------------
--function application
square :: Int -> Int
square x = x * x
--using other functions inside functions
quad :: Int -> Int
quad x = square (square x)
chopOffEnd :: String -> String
chopOffEnd cs = reverse(tail(reverse cs))
chopOffEnd' :: [a] -> [a]
chopOffEnd' = reverse . tail . reverse