Func%onal Decomposi%on

Similar documents
Thinking Induc,vely. COS 326 David Walker Princeton University

Why OCaml? Introduction to Objective Caml. Features of OCaml. Applications written in OCaml. Stefan Wehr

Introduction to Objective Caml

Modules and Representa/on Invariants

OCaml Datatypes. COS 326 David Walker Princeton University

Programming Languages and Techniques (CIS120)

Mutation. COS 326 Andrew W. Appel Princeton University. slides copyright David Walker and Andrew W. Appel

Error Processing: An Exercise in Functional Design

CSE341: Programming Languages Lecture 9 Function-Closure Idioms. Dan Grossman Winter 2013

Programovací jazyky F# a OCaml. Chapter 5. Hiding recursion using function-as-values

List Functions, and Higher-Order Functions

Computer Science Foundation Exam

COS 326 David Walker Princeton University

A Func'onal Space Model

Thinking Induc,vely. COS 326 David Walker Princeton University

Tuples. CMSC 330: Organization of Programming Languages. Examples With Tuples. Another Example

CSI33 Data Structures

Write a program that displays all the even integers between 1 and 100, inclusive

Local defini1ons. Func1on mul1ples- of

Computer Science Foundation Exam

CS1110 Lab 6 (Mar 17-18, 2015)

Introduction to OCaml

Faculty of Engineering Computer Engineering Department Islamic University of Gaza C++ Programming Language Lab # 6 Functions

CSci 4223 Principles of Programming Languages

CMSC 330: Organization of Programming Languages. OCaml Higher Order Functions

Functional Programming. Pure Functional Programming

CMSC 330: Organization of Programming Languages. OCaml Higher Order Functions

CMSC 330: Organization of Programming Languages. OCaml Higher Order Functions

GE U111 Engineering Problem Solving & Computation Lecture 6 February 2, 2004

COMP 322: Fundamentals of Parallel Programming. Lecture 13: Parallelism in Java Streams, Parallel Prefix Sums

Functions. Lab 4. Introduction: A function : is a collection of statements that are grouped together to perform an operation.

The Haskell HOP: Higher-order Programming

A Func'onal Introduc'on. COS 326 David Walker Princeton University

Programming Languages and Techniques (CIS120)

Programming Languages. Tail Recursion. CSE 130: Winter Lecture 8: NOT TR. last thing function does is a recursive call

COSE212: Programming Languages. Lecture 3 Functional Programming in OCaml

COS 326 Func,onal Programming: An elegant weapon for the modern age David Walker Princeton University

F28PL1 Programming Languages. Lecture 14: Standard ML 4

Multiple Choice (Questions 1 13) 26 Points Select all correct answers (multiple correct answers are possible)

I BCS-031 BACHELOR OF COMPUTER APPLICATIONS (BCA) (Revised) Term-End Examination. June, 2015 BCS-031 : PROGRAMMING IN C ++

Futures. COS 326 David Walker Princeton University

Programming Languages Fall 2013

Multiple-Subscripted Arrays

Modules and Representation Invariants

Recursion. Q: What does this evaluate to? Q: What does this evaluate to? CSE 130 : Programming Languages. Higher-Order Functions

FORM 2 (Please put your name and form # on the scantron!!!!)

Write: evens. evens [] ====> [] evens [1;2;3;4] ====> [2;4]

University of Washington CSE 140 Data Programming Winter Final exam. March 11, 2013

Poly-HO! COS 326 David Walker Princeton University. polymorphic, higher-order programming

Computer Programming

F# Overview: Immutable Data + Pure Func7ons

INF121: Functional Algorithmic and Programming

Type Inference. COS 326 David Walker Princeton University

CSE341: Programming Languages Lecture 9 Function-Closure Idioms. Dan Grossman Fall 2011

Today s Plan. Programming Languages. Example : Factorial. Recursion. CSE 130 : Spring Lecture 6: Higher-Order Functions

INDIAN INSTITUTE OF TECHNOLOGY KHARAGPUR

System Design and Programming II

Functional Programming in Haskell Part I : Basics

CSCI 131, Midterm Exam 1 Review Questions This sheet is intended to help you prepare for the first exam in this course. The following topics have

Lecture 9. Memory and Call Stacks

"'"' .;, b) F or ( c = l ; c <= 10,"' c++ ) X { JJ. ~. Computer Programming I COME Midterm Examination. Fall November 2016

Programming Languages and Compilers (CS 421)

Part I. Language Core

Computer Department. Question (1): State whether each of the following is true or false. Question (2): Select the correct answer from the following:

OCaml Datatypes. COS 326 David Walker* Princeton University

It is better to have 100 functions operate one one data structure, than 10 functions on 10 data structures. A. Perlis

CS 115 Exam 3, Spring 2010

University of Washington CSE 140 Data Programming Winter Final exam. March 11, 2013

Muta%on. COS 326 David Walker Princeton University

FUNCTIONAL PROGRAMMING

Introduction to C Language (M3-R )

Reviewing all Topics this term

Multiple Choice (Questions 1 14) 28 Points Select all correct answers (multiple correct answers are possible)

A Functional Evaluation Model

COMP 322: Fundamentals of Parallel Programming. Lecture 22: Parallelism in Java Streams, Parallel Prefix Sums

Outline. 1 About the course

School of Computer Science

CS 340 Spring 2019 Midterm Exam

CSE 130 [Winter 2014] Programming Languages

Multiple Choice (Questions 1 13) 26 Points Select all correct answers (multiple correct answers are possible)

Chapter 3. More Flow of Control. Copyright 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley

Lecture 19: Functions, Types and Data Structures in Haskell

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

PROGRAMMING IN HASKELL. Chapter 2 - First Steps

CS18000: Problem Solving And Object-Oriented Programming

Lecture Programming in C++ PART 1. By Assistant Professor Dr. Ali Kattan

Exam 2. CSI 201: Computer Science 1 Fall 2016 Professors: Shaun Ramsey and Kyle Wilson. Question Points Score Total: 80

CS457/557 Functional Languages

Final Examination Semester 3 / Year 2010

Computer Science II CSci 1200 Sections 6-10 Week 3, Monday Lecture January 28, 2002 Functions, Structs and Multiple Files

Practical Haskell. An introduction to functional programming. July 21, Practical Haskell. Juan Pedro Villa-Isaza. Introduction.

CSE 413 Midterm, May 6, 2011 Sample Solution Page 1 of 8

Haskell 5.1 INTERACTIVE SESSIONS AND THE RUN-TIME SYSTEM

Introduction to Functional Programming

CS421 Spring 2014 Midterm 1

Chapters 6-7. User-Defined Functions

RACKET BASICS, ORDER OF EVALUATION, RECURSION 1

Abram Hindle Kitchener Waterloo Perl Monger October 19, 2006

Computer Programming. Decision Making (2) Loops

EECS402 Lecture 02. Functions. Function Prototype

Transcription:

Func%onal Decomposi%on COS 326 David Walker Princeton University slides copyright 2013-2015 David Walker and Andrew W. Appel permission granted to reuse these slides for non-commercial educa%onal purposes

Func%onal Decomposi%on == Break down complex problems in to a set of simple func%ons; Recombine (compose) func%ons to form solu%on Such problems can oqen be solved using a combinator library. (a set of func%ons that fit together nicely) The list library, which contains map and fold, is a combinator library.

PIPELINES

Pipe let ( >) x f = f x ;; Type?

Pipe let ( >) x f = f x ;; Type? ( >) : 'a -> ('a -> 'b) -> 'b

Pipe let ( >) x f = f x ;; let twice f x = x > f > f;;

Pipe let ( >) x f = f x ;; let twice f x = (x > f) > f;; leq associa%ve: x > f1 > f2 > f3 == ((x > f1) > f2) > f3

Pipe let ( >) x f = f x ;; let twice f x = x > f > f;; let square x = x*x;; let fourth x = twice square;;

Pipe let ( >) x f = f x ;; let twice f x = x > f > f;; let square x = x*x;; let fourth x = twice square x;; let compute x = x > square > fourth > ( * ) 3 > print_int > print_newline;;

PIPING LIST PROCESSORS (Combining combinators cleverly)

Another Problem type student = {first: string; last: string; assign: float list; final: float};; let students : student list = [ {first = "Sarah"; last = "Jones"; assign = [7.0;8.0;10.0;9.0]; final = 8.5}; {first = "Qian"; last = "Xi"; assign = [7.3;8.1;3.1;9.0]; final = 6.5}; ] ;;

Another Problem type student = {first: string; last: string; assign: float list; final: float};; Create a func%on display that does the following: for each student, print the following: last_name, first_name: score score is computed by averaging the assignments with the final each assignment is weighted equally the final counts for twice as much one student printed per line students printed in order of score

(1968 novel) Do Professors Dream of Homeworkgrade Databases?

Another Problem Create a func%on display that takes a list of students as an argument prints the following for each student: last_name, first_name: score score is computed by averaging the assignments with the final each assignment is weighted equally the final counts for twice as much one student printed per line students printed in order of score let display (students : student list) : unit = students > compute score > sort by score > convert to list of strings > print each string

Another Problem let compute_score {first=f; last=l; assign=grades; final=exam} = let sum x (num,tot) = (num +. 1., tot +. x) in let score gs e = List.fold_right sum gs (2., 2. *. e) in let (number, total) = score grades exam in (f, l, total /. number) ;; let display (students : student list) : unit = students > List.map compute_score > sort by score > convert to list of strings > print each string

Another Problem let student_compare (_,_,score1) (_,_,score2) = if score1 < score2 then 1 else if score1 > score2 then -1 else 0 ;; let display (students : student list) : unit = students > List.map compute_score > List.sort compare_score > convert to list of strings > print each string

Another Problem let stringify (first, last, score) = last ^ ", " ^ first ^ ": " ^ string_of_float score;; let display (students : student list) : unit = students > List.map compute_score > List.sort compare_score > List.map stringify > print each string

Another Problem let stringify (first, last, score) = last ^ ", " ^ first ^ ": " ^ string_of_float score;; let display (students : student list) : unit = students > List.map compute_score > List.sort compare_score > List.map stringify > List.iter print_endline

COMBINATORS FOR OTHER TYPES: PAIRS

Simple Pair Combinators let both f (x,y) = (f x, f y);; let do_fst f (x,y) = (f x, y);; let do_snd f (x,y) = ( x, f y);; pair combinators

Example: Piping Pairs let both f (x,y) = (f x, f y);; let do_fst f (x,y) = (f x, y);; let do_snd f (x,y) = ( x, f y);; pair combinators let even x = (x/2)*2 == x;; let process (p : float * float) = p > both int_of_float (* convert to int *) > do_fst ((/) 3) (* divide fst by 3 *) > do_snd ((/) 2) (* divide snd by 2 *) > both even (* test for even *) > fun (x,y) -> x && y (* both even *)

Summary ( >) passes data from one func%on to the next compact, elegant, clear UNIX pipes ( ) compose file processors unix scrip%ng with is a kind of func%onal programming but it isn't very general since is not polymorphic you have to serialize and unserialize your data at each step there can be uncaught type (ie: file format) mismatches between steps we avoided that in your assignment, which is precy simple Higher-order combinator libraries arranged around types: List combinators (map, fold, reduce, iter, ) Pair combinators (both, do_fst, do_snd, ) Network programming combinators (Frene%c: frene%c-lang.org)