CS 330 Programming Languages 11 / 20 / 2007 Instructor: Michael Eckmann.

Slides:



Advertisements
Similar presentations
CS 206 Introduction to Computer Science II 09 / 05 / 2008 Instructor: Michael Eckmann.
Advertisements

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.
Lists in Lisp and Scheme a. Lists are Lisp’s fundamental data structures, but there are others – Arrays, characters, strings, etc. – Common Lisp has moved.
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
Chapter 3 Functional Programming. Outline Introduction to functional programming Scheme: an untyped functional programming language.
CS 355 – PROGRAMMING LANGUAGES Dr. X. Apply-to-all A functional form that takes a single function as a parameter and yields a list of values obtained.
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.
CS 330 Programming Languages 12 / 02 / 2008 Instructor: Michael Eckmann.
Lisp. Versions of LISP Lisp is an old language with many variants –LISP is an acronym for List Processing language Lisp is alive and well today Most modern.
Chapter 15 Functional Programming Languages. Copyright © 2007 Addison-Wesley. All rights reserved. 1–2 Introduction Design of imperative languages is.
ISBN Chapter 15 Functional Programming Languages Mathematical Functions Fundamentals of Functional Programming Languages Introduction to.
CS 330 Programming Languages 11 / 18 / 2008 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 02 / 22 / 2008 Instructor: Michael Eckmann.
CS 330 Programming Languages 11 / 16 / 2006 Instructor: Michael Eckmann.
CSC321: Programming Languages14-1 Programming Languages Tucker and Noonan Chapter 14: Functional Programming 14.1 Functions and the Lambda Calculus 14.2.
CS 106 Introduction to Computer Science I 10 / 09 / 2006 Instructor: Michael Eckmann.
CS 330 Programming Languages 09 / 30 / 2008 Instructor: Michael Eckmann.
Functional programming: LISP Originally developed for symbolic computing First interactive, interpreted language Dynamic typing: values have types, variables.
ISBN Chapter 15 Functional Programming Languages.
Programming Languages I LISP
ISBN Chapter 15 Functional Programming Languages.
CS 106 Introduction to Computer Science I 02 / 19 / 2007 Instructor: Michael Eckmann.
LISP A brief overview. Lisp stands for “LISt Process” –Invented by John McCarthy (1958) –Simple data structure (atoms and lists) –Heavy use of recursion.
(5.1) COEN Functional Languages  Functional programming basics  Atoms and lists; cons  Useful primitive functions  Predicates  Arithmetic functions.
1 Lists in Lisp and Scheme. 2 Lists are Lisp’s fundamental data structures. Lists are Lisp’s fundamental data structures. However, it is not the only.
1 Lisp Functions –Built-in functions –Defining functions –Function Evaluation and Special Forms defun, if Control statements –Conditional if, cond –Repetition.
Functional Programming in Scheme
ISBN Chapter 15 Functional Programming Languages.
Functional Programming in Scheme and Lisp. Overview In a functional programming language, functions are first class objects. You can create them, put.
CS 330 Programming Languages 11 / 21 / 2006 Instructor: Michael Eckmann.
CS 326 Programming Languages, Concepts and Implementation Instructor: Mircea Nicolescu Lecture 7.
CS 330 Programming Languages 11 / 13 / 2008 Instructor: Michael Eckmann.
Functional Programming in Scheme and Lisp.
COP4020 Programming Languages Functional Programming Prof. Xin Yuan.
A Second Look At ML 1. Outline Patterns Local variable definitions A sorting example 2.
ISBN Chapter 15 Functional Programming Languages.
CS 363 Comparative Programming Languages Functional Languages: Scheme.
Introduction to ACL2 CS 680 Formal Methods for Computer Verification Jeremy Johnson Drexel University.
ISBN Chapter 15 Functional Programming Languages.
ISBN Chapter 15 Functional Programming Languages.
LISP Data Types Functional Programming Academic Year Alessandro Cimatti
Chapter SevenModern Programming Languages1 A Second Look At ML.
CS 330 Programming Languages 11 / 15 / 2007 Instructor: Michael Eckmann.
Fall 2008Programming Development Techniques 1 Topic 8 Sequences as Conventional Interfaces Section October 2008.
CS 330 Programming Languages 11 / 28 / 2006 Instructor: Michael Eckmann.
1 FP Foundations, Scheme In Text: Chapter Chapter 14: FP Foundations, Scheme Mathematical Functions Def: A mathematical function is a mapping of.
ISBN Chapter 15 Functional Programming Languages.
Forms Writing your own procedures CS 480/680 – Comparative Languages.
CSE 425: Functional Programming I Programs as Functions Some programs act like mathematical functions –Associate a set of input values from the function’s.
Comparative Programming Languages Functional programming with Lisp/Scheme.
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.
CS 152: Programming Language Paradigms February 12 Class Meeting Department of Computer Science San Jose State University Spring 2014 Instructor: Ron Mak.
Functional Programming
Functional Programming Languages
Functional Programming
History of Computing – Lisp
CS 550 Programming Languages Jeremy Johnson
Modern Programming Languages Lecture 20 Fakhar Lodhi
Chapter 15 – Functional Programming Languages
Lists in Lisp and Scheme
CS 270 Math Foundations of CS Jeremy Johnson
COP4020 Programming Languages
LISP A brief overview.
FP Foundations, Scheme In Text: Chapter 14.
Functional Programming Languages
LISP A brief overview.
Announcements Quiz 5 HW6 due October 23
Lisp.
Presentation transcript:

CS 330 Programming Languages 11 / 20 / 2007 Instructor: Michael Eckmann

Michael Eckmann - Skidmore College - CS Fall 2007 Today’s Topics Questions / comments? Scheme –multiple topics

Michael Eckmann - Skidmore College - CS Fall 2007 Functional Programming Example of a function containing an IF (DEFINE (factorial n) (IF (= n 0) 1 (* n (factorial (- n 1))) )

Michael Eckmann - Skidmore College - CS Fall 2007 Functional Programming Example of a function containing a COND (DEFINE (compare x y) (COND ( (> x y) ( DISPLAY “x is > y” )) ( (< x y) ( DISPLAY “x is < y” )) ( ELSE ( DISPLAY “x is = y” ) )

Michael Eckmann - Skidmore College - CS Fall 2007 Functional Programming Want to try to write a function that determines whether an atom is in a particular list? e.g. (member 'B '(A B C)) should return #T and (member 'X '(A B C)) should return #F.

Michael Eckmann - Skidmore College - CS Fall 2007 Functional Programming Want to try to write a function that determines whether an atom is in a particular list? e.g. (member 'B '(A B C)) should return #T and (member 'X '(A B C)) should return () (which is #F.) (DEFINE (member atm lis) (COND ((NULL? lis) #F) ((EQ? atm (CAR lis)) #T) (ELSE ( member atm (CDR lis))) )

Michael Eckmann - Skidmore College - CS Fall 2007 Scheme when writing functions dealing with a list lis, if you want to go through the elements one at a time, use (car lis) to get the first and recurse on (cdr lis) also, you'll need to tell your function when to stop, that is, when the list is null, so (null? lis) should be the first thing you check. In general recursive functions need to 1st check the base case (the case that will be true when the recursion should stop) and in the recursive step(s) make sure that the function is working towards the base case (What do I mean here?)

Michael Eckmann - Skidmore College - CS Fall 2007 Scheme (review of some funcs) car --- of a list, returns the first element of a list --- it is only defined for non-empty lists cdr (pronounced could-er) --- of a list, returns the remaining elements of the list as a list, excluding the first element --- it is only defined for non-empty lists cons --- takes two arguments, the second must be a list --- returns a new list with the first argument being the first element of the new list and the elements of the second argument list being the remaining elements of the new list

Michael Eckmann - Skidmore College - CS Fall 2007 Scheme (review of some funcs) null? -- returns #t if the argument is the empty list -- returns #f otherwise list? -- returns #t or #f depending on whether the argument is a list or not (cond ( predicate1 expression { expression } ) ( predicate2 expression { expression } )... ( predicaten expression { expression } ) ( else expression { expression } ) )

Michael Eckmann - Skidmore College - CS Fall 2007 Scheme (review of some funcs) eq? -- used to compare atoms for equality = -- used to compare numbers for equality equal? -- used to compare lists for equality #t is literal true #f is literal false

Michael Eckmann - Skidmore College - CS Fall 2007 Functional Programming Scheme let's write the following functions: determine if an atom is in a list or any sublist of a list (deep member test) determine if a list is made up of only atoms (that is, no sublists) power function (for positive exponents) power function (for positive or negative exponents)

Michael Eckmann - Skidmore College - CS Fall 2007 Functional Programming Scheme let's write the following functions: remove the first occurence of an atom from a list remove all occurences of an atom from a list replace the first occurence of an atom in a list with another atom replace all occurences of an atom in a list with another atom

Michael Eckmann - Skidmore College - CS Fall 2007 Functional Programming Scheme logical functions –and or not some more functions available in scheme: –number? list? boolean? for numbers: –real? integer? rational? complex? odd? even? –+, -, *, /, abs, sqrt, remainder, min, max, round, truncate, ceiling, floor some functions on lists –length, reverse –list-ref

Michael Eckmann - Skidmore College - CS Fall 2007 Functional Programming Scheme examples: –(define mylist '(a b c)) –mylist ; gives (a b c) –(list-ref mylist 2) ; gives c –(length mylist) ; gives 3 –(reverse mylist) ; gives (c b a), but mylist is still (a b c) reverse is not a deep reverse (i.e. it does not reverse any sublists)

Michael Eckmann - Skidmore College - CS Fall 2007 Functional Programming Scheme –lambda creates a nameless function –e.g. (lambda (a) (* a a)) ; this is the lambda function with ; a as a parameter and it squares a ((lambda (a) (* a a)) 4) ; how to call it –to name the function we can do: (define square (lambda (a) (* a a)) )

Michael Eckmann - Skidmore College - CS Fall 2007 Functional Programming Scheme –to name the function we can do: (define square (lambda (a) (* a a)) ) –instead we have been using the shortened form to define functions: (define (square a) (* a a)) )

Michael Eckmann - Skidmore College - CS Fall 2007 Functional Programming more generally –to name the function we can do: (define myname (lambda (parameters) (expression)) ) –instead we have been using the shortened form to define functions: (define (myname parameters) (expression)) )

Michael Eckmann - Skidmore College - CS Fall 2007 Functional Programming Scheme –recall how I said cons takes an atom and a list and returns the new list with the atom on the front –and car returns the first element of a list –and cdr returns the remainder of the list as a list –This is true for what are called PROPER LISTS

Michael Eckmann - Skidmore College - CS Fall 2007 Functional Programming Scheme –PROPER LISTS are those lists whose last cdr is the empty list. –IMPROPER LISTS are those lists whose last cdr is something other than the empty list –see handout (p. 18,19 from Dybvig's "The Scheme Programming Language") –Anything created by consing two things together is a pair –(cons 'a 'b) ; creates a dotted pair (an improper list) –(cons 'a '(b)) ; creates a proper list (which is also a pair) –(cons 'a (cons 'b '(c)))

Michael Eckmann - Skidmore College - CS Fall 2007 Functional Programming Scheme –PROPER LISTS are displayed in parens with a space between the elements e.g. (define proplist (cons 'a (cons 'b '(c)))) proplist ; displays as (a b c) –but more explicitly that list (a b c) can be thought of as: (a. (b. (c. ()))) –where it is a nested set of pairs with dots between the car and the cdr of the pair –Notice that the last cdr is the empty list.

Michael Eckmann - Skidmore College - CS Fall 2007 Functional Programming IMPROPER LISTS are displayed with dots. –e.g. (define improplist (cons 'a 'b)) improplist ; displays as (a. b) (define improplist2 (cons 'a (cons 'b 'c)) improplist2 ; displays as (a b. c) but more explicitly improplist2 can be thought of as (a. (b. c)) (a b. c) is shorthand for that (define improplist3 (cons (cons 'a 'b) 'c) improplist3 ; displays as ((a. b). c)

Michael Eckmann - Skidmore College - CS Fall 2007 Functional Programming Scheme –list? ; determines if the argument is a proper list –pair? ; determines if the argument is a pair (list? '()) returns #t but (pair? '()) returns #f list? and pair? will return #t for non-empty proper lists list? will return #f for improper lists pair? will return #t for improper lists list? and pair? will return #f for non-pairs like –(pair? 'a) –(list? 'a)

Michael Eckmann - Skidmore College - CS Fall 2007 Functional Programming Scheme –let creates local variables Syntax: (let ((var val)...) exp1 exp2...) e.g. (let ((x 5) (y 7)) (+ x y)) ; x and y are local (let ((z (* 6 6))) (+ z z)) ; z is local without let we would write: (+ (* 6 6) (* 6 6)) ; and the * would be performed twice

Michael Eckmann - Skidmore College - CS Fall 2007 Functional Programming Scheme –let (let ((x 2)) (display (+ x 3)) (display "\n") (display x) ) ; x's value is not changed throughout the code above except ; when it is first given the value 2 (display x) ; x is local to the above code so this fails

Michael Eckmann - Skidmore College - CS Fall 2007 Functional Programming Scheme –set! performs assignments on previously declared variables not a pure functional language feature let's look at the function on the handout written with and without set!

Michael Eckmann - Skidmore College - CS Fall 2007 Functional Programming Scheme –let's write some more functions –count the number of #t's in a list of booleans (count-t '(#t #t #f #f #t #t #f)) ; should return 4