-
Notifications
You must be signed in to change notification settings - Fork 4
/
review1.hs
117 lines (73 loc) · 2.77 KB
/
review1.hs
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
-- e :: t evaluating expression e produces t
--every expression has a type, type inference is based
--all type errors are found at compile time
--safe high assurance code, plus its faster
-- :t caculates the type of an expression with evaluating it
{-
Bool - For logical values like true false statements
Char - single characters like strings
Sting - Strings of characters
Int - integer numbers
Float - floating point numbers
Lists: a sequence of values in square brackets separated by commas
[False,True,False] :: [Bool]
['a','b','c','d'] :: [Char]
We can have lists of lists
Tuple types: a sequence of values of possibly different types
(False,True) :: (Bool,Bool)
(False,'a',True) :: (Bool,Char,Bool)
use paranthesis for tuples
you can nest lists in tuples.
-}
-- FUNCTION TYPES --
-- A function is a mapping from values of one type to values of another type
{-
not :: Bool -> Bool
even :: Int -> Bool
-}
add :: (Int,Int) -> Int
add (x,y) = x+y
-- Curried Functions: these are functions with multiple arguments because they return functions as results
add' :: Int -> (Int -> Int)
add' x y = x+y
--the returned function adds x+Y as a result
--the parameters are taken one at a time, so they don't need brackets
--both functions take their arguments differently
--add' takes its arguments one at a time
--here is another curried function
mult :: Int -> (Int -> (Int -> Int))
mult x y z = x*y*z
--mult takes an integer x and returns a function mult x
--this takes an integer y and returns a function x y
--then it finally takes an integer z and returns x*y*z
--currying is helpful because you can make useful functions by partially applying them
--add' 1 :: Int -> Int increments by 1
-- -> arrows are right associative
-- function application bracketts to the left mult x y z ((mult x)y)z
-- Polymorphic functions has type that contains one or more type variables
--length :: [a] -> int
-- length is polymorphic becuase it can be applied to any typoe
{-
MANY OF THE FUNCTIONS IN HASKELL ARE POLYMORPHIC
fst :: (a,b) -> a Represents first
head :: [a] -> a returns the head of a list
take :: Int -> [a] -> [a] takes two parameters, an integer and a list, returning a list
zip :: [a] -> [b] -> [(a,b)] gives a list of pairs
id :: a -> a give the function something and it compies it
-}
-- OVERLOADED FUNCTIONS --
--polymorphic finction s is called overloaded when it contains one or more class constraints
-- (+) :: Num => a -> a -> a
--this is the type for the additon function
--the plus operator has type
--Num a must be a numberic type
-- the real a takes two types and gives one
-- 3 MAIN TYPE CLASSES --
-- Num - Numeric Types
-- Eq -Equality types
-- Ord - ordered types
{-
(+) :: Num => a -> a -> a
(==) :: Eq a => a -> a -> Bool
(<) :: Ord a => a -> a -> Bool
-}