CS 116 Tutorial 2 Functional Abstraction. Reminders Assignment 2 is due this Wednesday at Noon.

Slides:



Advertisements
Similar presentations
מבוא מורחב למדעי המחשב בשפת Scheme תרגול 5. 2 List Utilities Scheme built-in procedures –(list x y z...) –(list-ref lst index) –(length lst) –(append.
Advertisements

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.
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.
F28PL1 Programming Languages Lecture 14: Standard ML 4.
Chapter 3 DATA: TYPES, CLASSES, AND OBJECTS. Chapter 3 Data Abstraction Abstract data types allow you to work with data without concern for how the data.
Functional Programming. Pure Functional Programming Computation is largely performed by applying functions to values. The value of an expression depends.
1 Programming Languages and Paradigms Lisp Programming.
CSE 341 Lecture 16 More Scheme: lists; helpers; let/let*; higher-order functions; lambdas slides created by Marty Stepp
Cs1120 Fall 2009 David Evans Lecture 16: Power Analysis.
String and Lists Dr. Benito Mendoza. 2 Outline What is a string String operations Traversing strings String slices What is a list Traversing a list List.
0 PROGRAMMING IN HASKELL Chapter 7 - Higher-Order Functions.
Loops in Scheme, II c. Kathi Fisler, 2001 (early slides assume map/filter)
CS 330 Programming Languages 11 / 20 / 2007 Instructor: Michael Eckmann.
CS 330 Programming Languages 11 / 18 / 2008 Instructor: Michael Eckmann.
מבוא מורחב - שיעור 81 Lecture 8 Lists and list operations (continue).
CS 3 Final Review Gilbert Chou, Jenny Franco and Colleen Lewis December 14, pm GPB.
Recursion in Scheme recursion is the basic means for expressing repetition some recursion is on numbers –factorial –fibonacci some recursion is on structures.
Ormap, andmap, and filter CS 5010 Program Design Paradigms “Bootcamp” Lesson 5.3 TexPoint fonts used in EMF. Read the TexPoint manual before you delete.
Rewriting your function using map and foldr CS 5010 Program Design Paradigms “Bootcamp” Lesson 5.5 TexPoint fonts used in EMF. Read the TexPoint manual.
Comp. Eng. SW Lab II: FP with Scheme 1 Computer Eng. Software Lab II , Semester 2, Who I am: Andrew Davison CoE, WiG Lab Office.
Slide 1 Vitaly Shmatikov CS 345 Introduction to Scheme.
Lecture 2-1CS250: Intro to AI/Lisp Intelligent Agents Lecture 3-2 October 14 th, 1999 CS250.
0 REVIEW OF HASKELL A lightening tour in 45 minutes.
COP4020 Programming Languages Functional Programming Prof. Xin Yuan.
Lecture 6-2CS250: Intro to AI/Lisp Programming in Your Favorite Language Lecture 5-2 February 11 th, 1999 CS250.
Rewriting your function using map and foldr CS 5010 Program Design Paradigms “Bootcamp” Lesson TexPoint fonts used in EMF. Read the TexPoint manual.
Lee CSCE 314 TAMU 1 CSCE 314 Programming Languages Haskell: Higher-order Functions Dr. Hyunyoung Lee.
TUTORIAL 8 Generative Recursion. Reminders  Deadline of midterm remark  Assignment 7 due Wed, Mar 18 th.
CS116 Tutorial 1 Review of CS115. Reminders Assignment 1 is due Wednesday, January 21th at 12pm (Noon) Submit Assignment 0 if you have not done so already.
Cs1321 December 6, 2001 Review. What is computer science? What's an algorithm? Processes and programs Overview of some programming language concepts Functional.
Tutorial 9 Iteration. Reminder Assignment 8 is due Wednesday.
Tutorial 10 Dictionaries and Classes. Reminder Assignment 09 is due FRIDAY, Dec 4 at 4:00 pm.
1 FP Foundations, Scheme In Text: Chapter Chapter 14: FP Foundations, Scheme Mathematical Functions Def: A mathematical function is a mapping of.
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,
Recursion Higher Order Functions CSCE 314 Spring 2016.
Forms Writing your own procedures CS 480/680 – Comparative Languages.
Spring 16 CSCI 4430, A Milanova/BG Ryder 1 Announcements HW5 due March 28 Homework Server link is up I will have office hours, Fri or Mon, check Announcements.
String and Lists Dr. José M. Reyes Álamo. 2 Outline What is a string String operations Traversing strings String slices What is a list Traversing a list.
CS336 F07 Counting 2. Example Consider integers in the set {1, 2, 3, …, 1000}. How many are divisible by either 4 or 10?
CS314 – Section 5 Recitation 9
String and Lists Dr. José M. Reyes Álamo.
Functional Programming
CS314 – Section 5 Recitation 10
CS 550 Programming Languages Jeremy Johnson
CS 326 Programming Languages, Concepts and Implementation
PPL Lecture Notes: Chapter 3 High-Order Procedures Revisited.
PPL Lazy Lists.
A lightening tour in 45 minutes
PROGRAMMING IN HASKELL
PROGRAMMING IN HASKELL
CS 270 Math Foundations of CS Jeremy Johnson
6.001 SICP Data abstractions
PROGRAMMING IN HASKELL
PROGRAMMING IN HASKELL
Lists in Python.
PROGRAMMING IN HASKELL
CEV208 Computer Programming
String and Lists Dr. José M. Reyes Álamo.
Arrays.
(early slides assume map/filter)
Higher Order Functions
What would be our focus ? Geometry deals with Declarative or “What is” knowledge. Computer Science deals with Imperative or “How to” knowledge 2/23/2019.
CSE 3302 Programming Languages
Announcements Quiz 5 HW6 due October 23
List and list operations (continue).
PROGRAMMING IN HASKELL
ormap, andmap, and filter
Rewriting your function using map and foldr
Lisp.
Presentation transcript:

CS 116 Tutorial 2 Functional Abstraction

Reminders Assignment 2 is due this Wednesday at Noon

Review Build-list Lambda Function that produces a function

Build-list (build-list n f) ;;build-list: Nat (Nat->X) -> (listof X) Builds a list of size n from 0 to n-1, then applies f to every element in the newly created list. Produces: ( list (f 0 ) (f 1 ) … (f ( n-1 ) ) ) Size of list Applied to every element in the list; think of it like map

Lambda Single-use, nameless helper function (define (f p1 … pn) function body here) (lambda (p1 … pn) function body here) lambda

Function that produces a function ( define (f x y …) ( lambda (a b …) function body here ) ) Contract: f: _ _ … -> ( _ _ … ->_ ) Only job of f is to call the lambda function Lambda function takes care of all the work MUST use in order to produce a function Consumed values of f Produced value of f / contract for lambda

1. Using build-list, write a Scheme function powers-of-two that consumes a nonnegative integer n and produces a list of integers that are the powers of 2 from 2 n down to 2 0 = 1. ◦ For example, (powers-of-two 2) => (list 4 2 1)

2. Using build-lis t and filter, write a Scheme function factors that consumes a number n (at least 1) and returns a list of all positive factors of n in increasing order. ◦ For example, (factors 4) => (list 1 2 4)

3. Without using explicit recursion, write a function copies that consumes a natural number n and returns a list containing 1 copy of 1, followed by 2 copies of 2, etc., up to n copies of n. You may want to use the function flatten below. ;; flatten: (listof (listof X)) -> (listof X) ;; consumes a list of lists of type X and appends these ;; lists together to create a list of type X (define (flatten le) (foldr append empty le))

4. Write a function map-posns that consumes a function ( f ) and a list of posn structures (points) and produces a new list by applying f to the x- and y- fields of each posn in points. ◦ For example, (map-posns (list (make-posn 0 1 ) (make-posn 10 4) (make-posn -4 -4)))  (list )

5. Write a function char-count-function- maker that consumes a predicate and produces a one-argument function. The function that is produced consumes a string and produces the number of characters for which the predicate evaluates to true. ◦ For example, (char-count-function-maker char-upper- case?) produces a function that consumes a string s and produces the number of characters in s that are uppercase.

6. Write a function map-together that consumes a function f and two lists lst1 and lst2 of the same length, and returns the list: (list (f (first lst1)(first lst2)) (f (second lst1)(second lst2))...) that we get if we apply f to the first elements of both lists, then the second elements of both lists, and so on. What is the contract of map-together ?

7. ormap is another built-in abstract function in Scheme. It consumes a function f and a list lst, and produces true if applying f to any value in lst produces true. Otherwise, false is produced. ◦ For example, to determine if there are any even numbers in (list ), you can write (ormap even? (list )). What is the contract for ormap?