Creating Functional Programs Brad Vander Zanden. Basic Techniques Tail Recursion – Use continuation arguments if necessary – Akin to pre-processing a.

Slides:



Advertisements
Similar presentations
Joshua Eckroth The Plan 1.Review some functions. 2.Write more functions. 3.Consider the nature of recursion. 4.Look at the.
Advertisements

Lisp II. How EQUAL could be defined (defun equal (x y) ; this is how equal could be defined (cond ((numberp x) (= x y)) ((atom x) (eq x y)) ((atom y)
Functional Programming. Pure Functional Programming Computation is largely performed by applying functions to values. The value of an expression depends.
Lisp II. How EQUAL could be defined (defun equal (x y) ; this is how equal could be defined (cond ((numberp x) (= x y)) ((atom x) (eq x y)) ((atom y)
CSE 341 Lecture 16 More Scheme: lists; helpers; let/let*; higher-order functions; lambdas slides created by Marty Stepp
Tricks.
Tail Recursion. Problems with Recursion Recursion is generally favored over iteration in Scheme and many other languages It’s elegant, minimal, can be.
מבוא מורחב - שיעור 91 Lecture 9 Lists continued: Map, Filter, Accumulate, Lists as interfaces.
HASKELL Presentation Programming Segment
Symbolic Expressions (S Expressions) Syntax: Opening and Closing parenthesis having elements in between. List represented in LISP: (A B2 C3 Shahid) (A.
S: Application of quicksort on an array of ints: partitioning.
Functional programming: LISP Originally developed for symbolic computing First interactive, interpreted language Dynamic typing: values have types, variables.
CS3L: Introduction to Symbolic Programming Summer 2008Colleen Lewis Lecture 25: Trees and Generalized Lists.
Recursion in Scheme recursion is the basic means for expressing repetition some recursion is on numbers –factorial –fibonacci some recursion is on structures.
General pattern for selecting some elements of a list This negatives example illustrates a general pattern: If you want a function which selects some elements.
Spring 2008Programming Development Techniques 1 Topic 6 Hierarchical Data and the Closure Property Section September 2008.
Cs1120 Fall 2009 David Evans Lecture 20: Programming with State.
Plt /8/ Inductive Sets of Data Programming Language Essentials 2nd edition Chapter 1.2 Recursively Specified Programs.
CS 152: Programming Language Paradigms February 24 Class Meeting Department of Computer Science San Jose State University Spring 2014 Instructor: Ron Mak.
Functional Programming in Scheme and Lisp. Overview In a functional programming language, functions are first class objects. You can create them, put.
CS 326 Programming Languages, Concepts and Implementation Instructor: Mircea Nicolescu Lecture 7.
Scheme & Functional Programming. ( ) >> 64 ( ) >> 666 (* ) >> 1200 (+ (* 3 5) (- 10 6)) >> 19.
Functional Programming and Lisp. Overview In a functional programming language, functions are first class objects. In a functional programming language,
1 Append: process  (append list1 list2) (cons 1 (append ‘(2) list2)) (cons 1 (cons 2 (append ‘() list2))) (cons 1 (cons 2 list2)) (define (append list1.
Practice session #6: 1. Sequence operations 2. Partial evaluation with currying 3. Lazy-lists.
14-October-2002cse Lists © 2002 University of Washington1 Lists CSE 413, Autumn 2002 Programming Languages
Abstraction: Procedures as Parameters CMSC Introduction to Computer Programming October 14, 2002.
COP4020 Programming Languages Functional Programming Prof. Xin Yuan.
CS220 Programming Principles 프로그래밍의 이해 2002 가을학기 Class 6 한 태숙.
CS535 Programming Languages Chapter - 10 Functional Programming With Lists.
Dr. Philip Cannata 1 Simple Lisp John McCarthy Alonzo Church David Hilbert, Jules Richard, G. G. Berry, Georg Cantor, Bertrand Russell, Kurt Gödel, Alan.
PPL Lazy Lists. Midterm 2012 (define sum-vals (λ (ts) (if (ts-simple? ts) (ts-val ts) (accumulate + 0 (map ts-val (ts-inner-slots ts))))))
The “Beauty” of Scheme: Programs as Proofs Q: Is '(abc 123) a list? A: Yes Proof:  '(abc 123) = (cons 'abc (cons 123 '()))  '() is a list, so (cons 123.
Scope: What’s in a Name? CMSC Introduction to Computer Programming October 16, 2002.
Functional Programming: Lisp MacLennan Chapter 10.
PPL CPS. Moed A 2007 Solution (define scale-tree (λ (tree factor) (map (λ (sub-tree) (if (list? sub-tree) (scale-tree sub-tree factor) (* sub-tree.
A Recipe for Algorithm Design Or, What to do when the data type doesn't match the function…yet.
Logic Programming (Control and Backtracking). Contents Logic Programming –sans Control –with Backtracking Streams.
CSE 425: Functional Programming I Programs as Functions Some programs act like mathematical functions –Associate a set of input values from the function’s.
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.
PPL CPS. Moed A 2007 Solution (define scale-tree (λ (tree factor) (map (λ (sub-tree) (if (list? sub-tree) (scale-tree sub-tree factor) (* sub-tree.
1 Introduction to Functional Programming in Racket CS 270 Math Foundations of CS Jeremy Johnson.
Additional Scheme examples
CS 550 Programming Languages Jeremy Johnson
CSE341: Programming Languages Lecture 14 Thunks, Laziness, Streams, Memoization Dan Grossman Spring 2017.
6.001 Jeopardy.
PPL Lecture Notes: Chapter 3 High-Order Procedures Revisited.
PPL Lazy Lists.
(defmacro (delay x) (list 'lambda () x))
Lecture 7: List Recursion CS200: Computer Science
Racket CSC270 Pepper major portions credited to
September 4, 1997 Programming Languages (CS 550) Lecture 6 Summary Operational Semantics of Scheme using Substitution Jeremy R. Johnson TexPoint fonts.
Closures and Streams cs784(Prasad) L11Clos
CS 270 Math Foundations of CS Jeremy Johnson
COP4020 Programming Languages
CSE341: Programming Languages Lecture 14 Thunks, Laziness, Streams, Memoization Zach Tatlock Winter 2018.
CSE341: Programming Languages Lecture 14 Thunks, Laziness, Streams, Memoization Dan Grossman Spring 2013.
Lecture 8: Recursion Practice CS200: Computer Science
PPL Sequence Interface.
CSE341: Programming Languages Lecture 14 Thunks, Laziness, Streams, Memoization Dan Grossman Spring 2016.
Lecture #8 מבוא מורחב.
CSE341: Programming Languages Lecture 14 Thunks, Laziness, Streams, Memoization Dan Grossman Autumn 2017.
CS 36 – Chapter 11 Functional programming Features Practice
CSE341: Programming Languages Lecture 14 Thunks, Laziness, Streams, Memoization Dan Grossman Autumn 2018.
Lecture #7 מבוא מורחב.
List and list operations (continue).
Functional Programming: Lisp
Lecture # , , , , מבוא מורחב.
More Scheme CS 331.
CSE341: Programming Languages Lecture 14 Thunks, Laziness, Streams, Memoization Dan Grossman Spring 2019.
Presentation transcript:

Creating Functional Programs Brad Vander Zanden

Basic Techniques Tail Recursion – Use continuation arguments if necessary – Akin to pre-processing a list Inductive Construction – Akin to post-processing a list

Factorial Inductive Construction (define fact (lambda (n) (cond ((= n 0) 1) ((= n 1) 1) (#t (* n (fact (- n 1)))))))

Factorial Tail Recursion Construction (define fact (lambda (n) (letrec ((factHelper (lambda (n productThusFar) (cond ((= n 0) productThusFar) ((= n 1) productThusFar) (#t (factHelper (- n 1) (* n productThusFar))))))) (factHelper n 1))))

Sum of Numbers Inductive Construction (define sumList (lambda (L) (cond ((null? L) 0) (#t (+ (car L) (sumList (cdr L)))))))

Sum of Numbers Tail Recursion Construction (define sumList (lambda (L) (letrec ((sumListHelper (lambda (L sumThusFar) (cond ((null? L) sumThusFar) (#t (sumListHelper (cdr L) (+ (car L) sumThusFar))))))) (sumListHelper L 0))))

Quicksort Inductive Construction (define qsortPartition (lambda (pivot L) (if (null? L) (cons '() '()) (let* ((result (qsortPartition pivot (cdr L))) (lesserList (car result)) (greaterList (cdr result))) (if (< (car L) pivot) ; add the head of L to the lesserList (cons (cons (car L) lesserList) greaterList) ; add the head of L to the greaterList (cons lesserList (cons (car L) greaterList)))))))

Quicksort Tail Recursion Construction (define qsortPartition (lambda (pivot L L1 L2) (if (null? L) (cons L1 L2) ; return L1 and L2 once L is exhausted (let ((firstElement (car L))) (if (< firstElement pivot) ; add head of L to L1 and partition rest of L (qsortPartition pivot (cdr L) (cons firstElement L1) L2) ; add head of L to L2 and partition rest of L (qsortPartition pivot (cdr L) L1 (cons firstElement L2)))))))

Quicksort Either construction (define qsort (lambda (L) (if (null? L) L (let* ((result (qsortPartition (car L) (cdr L) '() '())) (lesserList (car result)) (greaterList (cdr result))) (append (qsort lesserList) (list (car L)) (qsort greaterList))))))