Programming Languages Dan Grossman 2013

Slides:



Advertisements
Similar presentations
Haskell Lets review some of the Haskell concepts you have been learning on your own. The answers are included, but try it yourself first.
Advertisements

Modern Programming Languages, 2nd ed.
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.
CSE341: Programming Languages Lecture 2 Functions, Pairs, Lists Dan Grossman Winter 2013.
void count_down (int count) { for(i=count; i>1; i--) printf(" %d\t", count); } printf("A%d\n", count); if(count>1) count_down(count-1); printf("B%d\n",
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.
Programming Languages Section 1 1 Programming Languages Section 1. SML Fundamentals Xiaojuan Cai Spring 2015.
Exercises – don’t use built in functions for these as we want the practice Write a recursive function to add up all the numbers in a list "flatten" a list.
CSE341: Programming Languages Lecture 5 Pattern-Matching Dan Grossman Fall 2011.
CSE341: Programming Languages Lecture 2 Functions, Pairs, Lists Dan Grossman Fall 2011.
Comp 205: Comparative Programming Languages Functional Programming Languages: More Lists Recursive definitions List comprehensions Lecture notes, exercises,
Functional Programming Element of Functional Programming.
A Second Look At ML 1. Outline Patterns Local variable definitions A sorting example 2.
CSE 341 : Programming Languages Lecture 2 Functions, Pairs, Lists Zach Tatlock Spring 2014.
Chapter SevenModern Programming Languages1 A Second Look At ML.
Haskell Chapter 4. Recursion  Like other languages  Base case  Recursive call  Author programs a number of built-in functions as examples.
Int fact (int n) { If (n == 0) return 1; else return n * fact (n – 1); } 5 void main () { Int Sum; : Sum = fact (5); : } Factorial Program Using Recursion.
© M. Winter COSC 4P41 – Functional Programming Some functions id :: a -> a id x = x const :: a -> b -> a const k _ = k ($) :: (a -> b) -> a -> b.
Recursion Recursion is a fundamental programming technique that can provide an elegant solution certain kinds of problems © 2004 Pearson Addison-Wesley.
CSE341: Programming Languages Lecture 3 Local Bindings; Options; Benefits of No Mutation Dan Grossman Winter 2013.
CS 550 Programming Languages Jeremy Johnson
Functional Programming Lecture 12 - more higher order functions
Programming Languages Dan Grossman 2013
CSE341: Programming Languages Lecture 3 Local Bindings; Options; Benefits of No Mutation Dan Grossman Spring 2017.
Programming Languages Dan Grossman 2013
CSE341: Programming Languages Lecture 6 Nested Patterns Exceptions Tail Recursion Dan Grossman Spring 2017.
Programming Languages Dan Grossman 2013
ML Again ( Chapter 7) Patterns Local variable definitions
Programming Languages Dan Grossman 2013
CSE341: Programming Languages Lecture 2 Functions, Pairs, Lists
Programming Languages Dan Grossman 2013
Programming Languages Dan Grossman 2013
PROGRAMMING IN HASKELL
PROGRAMMING IN HASKELL
PROGRAMMING IN HASKELL
PROGRAMMING IN HASKELL
CS 270 Math Foundations of CS Jeremy Johnson
CSE341: Programming Languages Lecture 5 More Datatypes and Pattern-Matching Dan Grossman Autumn 2017.
CSE341: Programming Languages Lecture 5 More Datatypes and Pattern-Matching Dan Grossman Spring 2016.
CSE341: Programming Languages Lecture 5 More Datatypes and Pattern-Matching Dan Grossman Winter 2013.
CSE341: Programming Languages Lecture 5 More Datatypes and Pattern-Matching Dan Grossman Spring 2013.
CSE341: Programming Languages Lecture 3 Local Bindings; Options; Benefits of No Mutation Dan Grossman Spring 2013.
CSE341: Programming Languages Lecture 3 Local Bindings; Options; Benefits of No Mutation Dan Grossman Spring 2016.
PROGRAMMING IN HASKELL
CSE341: Programming Languages Lecture 6 Nested Patterns Exceptions Tail Recursion Dan Grossman Spring 2013.
CSE341: Programming Languages Lecture 2 Functions, Pairs, Lists
CSE341: Programming Languages Lecture 2 Functions, Pairs, Lists
CSE341: Programming Languages Lecture 2 Functions, Pairs, Lists
CSE341: Programming Languages Lecture 6 Nested Patterns Exceptions Tail Recursion Dan Grossman Autumn 2018.
CSE341: Programming Languages Lecture 3 Local Bindings; Options; Benefits of No Mutation Dan Grossman Autumn 2018.
CSE341: Programming Languages Lecture 2 Functions, Pairs, Lists
CSE341: Programming Languages Lecture 5 More Datatypes and Pattern-Matching Dan Grossman Autumn 2018.
CSE341: Programming Languages Lecture 5 More Datatypes and Pattern-Matching Dan Grossman Spring 2017.
CSE341: Programming Languages Section 3 Function Patterns Tail Recursion Winter 2018.
CSE341: Programming Languages Lecture 3 Local Bindings; Options; Benefits of No Mutation Dan Grossman Winter 2018.
Higher Order Functions
PROGRAMMING IN HASKELL
Madhusudan Parthasarathy
CSCE 314: Programming Languages Dr. Dylan Shell
Programming Languages Dan Grossman 2013
CSE341: Programming Languages Lecture 2 Functions, Pairs, Lists
CSE341: Programming Languages Lecture 3 Local Bindings; Options; Benefits of No Mutation Dan Grossman Spring 2019.
CSE341: Programming Languages Lecture 3 Local Bindings; Options; Benefits of No Mutation Dan Grossman Autumn 2017.
CSE341: Programming Languages Lecture 6 Nested Patterns Exceptions Tail Recursion Dan Grossman Spring 2019.
CSE341: Programming Languages Lecture 2 Functions, Pairs, Lists
CSE341: Programming Languages Lecture 5 More Datatypes and Pattern-Matching Dan Grossman Spring 2019.
Brett Wortzman Summer 2019 Slides originally created by Dan Grossman
Brett Wortzman Summer 2019 Slides originally created by Dan Grossman
Brett Wortzman Summer 2019 Slides originally created by Dan Grossman
CSE341: Programming Languages Lecture 6 Nested Patterns Exceptions Tail Recursion Dan Grossman Autumn 2017.
Presentation transcript:

Programming Languages Dan Grossman 2013 List Functions

Dan Grossman, Programming Languages Functions over lists Gain experience with lists and recursion by writing several functions that process and/or produce lists… Jan-Mar 2013 Dan Grossman, Programming Languages

Example list functions fun sum_list (xs : int list) = if null xs then 0 else hd(xs) + sum_list(tl(xs)) fun countdown (x : int) = if x=0 then [] else x :: countdown (x-1) fun append (xs : int list, ys : int list) = then ys else hd (xs) :: append (tl(xs), ys) Jan-Mar 2013 Dan Grossman, Programming Languages

Dan Grossman, Programming Languages Recursion again Functions over lists are usually recursive Only way to “get to all the elements” What should the answer be for the empty list? What should the answer be for a non-empty list? Typically in terms of the answer for the tail of the list! Similarly, functions that produce lists of potentially any size will be recursive You create a list out of smaller lists Jan-Mar 2013 Dan Grossman, Programming Languages

Dan Grossman, Programming Languages Lists of pairs Processing lists of pairs requires no new features. Examples: fun sum_pair_list (xs : (int*int) list) = if null xs then 0 else #1(hd xs) + #2(hd xs) + sum_pair_list(tl xs) fun firsts (xs : (int*int) list) = then [] else #1(hd xs) :: firsts(tl xs) fun seconds (xs : (int*int) list) = else #2(hd xs) :: seconds(tl xs) fun sum_pair_list2 (xs : (int*int) list) = (sum_list (firsts xs)) + (sum_list (seconds xs)) Jan-Mar 2013 Dan Grossman, Programming Languages