-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBasics.hs
More file actions
270 lines (195 loc) · 5.74 KB
/
Copy pathBasics.hs
File metadata and controls
270 lines (195 loc) · 5.74 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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
module Basics where
-- Don't worry about this line. It's just hiding some functions that are
-- usually imported by default, but which I'm defining my own versions of
-- in this intro file.
import Prelude hiding (length,sum,product,map,foldr)
---------------------
-- Introduce Tools --
---------------------
-- * GHCi commands
-- :help, :load, :reload, :quit, :type, :info
-- * Hoogle
-- * doctest
---------------------
-- Getting Started --
---------------------
-- In GHCi:
-- * basic data types (Bool, Int, Float)
-- * numeric and boolean operators
-- * if-then-else expressions
-- * let-expressions
---------------------
-- Basic Functions --
---------------------
-- * defining and applying functions
-- * pattern matching
-- * partial application
-- | Add an integer to itself.
double :: Int -> Int
double x = x + x
-- | Is this integer zero?
isZero :: Int -> Bool
isZero 0 = True
isZero _ = False
-- isZero x = x == 0
-- | Is this integer non-zero?
isNonZero :: Int -> Bool
isNonZero = not . isZero
-- isNonZero x = not (isZero x)
-- | Computes the average of two floating point numbers.
avg :: Float -> Float -> Float
avg x y = (x + y) / 2.0
-- | Uses avg to compute half of a floating point number.
half :: Float -> Float
half = avg 0.0
-- half x = avg 0.0 x
-- In GHCi:
-- * infix vs. prefix application: operators are just functions!
-- * (+) x y = x + y
-- * avg x y = x `avg` y
-- * anonymous functions
-- | Operator that computes average.
(*-*) :: Float -> Float -> Float
(*-*) = avg
-- (*-*) x y = (x + y) / 2.0
----------------------
-- Basic Data Types --
----------------------
-- * a data type definition consists of:
-- * a new type name
-- * a set of cases, each with:
-- * a data constructor
-- * zero or more arguments
-- * more pattern matching
-- * top-level and case-expressions
-- | An example data type with two cases.
data Result = OK Int | Error
deriving (Eq,Show)
-- instance Show Result where
-- show (OK i) = show i ++ " :-)"
-- show Error = ":-("
-- | Safely divide two integers.
safeDiv :: Int -> Int -> Result
safeDiv _ 0 = Error
safeDiv x y = OK (x `div` y)
-- | Add two results.
addResults :: Result -> Result -> Result
addResults (OK x) (OK y) = OK (x + y)
addResults _ _ = Error
-- addResults rx ry = case rx of
-- Error -> Error
-- OK x -> case ry of
-- Error -> Error
-- OK y -> OK (x + y)
--
-- addResults rx ry = case (rx,ry) of
-- (OK x, OK y) -> OK (x + y)
-- _ -> Error
-- | Get the integer from an OK result, or return 0 on an Error.
fromResult :: Result -> Int
fromResult (OK i) = i
fromResult Error = 0
-- The definition of Bool in the Haskell Prelude looks like this:
--
-- data Bool = False | True
-- Similar to Result from Prelude:
--
-- data Maybe a = Just a | Nothing
---------------
-- Recursion --
---------------
-- * recursive data type definitions
-- * recursive functions
-- | An example of a recursive data type.
data List = Nil
| Cons Int List
deriving (Eq,Show)
-- | Compute the length of a list.
listLength :: List -> Int
listLength Nil = 0
listLength (Cons _ t) = 1 + listLength t
-- | Compute the sum of the integers in a list.
listSum :: List -> Int
listSum Nil = 0
listSum (Cons h t) = h + listSum t
-- Lists that contain elements of any type (but they all must be the same):
--
-- data List a = Nil
-- | Cons a (List a)
--
-- listLength :: List a -> Int
-- listSum :: List Int -> Int
-- listSum' :: Num a => List a -> a
-- Example evaluation:
--
-- listSum (Cons 3 (Cons 4 Nil))
-- => 3 + listSum (Cons 4 Nil)
-- => 3 + (4 + listSum Nil)
-- => 3 + (4 + 0)
-- =>* 7
-------------------
-- Haskell Lists --
-------------------
-- * Haskell's built-in list and string types
-- * cons, nil, and syntactic sugar
-- * more recursive functions
-- data [a] = [] -- Nil
-- | a : [a] -- Cons
-- The definition of String in the Haskell Prelude looks like this:
--
-- type String = [Char]
-- | Compute the length of a list.
length :: [a] -> Int
length [] = 0
length (_:t) = 1 + length t
-- | Compute the sum of an integer list.
sum :: [Int] -> Int
sum [] = 0
sum (h:t) = h + sum t
-- | Compute the product of the elements in a list.
product :: [Int] -> Int
product [] = 1
product (h:t) = h * product t
square :: [Int] -> [Int]
square [] = []
square (h:x) = (h * h) : square x
-- | Double all the elements in an integer list.
--doubleAll :: [Int] -> [Int]
--doubleAll [] = []
--doubleAll (h:t) = (2 * h) : doubleAll t
-- | Flip all of the boolean values in a boolean list.
notAll :: [Bool] -> [Bool]
notAll [] = []
notAll (h:t) = not h : notAll t
----------------------------
-- Higher-Order Functions --
----------------------------
-- * map and foldr
-- | Map a function over the elements in a list.
map :: (a -> b) -> [a] -> [b]
map f [] = []
map f (h:t) = f h : map f t
-- | Reimplement doubleAll using map.
doubleAll' :: [Int] -> [Int]
doubleAll' = map (2 *)
-- doubleAll' = map (\i -> 2 * i)
-- | Reimplement notAll using map.
notAll' :: [Bool] -> [Bool]
notAll' = map not
-- notAll' bs = map not bs
-- | Fold a function over the elements of a list.
simpleFold :: (a -> a -> a) -> a -> [a] -> a
simpleFold = undefined
-- | Reimplement sum using foldr.
sum' :: [Int] -> Int
sum' = undefined
-- | Reimplement product using foldr.
product' :: [Int] -> Int
product' = undefined
-- | Fold a function over the elements in a list, allowing the type of the
-- accumulated value to differ from the elements in the list.
foldr :: (a -> b -> b) -> b -> [a] -> b
foldr = undefined
--working on functions
f x y z = x + y + z
gain = print (f 3 4 5)