-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFirstSteps.hs
More file actions
145 lines (97 loc) · 3.19 KB
/
FirstSteps.hs
File metadata and controls
145 lines (97 loc) · 3.19 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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
module FirstSteps where
import Data.List
import Data.Maybe
import System.IO
addMe :: Integer -> Double -> Double
addMe x y = (fromIntegral x) + y
addMe2 :: Double -> Double -> Integer
addMe2 x y = round (x + y)
doSomething x = sum x
doSomething2 :: [Integer] -> Integer
doSomething2 x = sum x
noParam = [1..10]
add2 x y = x + y
add3 :: Num c => c -> c -> c
add3 x y = x + y
sumMaybe :: [Maybe Int] -> Int
sumMaybe x = sum (map unMaybe x)
unMaybe :: Num a => Maybe a -> a
unMaybe (Just x) = x
unMaybe Nothing = 0
addTuples :: (Int, Int) -> (Int, Int) -> (Int, Int)
addTuples (x1, y1) (x2, y2) = (x1 + x2, y1 + y2)
whatAge 16 = "you can drive"
whatAge 18 = "you can vote"
whatAge _ = "nothing important"
whatAge2 x
| x == 16 = "you can drive"
| x == 18 = "you can vote"
| otherwise = "nothing important"
times4 :: [Int] -> [Int]
times4 [] = []
--times4 (x:[]) = [4*x]
times4 (x:xs) = [4*x] ++ times4 xs
map2 :: (a -> b) -> [a] -> [b]
map2 f [] = []
--map2 f (x:xs) = [f x] ++ map2 f xs
map2 f (x:xs) = f x : map2 f xs
areStringEq :: [Char] -> [Char] -> Bool
areStringEq [] [] = True
areStringEq (x:xs) [] = False
areStringEq [] (y:ys) = False
areStringEq (x:xs) (y:ys) = x == y && areStringEq xs ys
getAddFunc :: Int -> (Int -> Int)
getAddFunc x y = x + y
adds3 = getAddFunc 3
fourPlus3 = adds3 4
concat2Strings :: String -> String -> String
concat2Strings x y = x ++ y
intermediateFuncThatHasFirstParamSetButSecondMissing = concat2Strings "hello "
welcome = intermediateFuncThatHasFirstParamSetButSecondMissing "world"
dbl1To10 = map (*2) [1..10]
lambdaTest = map (\x -> x^x + 1) [1..10]
data RPS = Rock | Paper | Scissors
shoot :: RPS -> RPS -> String
shoot Rock Paper = "player 2 wins"
shoot Rock Scissors = "player 1 wins"
shoot Paper Rock = "player 1 wins"
shoot Paper Scissors = "player 2 wins"
shoot Scissors Rock = "player 2 wins"
shoot Scissors Paper = "player 1 wins"
shoot _ _ = "draw"
-- Circle x, y, radius
-- Rectangle x, y, width, height
data Shape = Circle Float Float Float | Rectangle Float Float Float Float
deriving Show
area :: Shape -> Float
area (Circle _ _ r) = (r^2) * pi
area (Rectangle _ _ w h) = w * h
data Employee = Employee { name :: String,
position :: String,
idNum :: Int
} deriving (Eq, Show)
sam = Employee {name = "sam", position = "manager", idNum = 1}
pam = Employee {name = "pam", position = "sales", idNum = 2}
isSamPam = sam == pam
isSamSam = sam == sam
samData = show sam
data ShirtSize = S | M | L
instance Eq ShirtSize where
S == S = True
M == M = True
L == L = True
_ == _ = False
writeToFile = do
theFile <- openFile "test.txt" WriteMode
hPutStrLn theFile "hello world"
hClose theFile
fib = 1 : 1 : [a + b | (a, b) <- zip fib (tail fib)]
numberOfDecimalsOf1000thFib = floor (logBase 10 (fromIntegral (fib !! 1000)))
addNum :: Num c => c -> c -> c
addNum x y = x + y
showDouble :: Double -> String
showDouble d = show d
-- print all Chars
printAllChars = putStr . unlines $ [[x] | x <- [(minBound :: Char)..(maxBound :: Char)]]
-- or only a certain amount
printChars start num = putStr . unlines $ take num $ drop start [[x] | x <- [(minBound :: Char)..(maxBound :: Char)]]