Joseph Haugh
University of New Mexico
How do you sum the numbers from 1 to 10 in Java?
int s = 0;
for (int i = 0; i <= 10; i++) {
s = s + i;
}
How do you sum the numbers from 1 to 10 in Haskell?
sum [1..10]
What does the following code do?
f [] = []
f (x:xs) = f ys ++ [x] ++ f zs
where
ys = [ a | a <- xs, a <= x ]
zs = [ b | b <- xs, b > x ]
f [] = []
f (x:xs) = f ys ++ [x] ++ f zs
where
ys = [ a | a <- xs, a <= x ]
zs = [ b | b <- xs, b > x ]
[]
mean on its own?[1]
mean?f [] = []
f (x:xs) = f ys ++ [x] ++ f zs
where
ys = [ a | a <- xs, a <= x ]
zs = [ b | b <- xs, b > x ]
++
then appends the two lists togetherf [] = []
f (x:xs) = f ys ++ [x] ++ f zs
where
ys = [ a | a <- xs, a <= x ]
zs = [ b | b <- xs, b > x ]
Appending example:
[1,2,3] ++ [4,5,6] === [1,2,3,4,5,6]
What does this code do?
[1,2,3] ++ [] === ??
[] ++ [1,2,3] === ??
f [] = []
f (x:xs) = f ys ++ [x] ++ f zs
where
ys = [ a | a <- xs, a <= x ]
zs = [ b | b <- xs, b > x ]
Appending example:
[1,2,3] ++ [4,5,6] === [1,2,3,4,5,6]
What does this code do?
[1,2,3] ++ [] === [1,2,3]
[] ++ [1,2,3] === [1,2,3]
f [] = []
f (x:xs) = f ys ++ [x] ++ f zs
where
ys = [ a | a <- xs, a <= x ]
zs = [ b | b <- xs, b > x ]
What does : do?
It is an infix function of two arguments
The first argument must be a value and the second a list of those values
For example:
1 : [2,3,4] === [1,2,3,4]
f [] = []
f (x:xs) = f ys ++ [x] ++ f zs
where
ys = [ a | a <- xs, a <= x ]
zs = [ b | b <- xs, b > x ]
1 : [2] === ??
1 : [] === ??
f [] = []
f (x:xs) = f ys ++ [x] ++ f zs
where
ys = [ a | a <- xs, a <= x ]
zs = [ b | b <- xs, b > x ]
1 : [2] === [1,2]
1 : [] === [1]
f [] = []
f (x:xs) = f ys ++ [x] ++ f zs
where
ys = [ a | a <- xs, a <= x ]
zs = [ b | b <- xs, b > x ]
What does the < − do?
Does the whole expression [a|a < −xs, a < = x] remind of anything from math?
[ { a | a S a <= x === [ a | a <- xs, a <= x ] ]}
For example:
[x | x <- [1,2,3,4,5], x > 2] === [3,4,5]
f [] = []
f (x:xs) = f ys ++ [x] ++ f zs
where
ys = [ a | a <- xs, a <= x ]
zs = [ b | b <- xs, b > x ]
What does the < − do?
Does the whole expression [a|a < −xs, a < = x] remind of anything from math?
[ { a | a S a <= x === [ a | a <- xs, a <= x ] ]}
What does this code do?
[x | x <- [1,2,3,4,5], x > 4] === ??
[x | x <- [1,2,3,4,5]] === ??
f [] = []
f (x:xs) = f ys ++ [x] ++ f zs
where
ys = [ a | a <- xs, a <= x ]
zs = [ b | b <- xs, b > x ]
What does the < − do?
Does the whole expression [a|a < −xs, a < = x] remind of anything from math?
[ { a | a S a <= x === [ a | a <- xs, a <= x ] ]}
What does this code do?
[x | x <- [1,2,3,4,5], x > 4] === [5]
[x | x <- [1,2,3,4,5]] === [1,2,3,4,5]
f [] = []
f (x:xs) = f ys ++ [x] ++ f zs
where
ys = [ a | a <- xs, a <= x ]
zs = [ b | b <- xs, b > x ]
What is f [3,1,4,2]?
f [] = []
f (x:xs) = f ys ++ [x] ++ f zs
where
ys = [ a | a <- xs, a <= x ]
zs = [ b | b <- xs, b > x ]
f [] = []
f (x:xs) = f ys ++ [x] ++ f zs
where
ys = [ a | a <- xs, a <= x ]
zs = [ b | b <- xs, b > x ]