List are immutable Lists have recursive structure Lists are homogeneous

Similar documents
Linked lists and the List class

CompSci 220. Programming Methodology 12: Functional Data Structures

Classical Themes of Computer Science

Exercise 1: Graph coloring (10 points)

More Functions on Lists

Practically Functional. Daniel Spiewak

Shell CSCE 314 TAMU. Higher Order Functions

Standard prelude. Appendix A. A.1 Classes

CSCE 314 Programming Languages

Functional Programming

Functional Programming. Pure Functional Programming

F# - LISTS. In this method, you just specify a semicolon-delimited sequence of values in square brackets. For example

Outline. Collections Arrays vs. Lists Functions using Arrays/Lists Higher order functions using Arrays/Lists

An Introduction to Scala for Spark programming

Programming Languages 3. Definition and Proof by Induction

Strings are not collections, but they behave like them in almost every way. For example,

CS205: Scalable Software Systems

Programming Principles

CS 321 Programming Languages

Collections and Combinatorial Search

CSE 130, Fall 2006: Final Examination

Functional Programming

CS 457/557: Functional Languages

F# - ARRAYS. Arrays are fixed-size, zero-based, mutable collections of consecutive data elements that are all of the same type.

Comp 311 Functional Programming. Eric Allen, Two Sigma Investments Robert Corky Cartwright, Rice University Sagnak Tasirlar, Two Sigma Investments

CMSC 330: Organization of Programming Languages. Functional Programming with Lists

INF121: Functional Algorithmic and Programming

Funkcionalno programiranje. Liste

Higher-order operators. Higher-order operators. Scala. Higher-order operators. Java8. numerosi operatori higher-order sulle collezioni

Shell CSCE 314 TAMU. Functions continued

A tour of the Haskell Prelude

Overview. Elements of Programming Languages. Advanced constructs. Motivating inner class example

PROGRAMMING IN HASKELL. CS Chapter 6 - Recursive Functions

CSc 520 Principles of Programming Languages. Currying Revisited... Currying Revisited. 16: Haskell Higher-Order Functions

CSc 372. Comparative Programming Languages. 11 : Haskell Higher-Order Functions. Department of Computer Science University of Arizona

EDAF40. 2nd June :00-19:00. WRITE ONLY ON ONE SIDE OF THE PAPER - the exams will be scanned in and only the front/ odd pages will be read.

# true;; - : bool = true. # false;; - : bool = false 9/10/ // = {s (5, "hi", 3.2), c 4, a 1, b 5} 9/10/2017 4

List Processing in Ocaml

School of Computer Science

Sorting: Given a list A with n elements possessing a total order, return a list with the same elements in non-decreasing order.

CMSC 330: Organization of Programming Languages. Functional Programming with Lists

INTRODUCTION TO HASKELL

List Processing in SML

Download the forcomp.zip handout archive file and extract it somewhere on your machine.

Life in a Post-Functional World

CS 2340 Objects and Design - Scala

List Processing in SML

Functional programming

Lecture 2: List algorithms using recursion and list comprehensions

High Wizardry in the Land of Scala. Daniel Spiewak

Topic 5: Examples and higher-order functions

CPL 2016, week 10. Clojure functional core. Oleg Batrashev. April 11, Institute of Computer Science, Tartu, Estonia

Notes from a Short Introductory Lecture on Scala (Based on Programming in Scala, 2nd Ed.)

Booleans (aka Truth Values) Programming Languages and Compilers (CS 421) Booleans and Short-Circuit Evaluation. Tuples as Values.

Functional Programming TDA 452, DIT 142

CS 135 Winter 2018 Tutorial 7: Accumulative Recursion and Binary Trees. CS 135 Winter 2018 Tutorial 7: Accumulative Recursion and Binary Trees 1

n n Official Scala website n Scala API n

Announcements. CSCI 334: Principles of Programming Languages. Lecture 16: Intro to Scala. Announcements. Squeak demo. Instructor: Dan Barowy

A Second Look At ML. Chapter Seven Modern Programming Languages, 2nd ed. 1

CS 320: Concepts of Programming Languages

Using Scala in CS241

GADTs. Wouter Swierstra and Alejandro Serrano. Advanced functional programming - Lecture 7. [Faculty of Science Information and Computing Sciences]

Advanced Object-Oriented Languages Scala

List Processing Patterns and List Comprehension

Haskell Introduction Lists Other Structures Data Structures. Haskell Introduction. Mark Snyder

GADTs. Alejandro Serrano. AFP Summer School. [Faculty of Science Information and Computing Sciences]

CS558 Programming Languages

Efficient Mergesort. Christian Sternagel. October 11, 2017

How to Design Programs

Programming Principles

Programming Languages and Compilers (CS 421)

(ii) Define a function ulh that takes a list xs, and pairs each element with all other elements in xs.

Examples of Using the ARGV Array. Loop Control Operators. Next Operator. Last Operator. Perl has three loop control operators.

GADTs. Wouter Swierstra. Advanced functional programming - Lecture 7. Faculty of Science Information and Computing Sciences

Functional Programming TDA 452, DIT 142

Efficient Mergesort. Christian Sternagel. August 28, 2014

High-level Parallel Programming: Scala on the JVM

A general introduction to Functional Programming using Haskell

// Body of shortestlists starts here y flatmap { case Nil => Nil case x :: xs => findshortest(list(x), xs)

1 The smallest free number

Summer 2017 Discussion 10: July 25, Introduction. 2 Primitives and Define

General Computer Science (CH ) Fall 2016 SML Tutorial: Practice Problems

Bibliography. Analyse et Conception Formelle. Lesson 5. Crash Course on Scala. Scala in a nutshell. Outline

Lecture 10 September 11, 2017

FUNCTIONAL PROGRAMMING (1) PROF. SIMON PARSONS

Parallel Operations on Collections. Parallel Programming in Scala Viktor Kuncak

List Functions, and Higher-Order Functions

CSC324- TUTORIAL 5. Shems Saleh* *Some slides inspired by/based on Afsaneh Fazly s slides

Note that pcall can be implemented using futures. That is, instead of. we can use

Homework 3 COSE212, Fall 2018

Introduction to SML Basic Types, Tuples, Lists, Trees and Higher-Order Functions

Parametricity. Leo White. February Jane Street 1/ 70

Visual C# 2012 How to Program by Pe ars on Ed uc ati on, Inc. All Ri ght s Re ser ve d.

Scheme. Functional Programming. Lambda Calculus. CSC 4101: Programming Languages 1. Textbook, Sections , 13.7

Informatics 1 Functional Programming Lecture 4. Lists and Recursion. Don Sannella University of Edinburgh

CSCE 314 TAMU Fall CSCE 314: Programming Languages Dr. Flemming Andersen. Haskell Functions

CSci 4223 Lecture 12 March 4, 2013 Topics: Lexical scope, dynamic scope, closures

Scala Style Guide Spring 2018

Ozyegin University CS 321 Programming Languages Sample Problems 05

CS 340 Spring 2019 Midterm Exam

Transcription:

WORKING WITH LISTS

val fruit = List("apples", "oranges", "pears") val nums: List[Int] = List(1, 2, 3, 4) val diag3 = List( List(1, 0, 0), List(0, 1, 0), List(0, 0, 1) ) val empty = List() List are immutable Lists have recursive structure Lists are homogeneous

CONSTRUCTING LISTS val fruit = "apples" :: ("oranges" :: ("pears" :: Nil)) val nums =1::(2::(3::(4::Nil))) val diag3 = (1 :: (0 :: (0 :: Nil))) :: (0 :: (1 :: (0 :: Nil))) :: (0 :: (0 :: (1 :: Nil))) :: Nil val empty = Nil Nil represents the empty list :: ( cons ) extends list at the front :: is right associative => A :: B :: C = A :: (B :: C)

BASIC OPERATIONS head tail.head tail 1 2 3 4 5 init init.last last isempty returns true if the list is empty head returns the first element tail returns everything but the first element init returns everything but the last element last returns the last element

INSERTION SORT def isort(xs: List[Int]): List[Int] = if (xs.isempty) Nil else insert(xs.head, isort(xs.tail)) def insert(x: Int, xs: List[Int]): List[Int] = if (xs.isempty x <= xs.head) x :: xs else xs.head :: insert(x, xs.tail)

LIST PATTERNS val nums: List = List(1, 2, 3, 4, 5) val a :: b :: rest = nums 1 2 3 4 5 a b rest a: Int = 1 b: Int = 2 rest: List[Int] = List(3, 4, 5)

LIST PATTERNS 1 2 3 4 5 first third rest val nums: List = List(1, 2, 3, 4, 5) val first :: _ :: third :: rest = nums first: Int = 1 third: Int = 3 rest: List[Int] = List(4, 5)

LIST CONCATENATION val a = List(1, 2, 3) val b = List(4, 5, 6) val c = a :: b c: List[Any] = List(List(1, 2, 3), 4, 5, 6) val d = a ::: b d: List[Int] = List(1, 2, 3, 4, 5, 6)

LIST CONCATENATION def append[t](xs: List[T], ys: List[T]): List[T] = xs match { case List() => ys case x :: xs1 => x :: append(xs1, ys) }

COMMON LIST OPERATIONS length reverse drop take splitat flatten zip unzip returns the number of elements in the list returns a reversed version of the list returns everything but the first n elements returns the first n elements splits a list into two lists at a specified index takes a list of lists and flattens it out to a single list combines two lists into a list of pairs splits a list of pairs into two lists

LIST CONVERSIONS tostring mkstring toarray iterator returns a string representation of the list returns a string representation with prefix, seperator and postfix converts the list to an array creates an iterator for the list

HIGHER-ORDER LIST METHODS: MAP applies a specified function to each element of a list val alph = ('a' to 'z').tolist alph: List[Char] = List(a, b, c, d, e,, v, w, x, y, z) val alphup = alph.map(_.toupper) alphup: List[Char] = List(A, B, C, D, E,, V, W, X, Y, Z)

HIGHER-ORDER LIST METHODS: FLATMAP applies a specified function that returns a list to each element of the list and flattens (concatenates) the result to one single list val words = List("the", "quick", "brown", fox") words map (_.tolist) List[List[Char]] = List(List(t, h, e), List(q, u, i, c, k), List(b, r, o, w, n), List(f, o, x)) words flatmap (_.tolist) List[Char] = List(t, h, e, q, u, i, c, k, b, r, o, w, n, f, o, x)

HIGHER-ORDER LIST METHODS: FOREACH applies a specified procedure to each element of a list var sum = 0 sum: Int = 0 List(1, 2, 3, 4, 5) foreach (sum += _) sum Int = 15

HIGHER-ORDER LIST METHODS: FILTER, PARTITION filters/partition a list according to a predicate function List(1, 2, 3, 4, 5) filter (_ % 2 == 0) List[Int] = List(2, 4) List(1, 2, 3, 4, 5) partition (_ % 2 == 0) (List[Int], List[Int]) = (List(2, 4), List(1, 3, 5))

HIGHER-ORDER LIST METHODS: FIND find the first element satisfying a predicate function List(1, 2, 3, 4, 5) find (_ % 2 == 0) Option[Int] = Some(2)

HIGHER-ORDER LIST METHODS: DROPWHILE, TAKEWHILE, SPAN take/drop elements as long as they satisfy the predicate function List(1, 2, 3, 4, 5) takewhile (_ <= 2) List[Int] = List(1, 2) List(1, 2, 3, 4, 5) dropwhile (_ <= 2) List[Int] = List(3, 4, 5) List(1, 2, 3, 4, 5) span (_ <= 2) (List[Int], List[Int]) = (List(1, 2), List(3, 4, 5))

HIGHER-ORDER LIST METHODS: FORALL, EXISTS check if every/an element satisfies the predicate function List(1, 2, 3, 4, 5) forall (_ % 2 == 0) Boolean = false List(2, 4, 6, 8) forall (_ % 2 == 0) Boolean = true List(1, 2, 3, 4, 5) exits (_ % 2 == 0) Boolean = true

HIGHER-ORDER LIST METHODS: FOLDLEFT (/:), FOLDRIGHT (:\) combine elements with an operator op op c (0 /: List(1, 2, 3, 4, 5)) (_ + _) Int = 15 z op a b op (List(1, 2, 5, 3, 4) :\ 0) (_ max _) Int = 5 a b op op c z

HIGHER-ORDER LIST METHODS: SORTWITH sorts list according to a function List(1, -3, 4, 2, 6) sortwith (_ < _) List[Int] = List(-3, 1, 2, 4, 6) List("the", "quick", "brown", "fox") sortwith (_.length > _.length) List[java.lang.String] = List(quick, brown, the, fox)

HIGHER-ORDER LIST METHODS: FILL, TABULATE fill a list with elements List.fill(5)("x") List[String] = List(x, x, x, x, x) List.tabulate(5)(n => "x" * (n+1)) List[String] = List(x, xx, xxx, xxxx, xxxxx)