CS 326 Programming Languages, Concepts and Implementation

Slides:



Advertisements
Similar presentations
CS 63 LISP Philip Greenspun's Tenth* Rule of Programming:
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.
1 Scheme and Functional Programming Aaron Bloomfield CS 415 Fall 2005.
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.
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.
1 Programming Languages (CS 550) Lecture Summary Functional Programming and Operational Semantics for Scheme Jeremy R. Johnson.
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.
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. 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.
CMSC 471 LISP. Why Lisp? Because it’s the most widely used AI programming language Because it’s good for writing production software (Graham article)
Functional programming: LISP Originally developed for symbolic computing First interactive, interpreted language Dynamic typing: values have types, variables.
Recursion in Scheme recursion is the basic means for expressing repetition some recursion is on numbers –factorial –fibonacci some recursion is on structures.
1 (Functional (Programming (in (Scheme)))) Jianguo Lu.
Functional Programing Referencing material from Programming Language Pragmatics – Third Edition – by Michael L. Scott Andy Balaam (Youtube.com/user/ajbalaam)
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
ISBN Chapter 15 Functional Programming Languages.
CS 330 Programming Languages 11 / 21 / 2006 Instructor: Michael Eckmann.
Λ => Scheme for Rubyists. Scheme History Authors: Guy Steele and Gerald Sussman Structure and Interpretation of Computer Programs (SICP) by Abelson &
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.
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.
Milos Hauskrecht (PDF) Hieu D. Vu (PPT) LISP PROGARMMING LANGUAGE.
1 FP Foundations, Scheme In Text: Chapter Chapter 14: FP Foundations, Scheme Mathematical Functions Def: A mathematical function is a mapping of.
CSE 425: Functional Programming I Programs as Functions Some programs act like mathematical functions –Associate a set of input values from the function’s.
Computer Eng. Software Lab II , Semester 2, Who I am: Andrew Davison CoE, WiG Lab Office Functional Programming.
Comparative Programming Languages Functional programming with Lisp/Scheme.
CS314 – Section 5 Recitation 9
Operational Semantics of Scheme
Functional Programming
CS314 – Section 5 Recitation 10
Functional Programming Languages
User-Written Functions
Basic Scheme February 8, 2007 Compound expressions Rules of evaluation
Section 15.4, 15.6 plus other materials
CS 326 Programming Languages, Concepts and Implementation
CS 326 Programming Languages, Concepts and Implementation
CS 326 Programming Languages, Concepts and Implementation
CS 326 Programming Languages, Concepts and Implementation
CS 326 Programming Languages, Concepts and Implementation
Chapter 15 – Functional Programming Languages
September 4, 1997 Programming Languages (CS 550) Lecture 6 Summary Operational Semantics of Scheme using Substitution Jeremy R. Johnson TexPoint fonts.
Lists in Lisp and Scheme
Env. Model Implementation
Introduction to Functional Programming in Racket
The Metacircular Evaluator
FP Foundations, Scheme In Text: Chapter 14.
Functional Programming Languages
Functional Programming
This Lecture Substitution model
CS 36 – Chapter 11 Functional programming Features Practice
The Metacircular Evaluator (Continued)
6.001 SICP Further Variations on a Scheme
Streams, Delayed Evaluation and a Normal Order Interpreter
6.001 SICP Variations on a Scheme
Introduction to Functional Programming in Racket
Announcements Quiz 5 HW6 due October 23
Lecture 2 מבוא מורחב.
This Lecture Substitution model
6.001 SICP Interpretation Parts of an interpreter
This Lecture Substitution model
topics interpreters meta-linguistic abstraction eval and apply
Lecture 2 מבוא מורחב.
Lisp.
Presentation transcript:

CS 326 Programming Languages, Concepts and Implementation Instructor: Mircea Nicolescu Lecture 6 Good afternoon and thank you everyone for coming. My talk today will describe the research I performed at the IRIS at USC, the object of this work being to build a computational framework that addresses the problem of motion analysis and interpretation.

List Operations null? – returns #t if the list is null () #f otherwise list – returns a list built from its arguments (list 'a 'b 'c) => (a b c) (list 'a) => (a) (list '(a b c)) => (list '(a b) 'c) => (list '(a b) '(c d)) => ((a b c)) ((a b) c) ((a b) (c d))

List Operations length – returns the length of a list (length '((a b) c)) => reverse – returns the list reversed (reverse '(1 3 5 7)) => (7 5 3 1) (reverse '((a b) c)) => append – returns the concatenation of the lists received as arguments (append '(1 3 5) '(7 9)) => (1 3 5 7 9) (append '(a) '()) => (a) (append '(a b) '((c d) e)) => 2 (c (a b)) (a b (c d) e)

Type Predicates Check the type of the argument and return #t or #f (boolean? x) ; is x a boolean? (char? x) ; is x a char? (string? x) ; is x a string? (symbol? x) ; is x a symbol? (number? x) ; is x a number? (list? x) ; is x a list? (procedure? x) ; is x a procedure (function)?

Boolean Expressions (< 1 2) => #t (>= 3 4) => #f (= 4 4) => #t (eq? '(a b) '(a b)) => #f ; same object? (equal? '(a b) '(a b)) => #t ; recursively equivalent structure? (not (> 5 6)) => #t (and (< 3 4) (= 2 3)) => #f (or (< 3 4) (= 2 3)) => #t and, or are special forms - evaluate arguments only while needed

Conditional Expressions if – has the form: (if <test_exp> <then_exp> <else_exp>) (if (< 5 6) 1 2) => 1 (if (< 4 3) 1 2) => 2 Anything other than #f is treated as true: (if 3 4 5) => 4 (if '() 4 5) => 4 ; as opposed to Lisp!! if is a special form - evaluates its arguments only when needed: (if (= 3 4) 1 (2)) => Error: attempt to apply non-procedure 2. (if (= 3 3) 1 (2)) => 1

Conditional Expressions cond – has the form: (cond (<test_exp1> <exp1> ...) (<test_exp2> <exp2> ...) ... (else <exp> ...)) (define n -5) (cond ((< n 0) "negative") ((> n 0) "positive") (else "zero")) => "negative" cond is a special form - evaluates its arguments only when needed

Syntax (C vs. Scheme) C Scheme 1 + 2 + 3 3 + 4 * 5 factorial (9) (a == b) && (c != 0) (low < x) && (x < high) f (g(2,-1), 7) Scheme (+ 1 2 3) (+ 3 (* 4 5)) (factorial 9) (and (= a b) (not (= c 0))) (< low x high) (f (g 2 -1) 7)

Functions Create a function by evaluating a lambda expression: (lambda (id1 id2 ...) exp1 exp2 ...) id1 id2 ... - formal parameters exp1 exp1 ... - body of the function return value of function - last expression in body return value of lambda expression - the (un-named) function (lambda (x) (* x x)) => #<procedure> Returns an un-named function that takes a parameter and returns its square

Functions Call a function by applying the evaluated lambda expression on its actual parameters: ((lambda (x) (* x x)) 3) => 9 the function the actual parameter How can you reuse the function? You can’t! Why is it then useful? Return a function from another function What if you REALLY want to reuse it?

Functions Bind a name to a function: (define square (lambda (x) (* x x))) Equivalent short-hand notation (typical way to use it): (define (square x) (* x x)) Now call the function: (square 3) => 9 #<procedure> square

Functions Functions vs. variables: Last definition is equivalent to: (define f 3) f => 3 (f) => (define (f) 3) f => Error: attempt to apply non-procedure 3. #<procedure> 3 Last definition is equivalent to: (define f (lambda () 3)) ; a function that takes no parameters and ; returns 3

Functions C Scheme if (a == 0) return f(x,y) ; else return g(x,y) ;

Functions C Can we write it better in Scheme? Scheme if (a == 0) return f(x,y) ; else return g(x,y) ; Can we write it better in Scheme? repeated arguments (x,y) Scheme ((if (= a 0) f g) x y) evaluates to either #<procedure f> or #<procedure g> it is then applied on arguments x and y

Recursion Recursion plays a greater role in Scheme than in other languages Why? Functional programming – avoid side effects (assignments) and iterations Recursive data structures – a list is either empty, or has a car and a cdr; the cdr is (again) a list Elegance – recursive algorithms are considered more elegant than iterative ones (just ask a Scheme or Lisp programmer!)

Recursion How do you solve a problem recursively? Do not rush to implement it Think of a recursive way to describe the problem: Show how to solve the problem in the general case, by decomposing it into similar, but smaller problems Show how to solve the smallest version of the problem (the base case) Now the implementation should be straightforward (in ANY language) But don't forget to handle base case first when implementing

Recursion How do you THINK recursively? Example: define factorial factorial(n) = 1 * 2 * 3 * ...(n-1) * n factorial (n-1) 1 if n=1 (the base case) factorial(n) = n * factorial(n-1) otherwise (inductive step)

Recursion Implement factorial in Scheme: (define (factorial n) (if (= n 1) 1 (* n (factorial (- n 1))))) (factorial 4) => 24

Recursion Fibonacci: fib(n) = 1 if n = 1 Implement in Scheme: fib(n-1) + fib(n-2) otherwise Implement in Scheme: (define (fib n) (cond ((= n 0) 0) ((= n 1) 1) (else (+ (fib (- n 1)) (fib (- n 2))))))

Interacting with Scheme Instead of writing everything at the Scheme prompt, you can: write your function definitions and your global variable definitions (define...) in a file ("file_name") at the Scheme prompt, load the file with: (load "file_name") at the Scheme prompt call the desired functions there is no "formal" main function You can also invoke Scheme directly from Emacs: M-x run-scheme this will open another buffer, showing the Scheme prompt then you can switch between the buffer where you edit your program and the buffer with the Scheme prompt BONUS: when closing a parenthesis, Emacs will highlight the corresponding open one

Announcements Homework HW 2 out – due on September 28 Submission - at the beginning of class a paper version of your code with a title page: Name, Class, Assignment #, Date and send an email to cs326@cse.unr.edu containing: subject: HW 2 message body: your name attachment: the electronic version of your code (only one file with all the functions) do not forget about indentation!