CS 330 Programming Languages 11 / 28 / 2006 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.
CS 206 Introduction to Computer Science II 04 / 01 / 2009 Instructor: Michael Eckmann.
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.
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.
LISP Programming. LISP – simple and powerful mid-1950’s by John McCarthy at M.I.T. “ LIS t P rocessing language” Artificial Intelligence programs LISP.
Fall 2008Programming Development Techniques 1 Topic 2 Scheme and Procedures and Processes September 2008.
CS 330 Programming Languages 10 / 14 / 2008 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 11 / 09 / 2007 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 09 / 10 / 2008 Instructor: Michael Eckmann.
ITERATIVE CONSTRUCTS: DOLIST Dolist is an iterative construct (a loop statement) consisting of a variable declaration and a body The body states what happens.
CS 106 Introduction to Computer Science I 11 / 20 / 2006 Instructor: Michael Eckmann.
CS 330 Programming Languages 10 / 11 / 2007 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.
CS 106 Introduction to Computer Science I 02 / 12 / 2007 Instructor: Michael Eckmann.
CS 330 Programming Languages 11 / 20 / 2007 Instructor: Michael Eckmann.
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 106 Introduction to Computer Science I 02 / 11 / 2008 Instructor: Michael Eckmann.
CS 330 Programming Languages 11 / 18 / 2008 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 02 / 22 / 2008 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 10 / 26 / 2009 Instructor: Michael Eckmann.
CS 330 Programming Languages 11 / 16 / 2006 Instructor: Michael Eckmann.
CS 330 Programming Languages 11 / 13 / 2007 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 03 / 30 / 2007 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 10 / 09 / 2006 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 03 / 03 / 2008 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.
CS 106 Introduction to Computer Science I 02 / 19 / 2007 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 10 / 15 / 2007 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 09 / 28 / 2007 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 10 / 28 / 2009 Instructor: Michael Eckmann.
(5.1) COEN Functional Languages  Functional programming basics  Atoms and lists; cons  Useful primitive functions  Predicates  Arithmetic functions.
Advanced Functions In CL, functions are often supplied as parameters to other functions –This gives us tremendous flexibility in writing functions whose.
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.
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.
Functional Programming and Lisp. Overview In a functional programming language, functions are first class objects. In a functional programming language,
CS 330 Programming Languages 10 / 07 / 2008 Instructor: Michael Eckmann.
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.
ISBN Chapter 15 Functional Programming Languages.
LISP Data Types Functional Programming Academic Year Alessandro Cimatti
Operating on Lists Chapter 6. Firsts and Seconds n Transforming list of pairs into two lists –(firsts ‘((1 5) (2 6) (3 7)))  (1 2 3) –(seconds ‘((1 5)
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.
FUNCTIONS. Midterm questions (1-10) review 1. Every line in a C program should end with a semicolon. 2. In C language lowercase letters are significant.
1 FP Foundations, Scheme In Text: Chapter Chapter 14: FP Foundations, Scheme Mathematical Functions Def: A mathematical function is a mapping of.
Functional Programming. Some Functional Languages Lisp Scheme - a dialect of Lisp Haskell Miranda.
Forms Writing your own procedures CS 480/680 – Comparative Languages.
Functional Programming
Functional Programming Languages
Functional Programming
History of Computing – Lisp
CS 326 Programming Languages, Concepts and Implementation
Lists in Lisp and Scheme
FP Foundations, Scheme In Text: Chapter 14.
Lecture 13 - Assignment and the environments model Chapter 3
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.
Announcements Quiz 5 HW6 due October 23
Common Lisp II.
Presentation transcript:

CS 330 Programming Languages 11 / 28 / 2006 Instructor: Michael Eckmann

Michael Eckmann - Skidmore College - CS Fall 2006 Today’s Topics Questions / comments? Scheme –fix the power function for negative exponents –more Scheme features Next class we'll discuss the points made in the Hughes paper

Michael Eckmann - Skidmore College - CS Fall 2006 Functional Programming Scheme let's get back into scheme by fixing that power function and writing 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 2006 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 2006 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 2006 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 2006 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 2006 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 2006 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 2006 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 2006 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 2006 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 2006 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 2006 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 2006 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 2006 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 2006 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

Michael Eckmann - Skidmore College - CS Fall 2005 Map map –a function that takes two parameters which are a function and a list –map applies the function to each element of the list and returns a list of the returned values –the following returns (#t #t #t #f #f #f #f #t) –(map number? '(1 2 3 a b r tttt 4.5)) –another example: –(map floor '( ))

Michael Eckmann - Skidmore College - CS Fall 2005 Apply apply –a function that takes two parameters which are a function and a list –it applies the function to all elements of the list and returns whatever the function would have returned if called using those elements of this list as individual parameters. –only functions that take a collection of arguments, not as a list, individually (like + and <) can be passed to apply –(apply < '( )) ; ok, because (< ) is a valid call. –(apply + '( )) ; ok, because ( ) is a valid call.

Michael Eckmann - Skidmore College - CS Fall 2005 recall this function we wrote ; the list that is passed in is expected to contain only boolean values. It returns the number of #t's in the list. ; this is a modified version of the one we wrote which is only valid for lists of booleans (define (count-t lis) (cond ((null? lis) 0) ((car lis) (+ 1 (count-t (cdr lis)))) (else (count-t (cdr lis))) ) ;; this will return 4 because there are 4 #t's in the list (count-t '(#t #t #f #f #t #t #f))

Michael Eckmann - Skidmore College - CS Fall 2005 How to write count-if ;; count-if is supposed to take a function and a list as parameters and count how many trues would result when the function is applied to each element of the list. (define (count-if fun lis) ;; help me finish it ) ;; this will return 4 because 4 of the list elements are numbers (count-if number? '(1 2 3 a b r tttt 4.5))

Michael Eckmann - Skidmore College - CS Fall 2005 How to write count-if ;; count-if is supposed to take a function and a list as parameters and count how many trues would result when the function is applied to each element of the list. (define (count-if fun lis) (count-t (map fun lis)) ) ;; this will return 4 because 4 of the list elements are numbers (count-if number? '(1 2 3 a b r tttt 4.5))

Michael Eckmann - Skidmore College - CS Fall 2005 How can we use count-if to... ;; write a function named every which takes in a function and a list as parameters and will return #t if the result of the function applied to each element is true (define (every fun lis) ;; what goes here? ) ;; write a function named any which takes in a function and a list as parameters and will return #t if the result of the function applied to at least one element is true (define (any fun lis) ;; what goes here? )

Michael Eckmann - Skidmore College - CS Fall 2005 How can we use count-if to... ;; write a function named every which takes in a function and a list as parameters and will return #t if the result of the function applied to each element is true (define (every fun lis) (= (length lis) (count-if fun lis) ) ) ;; write a function named any which takes in a function and a list as parameters and will return #t if the result of the function applied to at least one element is true (define (any fun lis) (>= (count-if fun lis) 1 ) )

Michael Eckmann - Skidmore College - CS Fall 2005 optional parameters A function that takes optional parameters is specified in this way: (define (fun reqp1 reqp2. optparms) ; reqp1 is required ; reqp2 is required ; optparms will be a list of all the rest of the arguments passed in ) ; if you want a function with only optional parameters: (define (fun2. optparms) ; optparms will be a list of all the arguments passed in )

Michael Eckmann - Skidmore College - CS Fall 2005 How about writing avg... ;; write a function named avg which takes in any number of numbers as parameters and averages them. ;; how will I write the parameters when defining the function? ;; how will we handle the parameters inside the function? ;; can we use anything we just learned to allow us to write the code inside the function? (define (avg ) )

Michael Eckmann - Skidmore College - CS Fall 2005 How about writing avg... ;; write a function named avg which takes in any number of numbers as parameters and averages them. ;; how will I write the parameters when defining the function? ;; how will we handle the parameters inside the function? ;; can we use anything we just learned to allow us to write the code inside the function? (define (avg. nums ) (cond ((null? nums) 0) ; returns 0 as the average of no nums (else (/ (apply + nums) (length nums)))) )

Michael Eckmann - Skidmore College - CS Fall 2005 Examples of functions as parameters So, we just witnessed several ways in which we can use functions as parameters. This is interesting since passing funcions as parameters are not common in imperitive languages. Hopefully you get the sense of how useful this feature is.

Michael Eckmann - Skidmore College - CS Fall 2005 Learning FLs There are enough people who believe, even though Scheme is not widely used, the way of thinking that is required to program in a FL like Scheme is a worthwhile skill for a computer scientist. “LISP is worth learning for a different reason — the profound enlightenment experience you will have when you finally get it. That experience will make you a better programmer for the rest of your days, even if you never actually use LISP itself a lot. “ - ESR (Eric S. Raymond)