Lists 20-Sep-18.

Slides:



Advertisements
Similar presentations
Introduction A function is called higher-order if it takes a function as an argument or returns a function as a result. twice :: (a  a)  a  a twice.
Advertisements

7-Jun-14 Lists. Arrays and Lists Arrays are a fixed length and occupy sequential locations in memory This makes random access (for example, getting the.
Intro to Scala Lists. Scala Lists are always immutable. This means that a list in Scala, once created, will remain the same.
Lisp. Versions of LISP Lisp is an old language with many variants Lisp is alive and well today Most modern versions are based on Common Lisp LispWorks.
ML Lists.1 Standard ML Lists. ML Lists.2 Lists  A list is a finite sequence of elements. [3,5,9] ["a", "list" ] []  Elements may appear more than once.
A Third Look At ML 1. Outline More pattern matching Function values and anonymous functions Higher-order functions and currying Predefined higher-order.
Container Types in Python
Introduction to OCaml Slides prepared by Matt Gruskin Some material borrowed from the CIS 500 lecture notes.
F28PL1 Programming Languages Lecture 14: Standard ML 4.
CMSC 330: Organization of Programming Languages Tuples, Types, Conditionals and Recursion or “How many different OCaml topics can we cover in a single.
CHAPTER 4 AND 5 Section06: Sequences. General Description "Normal" variables x = 19  The name "x" is associated with a single value Sequence variables:
ML Lists.1 Standard ML Lists. ML Lists.2 Lists  A list is a finite sequence of elements. [3,5,9] ["a", "list" ] []  ML lists are immutable.  Elements.
1 Programming Languages and Paradigms Lisp Programming.
CS 116 Tutorial 2 Functional Abstraction. Reminders Assignment 2 is due this Wednesday at Noon.
0 PROGRAMMING IN HASKELL Chapter 7 - Higher-Order Functions.
Getting Functional. 2 What is Functional Programming (FP)? In FP, Functions are first-class objects. That is, they are values, just like other objects.
Clojure 3 Recursion, Higher-order-functions 27-Aug-15.
Pattern matching. The if expression The else part of an if expression is optional if ( condition ) expression1 else expression2 If the condition evaluates.
Fall Week 4 CSCI-141 Scott C. Johnson.  Computers can process text as well as numbers ◦ Example: a news agency might want to find all the articles.
Clojure 2 Feb 7,
Chapter 9: Functional Programming in a Typed Language.
A Second Look At ML 1. Outline Patterns Local variable definitions A sorting example 2.
Functions and Methods. Definitions and types A function is a piece of code that takes arguments and returns a result A pure function is a function whose.
A Third Look At ML Chapter NineModern Programming Languages, 2nd ed.1.
Lee CSCE 314 TAMU 1 CSCE 314 Programming Languages Haskell: Higher-order Functions Dr. Hyunyoung Lee.
Cases and Classes and Case Classes And Other Miscellany 16-Dec-15.
Monads. foo1 Method to print a string, then return its length: scala> def foo1(bar: String) = { | println(bar) | bar.size | } foo1: (bar: String)Int scala>
Introduction to Objective Caml. General comments ML is a purely functional language--there are (almost) no side effects There are two basic dialects of.
Chapter SevenModern Programming Languages1 A Second Look At ML.
0 PROGRAMMING IN HASKELL Based on lecture notes by Graham Hutton The book “Learn You a Haskell for Great Good” (and a few other sources) Odds and Ends,
Getting Functional. Object-Oriented Programming in Scala Scala is object-oriented, and is based on Java’s model An object is a singleton object (there.
Chapter 5: ARRAYS ARRAYS. Why Do We Need Arrays? Java Programming: From Problem Analysis to Program Design, 4e 2  We want to write a Java program that.
23-Feb-16 Lists. Arrays and Lists Arrays are a fixed length and occupy sequential locations in memory This makes random access (for example, getting the.
4-Mar-16 Getting Started with Scala. Getting Scala Download ScalaIDE: / This is a customized version of Eclipse I'm.
Haskell Chapter 5, Part II. Topics  Review/More Higher Order Functions  Lambda functions  Folds.
21-Mar-16 Getting Started with Scala. Hello World /** Everybody’s first program */ object HelloWorld { def main(args: Array[String]) { println("Hello,
Sequences and for loops. Simple for loops A for loop is used to do something with every element of a sequence scala> for (i
Lecture 7: Arrays Michael Hsu CSULA 3 Opening Problem Read one hundred numbers, compute their average, and find out how many numbers are above the average.
ML: a quasi-functional language with strong typing
Computer Programming Fundamentals
CMSC201 Computer Science I for Majors Lecture 21 – Dictionaries
Classes and Objects and Traits
A lightening tour in 45 minutes
Chapter 10 Lists.
Miscellaneous Items Loop control, block labels, unless/until, backwards syntax for “if” statements, split, join, substring, length, logical operators,
PROGRAMMING IN HASKELL
PROGRAMMING IN HASKELL
Getting Functional.
Exceptions and other things
PROGRAMMING IN HASKELL
PROGRAMMING IN HASKELL
Getting Started with Scala
Functions As Objects.
Chapter 10 Lists.
int [] scores = new int [10];
PROGRAMMING IN HASKELL
And why they are so useful
Getting Started with Scala
Cases and Classes and Case Classes
Getting Functional.
Arrays.
Higher Order Functions
Getting Started with Scala
15-110: Principles of Computing
Recursion, Higher-order-functions
Getting Started with Scala
PROGRAMMING IN HASKELL
Scala Apologia 27-Apr-19.
Lisp.
Monads.
Presentation transcript:

Lists 20-Sep-18

Arrays and Lists Arrays are a fixed length and occupy sequential locations in memory This makes random access (for example, getting the 37th element) very fast--O(1) Lists are composed of values linked together All access starts from the head (first element) and follows links Random access takes linear time a r t 0 1 2 myArray a r t 0 1 2 myList

Lists are immutable Lists, like Strings, are immutable Because all access is via the head, creating a “new” list is a fast operation p myLongerList a r t myList myShorterList • myLongerList looks like List("p", "a", "r", "t"); the "p" is not visible from myList • myShorterList looks like List("r", "t") • myList has not been changed--it is immutable

List operations Basic fast (constant time) operations list.head (or list head) returns the first element in the list list.tail (or list tail) returns a list with the first element removed value :: list returns a list with value appended to the front list.isEmpty (or list isEmpty ) tests whether the list is empty Some slow (linear time) operations list(i) returns the ith element (starting from 0) of the list list.last (or list last) returns the last element in the list list.init (or list init) returns a list with the last element removed This involves making a complete copy of the list list.length (or list length) returns the number of elements in the list list.reverse (or list reverse) returns a new list with the elements in reverse order In practice, the slow operations are hardly ever needed

Stepping through a list def printList1(myList: List[Any]) { for (i <- 0 until myList.length) { println(myList(i)) } } What is the time complexity of this method? def printList2(myList: List[Any]) { if(! myList.isEmpty) { // the dot is required here println(myList head) printList2(myList tail) } }

List construction with :: and Nil Lists are homogeneous: All elements have the same type However, scala> "abc" :: List(1, 2, 3) res15: List[Any] = List(abc, 1, 2, 3) The newly-created list has a type which is the least upper bound An empty list has “nothing” in it scala> List() res16: List[Nothing] = List() The “name” of the empty list is Nil scala> Nil res17: scala.collection.immutable.Nil.type = List() Lists are built from Nil and the :: operator (which is right-associative) scala> 1 :: 2 :: 3 :: Nil res18: List[Int] = List(1, 2, 3) scala> 1 :: (2 :: (3 :: Nil)) res19: List[Int] = List(1, 2, 3)

Basic recursion Recursion is when a method calls itself Here’s the basic formula for working with a list: if the list is empty return some initial value (often an empty list) else process the head recur with the tail def printList2(myList: List[Any]) { if(! myList.isEmpty) { println(myList head) printList2(myList tail) } }

Again, with pattern matching Here’s our same method again: def printList2(myList: List[Any]) { if(! myList.isEmpty) { println(myList head) printList2(myList tail) } } Here it is with pattern matching: def printList3(myList: List[Any]) { myList match { case h :: t => println(myList head) printList3(myList tail) case _ => } }

map The result list doesn’t have to be of the same type map applies a one-parameter function to every element of a List, returning a new List scala> List(1, 2, 3, 4) map (n => 10 * n) res0: List[Int] = List(10, 20, 30, 40) The result list doesn’t have to be of the same type scala> List(1, 2, 3, 4) map (n => n % 2 == 0) res1: List[Boolean] = List(false, true, false, true) Since an element of the list is the only parameter to the function, and it’s only used once, you can abbreviate the function scala> List(1, 2, 3, 4) map (10 * _ + 6) res2: List[Int] = List(16, 26, 36, 46) Of course, you don’t have to use a literal function; you can use any previously defined function (yours or Scala’s) scala> List(-1, 2, -3, 4) map (_ abs) res3: List[Int] = List(1, 2, 3, 4)

flatMap flatten “flattens” a list (removes one level of nesting) scala> val nested = List(List(1, 2, 3), List(4, 5)) nested: List[List[Int]] = List(List(1, 2, 3), List(4, 5)) scala> nested flatten res0: List[Int] = List(1, 2, 3, 4, 5) flatMap is like map, but the function given to flatMap is expected to return a list of values; the resultant list of lists is then “flattened” Syntax: def map[B](f: (A) => B): List[B] def flatMap[B](f: (A) => Traversable[B]): List[B] Example: scala> val greeting = List("Hello".toList, "from".toList, "Scala".toList) greeting: List[List[Char]] = List(List(H, e, l, l, o), List(f, r, o, m), List(S, c, a, l, a)) scala> greeting map (word => word.toList) res2: List[List[Char]] = List(List(H, e, l, l, o), List(f, r, o, m), List(S, c, a, l, a)) scala> greeting flatMap (word => word.toList) res3: List[Char] = List(H, e, l, l, o, f, r, o, m, S, c, a, l, a)

filter filter is used to remove unwanted elements from a list, returning a new list scala> List(1, -2, 3, -4) filter (_ > 0) res3: List[Int] = List(1, 3) There is a corresponding (less often used) filterNot method scala> List(1, -2, 3, -4) filterNot (_ > 0) res4: List[Int] = List(-2, -4)

foldl, foldr The “fold” functions apply a binary operator to the values in a list, pairwise, starting from the left or starting from the right scala> val list = List(10, 1, 2, 3) list: List[Int] = List(10, 1, 2, 3) scala> list.foldLeft(0)(_ - _) res3: Int = -16 scala> list.foldRight(0)(_ - _) res4: Int = 8 scala> ((((0 - 10) - 1) - 2) - 3) res6: Int = -16 scala> (10 - (1 - (2 - (3 - 0)))) res8: Int = 8

for Scala’s for comprehension can be used like Java’s for loop scala> for (ch <- "abcde") print(ch + "*") a*b*c*d*e* The ch <- "abcde" is a generator; you can have more than one scala> for { x <- 1 to 5 | y <- 10 to 30 by 10 } print((x + y) + " ") 11 21 31 12 22 32 13 23 33 14 24 34 15 25 35 The above needs braces, { }, not parentheses, ( ) You can have definitions (not the same as declarations): scala> for (i <- 1 to 10; | j = 100) print ((i + j) + " ") 101 102 103 104 105 106 107 108 109 110 j = 100 is a definition In this example, the semicolon preceding the definition is required You can also have guards: scala> for (i <- 1 to 10 | if i != 7) print(i + " ") 1 2 3 4 5 6 8 9 10

Another for example You need to start with a generator, and after that you can have more generators, definitions, and guards scala> for { i <- 1 to 5 if i % 2 == 0 | k = 100 | j <- 1 to 5 | if j * k < 450 } print((k + 10 * i + j) + " ") 121 122 123 124 141 142 143 144

for-yield The value of a for comprehension, without a yield, is () With a yield, the value is a list of results (one result for each time through the loop) The syntax is: for (sequence) yield expression Examples: scala> for (i <- 1 to 5) yield 10 * i res12: scala.collection.immutable.IndexedSeq[Int] = Vector(10, 20, 30, 40, 50) scala> for (n <- List("one", "two", "three")) yield n.substring(0, 2) res2: List[java.lang.String] = List(on, tw, th)

Another for-yield example Here’s a more complete example (Odersky, p. 125): val forLineLengths = for { file <- filesHere // ‘filesHere’ is an array of files if file.getName.endsWith(".scala") line <- fileLines(file) // get an Iterator[String] trimmed = line.trim if trimmed.matches(".*for.*") } yield trimmed.length // get an Array[Int] The above method: gets each file from an array of files considers only the file with the .scala extension gets an iterator for the lines in the file removes whitespace from the beginning and end of the line looks for “for” within the line (using a regular expression) counts the number of characters in the line returns an array of line lengths of lines containing “for” in scala files

toList scala> Array(1, 2, 3, 4) toList res12: List[Int] = List(1, 2, 3, 4) scala> "abc" toList res13: List[Char] = List(a, b, c) scala> Map("apple" -> "red", "banana" -> "yellow") toList res14: List[(java.lang.String, java.lang.String)] = List((apple,red), (banana,yellow)) scala> Set("abc", 123) toList res16: List[Any] = List(abc, 123) scala> List(1, 2, 3) toList res17: List[Int] = List(1, 2, 3) Also: toArray, toString, toSet, toMap

Pattern matching Given this definition: This works: scala> val myList = List("a", "b", "c") myList: List[java.lang.String] = List(a, b, c) This works: scala> val List(x, y, z) = myList x: java.lang.String = a y: java.lang.String = b z: java.lang.String = c But it’s pretty useless unless you know the exact number of items in the list Here’s a better way: scala> val hd :: tl = myList hd: java.lang.String = a tl: List[java.lang.String] = List(b, c)

Example program object EnglishToGerman { def main(args: Array[String]) { println(translate("Scala is a wonderful language !")) } def translate(english: String) = { val dictionary = Map("a" -> "ein", "is" -> "ist", "language" -> "Sprache", "wonderful" -> "wunderbar") def lookup(word: String) = { if (dictionary contains word) dictionary(word) else word } (english.split(" ") map (lookup(_))).mkString(" ") } } Output: Scala ist ein wunderbar Sprache !

The End