1-1 An Introduction to Scheme March 2008. 1-2 Introduction A mid-1970s dialect of LISP, designed to be a cleaner, more modern, and simpler version than.

Slides:



Advertisements
Similar presentations
Functional Programming Languages Session 12
Advertisements

1 Copyright © 1998 by Addison Wesley Longman, Inc. Chapter 14 Functional Programming Languages - The design of the imperative languages is based directly.
CSE 3341/655; Part 4 55 A functional program: Collection of functions A function just computes and returns a value No side-effects In fact: No program.
Functional Programming. Pure Functional Programming Computation is largely performed by applying functions to values. The value of an expression depends.
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.
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 Functional programming Languages And a brief introduction to Lisp and Scheme.
Functional Programming Languages
ISBN Chapter 15 Functional Programming Languages.
CS 330 Programming Languages 11 / 20 / 2007 Instructor: Michael Eckmann.
Copyright © 2006 The McGraw-Hill Companies, Inc. Programming Languages 2nd edition Tucker and Noonan Chapter 14 Functional Programming It is better to.
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.
CSC321: Programming Languages14-1 Programming Languages Tucker and Noonan Chapter 14: Functional Programming 14.1 Functions and the Lambda Calculus 14.2.
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.
(5.1) COEN Functional Languages  Functional programming basics  Atoms and lists; cons  Useful primitive functions  Predicates  Arithmetic functions.
Chapter 15: Functional Programming Languages
1 Copyright © 1998 by Addison Wesley Longman, Inc. Chapter 13 In a language without exception handling: When an exception occurs, control goes to the operating.
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.
ISBN Chapter 15 Functional Programming Languages.
Functional Programming and Lisp. Overview In a functional programming language, functions are first class objects. In a functional programming language,
Functional Programming Universitatea Politehnica Bucuresti Adina Magda Florea
ISBN Chapter 15 Functional Programming Languages.
CS 330 Programming Languages 11 / 13 / 2008 Instructor: Michael Eckmann.
Chapter Fifteen: Functional Programming Languages Lesson 12.
ISBN Chapter 15 Functional Programming Languages.
CS 363 Comparative Programming Languages Functional Languages: Scheme.
Comparative Programming Languages Language Comparison: Scheme, Smalltalk, Python, Ruby, Perl, Prolog, ML, C++/STL, Java, Haskell.
ISBN Chapter 15 Functional Programming Languages.
ISBN Chapter 15 Functional Programming Languages.
ISBN Chapter 15 Functional Programming Languages.
1 Chapter 15 © 2002 by Addison Wesley Longman, Inc Introduction - The design of the imperative languages is based directly on the von Neumann architecture.
ISBN Chapter 15 Functional Programming Languages.
Chapter 15 Functional Programming Languages. Copyright © 2012 Addison-Wesley. All rights reserved.1-2 Chapter 15 Topics Introduction Mathematical Functions.
CS 330 Programming Languages 11 / 15 / 2007 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.
Functional Programming Part 1. Organization of Programming Languages-Cheng Big Picture u What we’ve learned so far: Imperative Programming Languages 
ISBN Chapter 15 Functional Programming Languages.
Comparative Programming Languages Functional programming with Lisp/Scheme.
ISBN Chapter 15 Functional Programming Languages.
Functional Programming Languages
Functional Programming Languages
Functional Programming Languages
Chapter 15 :Functional Programming Languages
Functional Programming Languages
Functional Programming
History of Computing – Lisp
Chapter 15 – Functional Programming Languages
Functional Programming Languages
Functional Programming Languages
Chapter 15 Functional Programming Languages
Functional Programming Languages
FP Foundations, Scheme In Text: Chapter 14.
Functional Programming Languages
Functional Programming Languages
Functional Programming Languages
Functional Programming Languages
Functional Programming Languages
15.2 Mathematical Functions
Functional Programming Languages
Functional Programming Languages
Chapter 15 Functional Programming 6/1/2019.
Functional Programming Languages
Presentation transcript:

1-1 An Introduction to Scheme March 2008

1-2 Introduction A mid-1970s dialect of LISP, designed to be a cleaner, more modern, and simpler version than the contemporary dialects of LISP Uses only static scoping Functions are first-class entities –They can be the values of expressions and elements of lists –They can be assigned to variables and passed as parameters

1-3 Primitive Functions 1. Arithmetic: +, -, *, /, ABS, SQRT, REMAINDER, MIN, MAX e.g., (+ 5 2) yields 7 2. QUOTE -takes one parameter; returns the parameter without evaluation –QUOTE is required because the Scheme interpreter, named EVAL, always evaluates parameters to function applications before applying the function. QUOTE is used to avoid parameter evaluation when it is not appropriate –QUOTE can be abbreviated with the apostrophe prefix operator e.g., '(A B) is equivalent to (QUOTE (A B))

1-4 Primitive Functions 3. CAR takes a list parameter; returns the first element of that list e.g., (CAR '(A B C)) yields A (CAR '((A B) C D)) yields (A B) 4. CDR takes a list parameter; returns the list after removing its first element e.g., (CDR '(A B C)) yields (B C) (CDR '((A B) C D)) yields (C D)

1-5 Primitive Functions 5. CONS takes two parameters, the first of which can be either an atom or a list and the second of which is a list; returns a new list that includes the first parameter as its first element and the second parameter as the remainder of its result e.g., (CONS 'A '(B C)) returns (A B C) 6. LIST - takes any number of parameters; returns a list with the parameters as elements

1-6 Lambda Expressions –Form is based on notation e.g., (LAMBDA (L) (CAR (CAR L))) L is called a bound variable Lambda expressions can be applied e.g., ((LAMBDA (L) (CAR (CAR L))) '((A B) C D)) Returns A

1-7 Scheme Functions A Function for Constructing Functions DEFINE - Two forms: 1. To bind a symbol to an expression e.g., (DEFINE pi ) (DEFINE two_pi (* 2 pi)) 2. To bind names to lambda expressions e.g., (DEFINE (cube x) (* x x x)) Example use: (cube 4) Return 64

1-8 Scheme Functions (Cont’d) Evaluation process (for normal functions): 1. Parameters are evaluated, in no particular order 2. The values of the parameters are substituted into the function body 3. The function body is evaluated 4. The value of the last expression in the body is the value of the function (Special forms use a different evaluation process)

1-9 Examples: (DEFINE (square x) (* x x)) e.g., (square 4) yields 16 (DEFINE (hypotenuse side1 side1) (SQRT (+ (square side1) (square side2)))) e.g., (hypotenuse 4 3) yields 5 Scheme Functions (Cont’d)

1-10 Predicate Functions: ( #T is true and () is false) 1. EQ? takes two symbolic parameters; it returns #T if both parameters are atoms and the two are the same e.g., (EQ? 'A 'A) yields #T (EQ? 'A '(A B)) yields () –Note that if EQ? is called with list parameters, the result is not reliable e.g., (EQ? ‘(A B) ‘(A B)) yields () Scheme Functions (Cont’d)

1-11 Predicate Functions: 2. LIST? takes one parameter; it returns #T if the parameter is a list; otherwise () e.g., (LIST? ‘(A B)) yields #T (LIST? 'A) yields () 3. NULL? takes one parameter; it returns #T if the parameter is the empty list; otherwise () e.g., (NULL? ‘()) yields #T (NULL? 'A) yields () (NULL? ‘(A B)) yields () Scheme Functions (Cont’d)

Numeric Predicate Functions =, <>, >, =, <=, EVEN?, ODD?, ZERO?, NEGATIVE? e.g., (= 3 3) yields #T (= 3 7) yields () (EVEN? 4) yields #T Output Utility Functions: (DISPLAY expression) (NEWLINE) e.g., (DISPLAY “Hello World”)) yields Hello World Scheme Functions (Cont’d)

1-13 Control Flow 1. Selection- the special form, IF (IF predicate then_exp else_exp) e.g., (DEFINE (fact n) (IF (= n 0) 1 (* N (fact (- N 1))))) e.g., (fact 5) yields 120 Scheme Functions (Cont’d)

1-14 Control Flow 2. Multiple Selection - the special form, COND General form: (COND (predicate_1 expr {expr}) (predicate_2 expr {expr})... (predicate_n expr {expr}) (ELSE expr {expr}) ) Returns the value of the last expr in the first pair whose predicate evaluates to true. Scheme Functions (Cont’d)

1-15 (DEFINE (compare x y) (COND ((> x y) (DISPLAY “x is greater than y”)) ((< x y) (DISPLAY “y is greater than x”)) (ELSE (DISPLAY “x and y are equal”)) ) e.g., (compare 3 4) yields y is greater than x Scheme Functions (Cont’d)

member - takes an atom and a simple list; returns #T if the atom is in the list; () otherwise (DEFINE (member atm lis) (COND ((NULL? lis) '()) ((EQ? atm (CAR lis)) #T) ((ELSE (member atm (CDR lis))) )) Example Scheme Functions

equalsimp - takes two simple lists as parameters; returns #T if the two simple lists are equal; () otherwise (DEFINE (equalsimp lis1 lis2) (COND ((NULL? lis1) (NULL? lis2)) ((NULL? lis2) '()) ((EQ? (CAR lis1) (CAR lis2)) (equalsimp(CDR lis1)(CDR lis2))) (ELSE '()) )) Example Scheme Functions

equal - takes two general lists as parameters; returns #T if the two lists are equal; () otherwise (DEFINE (equal lis1 lis2) (COND ((NOT (LIST? lis1))(EQ? lis1 lis2)) ((NOT (LIST? lis2)) '()) ((NULL? lis1) (NULL? lis2)) ((NULL? lis2) '()) ((equal (CAR lis1) (CAR lis2)) (equal (CDR lis1) (CDR lis2))) (ELSE '()) )) Example Scheme Functions

append - takes two lists as parameters; returns the first parameter list with the elements of the second parameter list appended at the end (DEFINE (append lis1 lis2) (COND ((NULL? lis1) lis2) (ELSE (CONS (CAR lis1) (append (CDR lis1) lis2))) )) Example Scheme Functions

1-20 The LET function –General form: (LET ( (name_1 expression_1) (name_2 expression_2)... (name_n expression_n)) expression { expression } ) –Semantics: Evaluate all expressions, then bind the values to the names; evaluate the body Example Scheme Functions

1-21 (DEFINE (quadratic_roots a b c) (LET ( (root_part_over_2a (/ (SQRT (- (* b b) (* 4 a c))) (* 2 a))) (minus_b_over_2a (/ (- 0 b) (* 2 a))) (DISPLAY (+ minus_b_over_2a root_part_over_2a)) (NEWLINE) (DISPLAY (- minus_b_over_2a root_part_over_2a)) )) Example Scheme Functions

1-22 Functional Forms 1. Composition Only primitive functional form provided by the original LISP. It is the essence of how EVAL works. All non-quoted lists are assumed to be function calls, which requires their parameters to be evaluated first. –Examples (cdr (cdr ‘(a b c))) (cons (car ‘(a b)) (cdr ‘(a b))) Functional Forms

1-23 Functional Forms 2. Apply to All One form in Scheme is mapcar Applies the given function to all elements of the given list; result is a list of the results –Example (DEFINE (mapcar fun lis) (COND ((NULL? lis) '()) (ELSE (CONS (fun (CAR lis)) (mapcar fun (CDR lis))))))

1-24 It is possible in Scheme to define a function that builds Scheme code and requests its interpretation using EVAL. This is possible because the interpreter is a user-available function, EVAL e.g., suppose we have a list of numbers that must be added together (DEFINE (adder lis) (COND ((NULL? lis) 0) (ELSE (+ (CAR lis) (adder(CDR lis )))) )) Functions that builds code

1-25 An alternative solution is to write a function that builds a call to + with propoer parameter forms. This can be done with cons to insert the atom + into the list. This new list can be submitted to EVAL for evaluation. ((DEFINE (adder lis) (COND ((NULL? lis) 0) (ELSE (EVAL (CONS '+ lis))) )) Functions that builds code

1-26 Scheme includes some imperative features: 1. SET! binds or rebinds a value to a name 2. SET-CAR! replaces the car of a list 3. SET-CDR! replaces the cdr part of a list Scheme Features