CSC302 2011S Programming Languages
[Skip to Body]
Admin:
[Front Door]
[Schedule]
[Handouts]
[Honesty]
[Piazzza]
Current:
[Current Outline]
[Current EBoard]
[Current Assignment]
[Current Lab]
[Current Reading]
Groupings:
[Assignments]
[EBoards]
[Examples]
[Exams]
[Handouts]
[Labs]
[Outlines]
[Readings]
[Reference]
Languages:
[Clojure]
[Erlang]
[Haskell]
[Io]
[Prolog (GNU)]
[Ruby]
[Scala]
Misc:
[SamR]
[CSC302 2007S]
[7L7W]
Summary: We continue our exploration of the Scala programming language, focusing on its collections and some of its functional aspects.
Prerequisites: The first Scala lab.. Tate, Section 5.3.
Contents:
a. Create a directory for the lab.
b. Open a browser window on Tate's examples, in case you want to try any of them.
a. Consider the following function that sums a pair of integers (taken as a pair).
def sump(p: (Int,Int)): Int = p._1 + p._2
Verify that the function works as one would expect.
b. Consider the following function that builds a pair.
def pair(x: Any): (Any,Any) = (x,x)
Verify that it works as you might expect.
c. What do you expect the result of the following to be?
scala> sump(pair(2))
d. Check your answer experimentally.
e. As you may have discovered, we get a type error because pair
returns a pair of Any
values, rather than a pair in which each
element is the same type as the parameter. Fortunately, scala lets us
add type parameters to functions by putting them in brackets
after the function name.
def pair[A](x: A): (A,A) = (x,x)
Verify that this solves the problem you just encountered.
As you may recall, the composition operator is useful for higher-order programming. Write that operator.
Test it with combinations of the following functions
def len(x: scala.runtime.RichString) = x.length def rev(x: scala.runtime.RichString) = x.reverse def inc(x: Int) = x + 1 def sqr(x: Int) = x * x def one(x: Any) = 1
As you may recall, the Map type in scala provides
immutable maps (tables, hashes, dictionaries, whatever you want
to call the ADT).
scala> var x = Map("A" -> 1)
x: scala.collection.immutable.Map[java.lang.String,Int] = Map(A -> 1)
a. Repeat the definition above.
b. What do you expect the effect of each of the following to be?
scala> x("A")
scala> x("B")
c. Check your answer experimentally.
d. What do you expect the result of the following to be?
scala> var y = Map("A" -> 1) + ("B" -> 2)
e. Check your answer experimentally.
f. What do you expect the result of the following to be?
scala> x += ("B" -> 2)
g. Check your answer experimentally.
h. Explain the difference between the two results.
i. What do you expect the result of the following to be?
scala> x += ("B" -> 3)
j. Check your answer experimentally.
k. What do you expect the result of the following to be?
scala> x += (3 -> "B")
l. Check your answer experimentally.
m. Figure out how to create an empty Map that lets you use any kind of index.
As you know from implementing hash tables, in the most common implementation, when the hash table gains too many elements, we build a new table and rehash all of the elements.
Determine experimentally when (or if) that reordering happens in a Scala Map.
These problems are based on exercises in Tate.
a. Using foldLeft, find the total length of all the strings
in a list of strings.
b. Using foldLeft, find a longest string in a list of
strings.
c. Using foldLeft, find the alphabetically first string
in a list of strings.
This problem is based on an exercise from Tate.
Write a TLA trait for strings that expands some common
three-letter-acronyms. E.g., "TLA" -> "Three Letter Acronym",
"DEC" -> "Digital Equipment Corportation", "FOO" -> "Fun Object
Orientation".
If you find yourself with extra time, start working on Assignment 5.
Sunday, 20 February 2011 [Samuel A. Rebelsky]
Monday, 21 February 2011 [Samuel A. Rebelsky]
http://www.cs.grinnell.edu/~rebelsky/Courses/CSC302/2011S/Labs/scala-2.html.
[Skip to Body]
Admin:
[Front Door]
[Schedule]
[Handouts]
[Honesty]
[Piazzza]
Current:
[Current Outline]
[Current EBoard]
[Current Assignment]
[Current Lab]
[Current Reading]
Groupings:
[Assignments]
[EBoards]
[Examples]
[Exams]
[Handouts]
[Labs]
[Outlines]
[Readings]
[Reference]
Languages:
[Clojure]
[Erlang]
[Haskell]
[Io]
[Prolog (GNU)]
[Ruby]
[Scala]
Misc:
[SamR]
[CSC302 2007S]
[7L7W]
Disclaimer:
I usually create these pages on the fly
, which means that I rarely
proofread them and they may contain bad grammar and incorrect details.
It also means that I tend to update them regularly (see the history for
more details). Feel free to contact me with any suggestions for changes.
This document was generated by
Siteweaver on Mon Apr 25 08:06:54 2011.
The source to the document was last modified on Mon Feb 21 09:26:25 2011.
This document may be found at http://www.cs.grinnell.edu/~rebelsky/Courses/CSC302/2011S/Labs/scala-2.html.
A PDF version of this document may be found at
http://www.cs.grinnell.edu/~rebelsky/Courses/CSC302/2011S/Labs/scala-2.pdf
You may wish to
validate this document's HTML
;
;