Copyright © Cay S. Horstmann 2015
This work is licensed under a Creative Commons Attribution 4.0 International License
def
val
to define functions
val triple = (x: Double) => 3 * x
def triple(x: Double) = 3 * x
def
isn't followed by =
, you are defining a function returning ()
def triple(x: Double) { 3 * x } // Returns ()!
def fac(n: Int):Int = ...
def sumInts(a: Int, b: Int): Int = a.to(b).reduce(_+_)
def square(x: Int): Int = x * x def sumSquares(a: Int, b: Int): Int = a.to(b).map(square).reduce(_+_)
def cube(x: Int): Int = x * x * x def sumCubes(a: Int, b: Int): Int = a.to(b).map(cube).reduce(_+_)
def sum(f: Int => Int, a: Int, b: Int): Int = a.to(b).map(f).reduce(_+_)
def sumInts(a: Int, b: Int) = sum(id, a, b) def sumSquares(a: Int, b: Int) = sum(squares, a, b) def sumCubes(a: Int, b: Int) = sum(cube, a, b)
def id(x: Int): Int = x def square(x: Int): Int = x * x def cube(x: Int): Int = x * x * x
def sum(f: Int => Int): (Int, Int) => Int = (a: Int, b: Int) =>a.to(b).map(f).reduce(_+_)
sum(square)(1,4)
sum
must be a paire of IntssumSquares(1,4)
def sumInts = sum(id) def sumSquares = sum(square)
def sumCubes = sum(cube)
sumCubes(1, 10) + sumSquares(10, 20)
sum (cube) (1, 10)
sum(cube)(1, 10) == (sum (cube)) (1, 10)
Seq[A].corresponds(t: Seq[B])(p: (A, B) => Boolean)
checks whether two sequences have “corresponding” elements, as defined by p
val s = "Mary had a little lamb".split(" ") val t = Array(4, 3, 1, 6, 4) s.corresponds(t)((a: String, b: Int) => a.length == b)
s.corresponds(t)(_.length == _)
s.corresponds(t)(_.length == _)
t
is needed to infer the type B
Vector(a, b, c)
is
a * b * c = 1 * a * b * c = ((1 * a) * b) * c
foldLeft
:
def prod(lst: Vector[Int]) = lst.foldLeft(1) (_ * _)
* / \ * c / \ * b / \ 1 a
foldRight
operator works right-to-left: a * (b * (c * 1))
1.to(10).foldLeft("")(_ + _)
"12345678910"
"10987654321"
reduce
reduce
only works with operators (A, A) => A
foldLeft
works with any operator (B, A) => B
augment(augment(augment(Vector(Vector("1")), '2'), '3'), '4')
reduce
because augment
takes arguments of different types
val augment = (a: Vector[Vector[String]], t: Char) => ...
foldLeft
—see lab`f(n) = {(s , if n = 0),(t(f(n - 1), n) , if n > 0):}`
can be computed with a fold.1.to(n).foldLeft(s)(t)
`n! = {(1 , if n = 0),((n - 1)! * n , if n > 0):}`
`S(n) = {({} , if n = 0),(S(n - 1) ∪ { s ∪ {n} | s ∈ S(n - 1) } , if n > 0):}`
"Mississippi"
→ Map(M -> 1, i -> 4, s -> 4, p -> 2)
. . . op / \ op 's' / \ op 'i' / \ empty map 'M'
op
?
(m, c) => m + (c -> (m.getOrElse(c, 0) + 1))
def max(lst : Seq[String], less : (String, String) => Boolean) = lst.reduce((x, y) => if (less(x, y)) y else x)
Make a call to max
that yields the longest string in a sequence of strings. Important: Use _
for the string parameters in your less
function.
def max[T](lst : Seq[T], less : (T, T) => Boolean) = lst.reduce((x, y) => if (less(x, y)) y else x)
What happens when you call max(lst, _ < _)?
max[T]
function, exactly like mul2
above. What is the code for your revised function? What happens when you call max2(lst)(_ < _)
? _
parameters?val fac = (n: Int) => ...
subsets(2)
to be Vector(Vector(), Vector(1), Vector(2), Vector(1, 2))
. How do you get that out of subsets(1)
, i.e. Vector(Vector(), Vector(1))
?s
and an integer n
, and you want s
together with the sequence obtained by adding n
to all elements of s
. Write a function that does that, and test it.subsets
by using foldLeft
.subsets
as a recursive function. Try it!letterFrequencies
function from the slide 12. What do you get for letterFrequencies("Mississippi")
?augment
to get all ways of breaking up a string into substrings. For example, here are all substrings of "1234"
augment(augment(augment(Vector(Vector("1")), '2'), '3'), '4')Get last week's worksheet from your buddy and copy over all you need to today's worksheet.
"1234"
. Of course, we want to use foldLeft
with augment
. What is the seed value? And to what sequence do you apply foldLeft
?val substrings = (s: String) => ...
. What is substrings("2728")
?Vector[String]
. The shining achievement of last week's lab was the function wordsForDigitSequence
that found all words for such a sequence. What do you get when you call
substrings("2728").map(wordsForDigitsSequence)
val phoneMnemonics = (digits: String) => ...
that does this for any string of digits, not just "2728"
. What is phoneMnemonics("7225247386")
? (You can change the cutoff limit of the worksheet in the preferences to see all solutions.)