COP4020 Programming Languages Functional Programming Prof. Xin Yuan.

Slides:



Advertisements
Similar presentations
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.
Advertisements

1 Programming Languages (CS 550) Lecture Summary Functional Programming and Operational Semantics for Scheme Jeremy R. Johnson.
CSE341: Programming Languages Lecture 16 Datatype-Style Programming With Lists or Structs Dan Grossman Winter 2013.
1 Programming Languages and Paradigms Lisp Programming.
Lambda Calculus and Lisp PZ03J. Lambda Calculus The lambda calculus is a model for functional programming like Turing machines are models for imperative.
Chapter 3 Functional Programming. Outline Introduction to functional programming Scheme: an untyped functional programming language.
1-1 An Introduction to Scheme March Introduction A mid-1970s dialect of LISP, designed to be a cleaner, more modern, and simpler version than.
Lisp Recitation (cse471/598 Fall 2007 ) Aravind Kalavagattu.
6.001 SICP SICP – September ? 6001-Introduction Trevor Darrell 32-D web page: section.
CS 330 Programming Languages 11 / 20 / 2007 Instructor: Michael Eckmann.
The Scheme Programming Language History and Significance Dmitry Nesvizhsky CIS24 Professor Danny Kopec.
6.001 SICP SICP Sections 5 & 6 – Oct 5, 2001 Quote & symbols Equality Quiz.
Scheme examples. Recursion Iteration is achieved via recursion Selection is achieved via COND Review the following examples to learn to think recursively.
Recursion in Scheme recursion is the basic means for expressing repetition some recursion is on numbers –factorial –fibonacci some recursion is on structures.
Spring 2008Programming Development Techniques 1 Topic 6 Hierarchical Data and the Closure Property Section September 2008.
1 (Functional (Programming (in (Scheme)))) Jianguo Lu.
CS 152: Programming Language Paradigms February 24 Class Meeting Department of Computer Science San Jose State University Spring 2014 Instructor: Ron Mak.
Slide 1 Vitaly Shmatikov CS 345 Introduction to Scheme.
Functional Programming in Scheme
CSC3315 (Spring 2009)1 CSC 3315 Programming Paradigms Scheme Language Hamid Harroud School of Science and Engineering, Akhawayn University
Functional Programming in Scheme and Lisp. Overview In a functional programming language, functions are first class objects. You can create them, put.
CSE 341, S. Tanimoto Lisp Defining Functions with DEFUN Functions are the primary abstraction mechanism available in Lisp. (Others are structures.
CS 326 Programming Languages, Concepts and Implementation Instructor: Mircea Nicolescu Lecture 7.
CS 152: Programming Language Paradigms February 17 Class Meeting Department of Computer Science San Jose State University Spring 2014 Instructor: Ron Mak.
For B551 (and B351) Todd Holloway Indiana University Please download Petite Chez Scheme (Scheme.com) If you haven’t already…
Scheme & Functional Programming. ( ) >> 64 ( ) >> 666 (* ) >> 1200 (+ (* 3 5) (- 10 6)) >> 19.
Introduction to Scheme CS 480/680 – Comparative Languages “And now for something completely different…” – Monty Python “And now for something completely.
Functional Programming and Lisp. Overview In a functional programming language, functions are first class objects. In a functional programming language,
Input and Output in Scheme CS 480/680 – Comparative Languages.
CS 326 Programming Languages, Concepts and Implementation Instructor: Mircea Nicolescu Lecture 8.
CS 330 Programming Languages 11 / 13 / 2008 Instructor: Michael Eckmann.
COP4020 Programming Languages Functional Programming Prof. Xin Yuan.
COP4020 Programming Languages Functional Programming Prof. Robert van Engelen.
Introduction to Scheme. Lisp and Scheme Lisp: List processor language –Full recursion –Conditional expression –Extensibility –Interactive Scheme: one.
Abstraction: Procedures as Parameters CMSC Introduction to Computer Programming October 14, 2002.
Milos Hauskrecht (PDF) Hieu D. Vu (PPT) LISP PROGARMMING LANGUAGE.
1 Variable Declarations Global and special variables – (defvar …) – (defparameter …) – (defconstant …) – (setq var2 (list 4 5)) – (setf …) Local variables.
LECTURE 16 Functional Programming. WHAT IS FUNCTIONAL PROGRAMMING? Functional programming defines the outputs of a program as a mathematical function.
21-October-2002cse IO © 2002 University of Washington1 Input / Output CSE 413, Autumn 2002 Programming Languages
Computer Eng. Software Lab II , Semester 2, Who I am: Andrew Davison CoE, WiG Lab Office Functional Programming.
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.
COP4020 Programming Languages Logic Programming Prof. Xin Yuan.
Comparative Programming Languages Functional programming with Lisp/Scheme.
PPL Lecture 4 Slides by Yaron Gonen, based on slides by Daniel Deutch and lecture notes by Prof. Mira Balaban.
CS 152: Programming Language Paradigms February 12 Class Meeting Department of Computer Science San Jose State University Spring 2014 Instructor: Ron Mak.
COP4020 Programming Languages Functional Programming Prof. Robert van Engelen (modified by Prof. Em. Chris Lacher)
CS314 – Section 5 Recitation 9
Functional Programming
CS314 – Section 5 Recitation 10
Functional Programming
Additional Scheme examples
CS 326 Programming Languages, Concepts and Implementation
CS 326 Programming Languages, Concepts and Implementation
Racket CSC270 Pepper major portions credited to
CS 326 Programming Languages, Concepts and Implementation
Chapter 15 – Functional Programming Languages
COP4020 Programming Languages
Lecture 8: Recursion Practice CS200: Computer Science
FP Foundations, Scheme In Text: Chapter 14.
CS 36 – Chapter 11 Functional programming Features Practice
Abstraction and Repetition
Streams, Delayed Evaluation and a Normal Order Interpreter
Announcements Quiz 5 HW6 due October 23
Defining Functions with DEFUN
Abstraction and Repetition
6.001 SICP Interpretation Parts of an interpreter
COP4020 Programming Languages
Lisp.
More Scheme CS 331.
Abstraction and Repetition
Presentation transcript:

COP4020 Programming Languages Functional Programming Prof. Xin Yuan

COP4020 Spring /27/2015 Topics Functional programming with Scheme Learn by examples

COP4020 Spring /27/2015 Defining Global Names A global name is defined with the “define” special form (define name value) Usually the values are functions (lambda abstractions) Examples:  (define my-name ''foo'')  (define determiners '(''a'' ''an'' ''the''))  (define sqr (lambda (x) (* x x)))  (define twice (lambda (f a) (f (f a))))  (twice sqr 3)  ((lambda (f a) (f (f a))) (lambda (x) (* x x)) 3)  …  81

COP4020 Spring /27/2015 Defining Global Names A global name defined with a lambda express in essence creates a named function.  This allows recursion – one of the most important/useful concept in scheme.  (define fab (lambda (x) (if (= x 0) 0 (if (=x 1) 1 (+ (fab (- x 1)) (fab (- x 2)))))))  Define a function that computes the sum of all elements in a list.  Define a function that put an element at the end of a list

Load program from file Load function (load filename) load the program from a file  (load “myscheme”): load the program in “myscheme.scm”  After load, one has access to all global names in the program. COP4020 Spring /27/2015

COP4020 Spring /27/2015 I/O (display x) prints value of x and returns an unspecified value  (display "Hello World!") Displays: "Hello World!"  (display (+ 2 3)) Displays: 5 (newline) advances to a new line (read) returns a value from standard input  (if (member (read) '( )) "You guessed it!" "No luck") Enter: 5 Displays: You guessed it! (read-line) returns the string in a line (without the “\n”).

I/O with files Open file for reading: (open-input-file filename) Open file for writing: (open-output-file filename) Read one character from a file: (read-char file) Read a scheme object from file: (read file) Check the current character in a file without consuming the character: (peek-char file) Check end-of-file: (eof-object? (peek-char file)) Write one character to a file: (write-char char file) Write a scheme object to file: (write object file) Close file: (close-port file) COP4020 Spring /27/2015

COP4020 Spring /27/2015 Blocks (begin x 1 x 2 … x n ) sequences a series of expressions x i, evaluates them, and returns the value of the last one x n Examples:  (begin (display "Hello World!") (newline) )

COP4020 Spring /27/2015 Example 1: define factorial function: fact(n) = 1*2*3*…*n

COP4020 Spring /27/2015 Example 1: define factorial function: fact(n) = 1*2*3*…*n Recursive factorial: (define fact (lambda (n) (if (zero? n) 1 (* n (fact (- n 1)))) ) ) (fact 2)  (if (zero? 2) 1 (* 2 (fact (- 2 1))))  (* 2 (fact 1))  (* 2 (if (zero? 1) 1 (* 1 (fact (- 1 1)))))  (* 2 (* 1 (fact 0)))  (* 2 (* 1 (if (zero? 0) 1 (* 0 (fact (- 0 1))))  (* 2 (* 1 1))  2

COP4020 Spring /27/2015 Example 2 Sum the elements of a list (define sum (lambda (lst) (if (null? lst) 0 (+ (car lst) (sum (cdr lst))) ) ) ) (sum '(1 2 3))  (+ 1 (sum (2 3))  (+ 1 (+ 2 (sum (3))))  (+ 1 (+ 2 (+ 3 (sum ()))))  (+ 1 (+ 2 (+ 3 0)))

COP4020 Spring /27/2015 Example 3 Generate a list of n copies of x (define fill (lambda (n x) ??? ) )

COP4020 Spring /27/2015 Example 3 Generate a list of n copies of x (define fill (lambda (n x) (if (= n 0) () (cons x (fill (- n 1) x))) ) ) (fill 2 'a)  (cons a (fill 1 a))  (cons a (cons a (fill 0 a)))  (cons a (cons a ()))  (a a)

COP4020 Spring /27/2015 Example 4 Replace x with y in list xs (define subst (lambda (x y xs) ??? ) )

COP4020 Spring /27/2015 Example 4 Replace x with y in list xs (define subst (lambda (x y xs) (cond ((null? xs) ()) ((eq? (car xs) x) (cons y (subst x y (cdr xs)))) (else (cons (car xs) (subst x y (cdr xs)))) ) ) )