Abstraction and Repetition

Slides:



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

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.
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.
CSE (c) S. Tanimoto, 2007 Python Introduction 1 Introduction to Python for Artificial Intelligence Outline: Why Python? Interactive programming,
Functional programming: LISP Originally developed for symbolic computing Main motivation: include recursion (see McCarthy biographical excerpt on web site).
The Scheme Programming Language History and Significance Dmitry Nesvizhsky CIS24 Professor Danny Kopec.
Chapter 15 Functional Programming Languages. Copyright © 2007 Addison-Wesley. All rights reserved. 1–2 Introduction Design of imperative languages is.
Functional programming: LISP Originally developed for symbolic computing First interactive, interpreted language Dynamic typing: values have types, variables.
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.
CSC3315 (Spring 2009)1 CSC 3315 Programming Paradigms Scheme Language Hamid Harroud School of Science and Engineering, Akhawayn University
ISBN Chapter 15 Functional Programming Languages.
CSE 341, S. Tanimoto Lisp Defining Functions with DEFUN Functions are the primary abstraction mechanism available in Lisp. (Others are structures.
COP4020 Programming Languages Functional Programming Prof. Xin Yuan.
18-October-2002cse Symbols © 2002 University of Washington1 Symbols CSE 413, Autumn 2002 Programming Languages
COP4020 Programming Languages Functional Programming Prof. Xin Yuan.
1 Programming Languages (CS 550) Lecture 4 Summary Functional Programming and Operational Semantics for Scheme Jeremy R. Johnson.
1 FP Foundations, Scheme In Text: Chapter Chapter 14: FP Foundations, Scheme Mathematical Functions Def: A mathematical function is a mapping of.
CS 152: Programming Language Paradigms February 12 Class Meeting Department of Computer Science San Jose State University Spring 2014 Instructor: Ron Mak.
1 Introduction to Functional Programming in Racket CS 270 Math Foundations of CS Jeremy Johnson.
COP4020 Programming Languages Functional Programming Prof. Robert van Engelen (modified by Prof. Em. Chris Lacher)
Functional Programming
Functional Programming
History of Computing – Lisp
Basic Scheme February 8, 2007 Compound expressions Rules of evaluation
6.001 Jeopardy.
6.001 SICP Variations on a Scheme
CSC 533: Programming Languages Spring 2017
CS 326 Programming Languages, Concepts and Implementation
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
CSC 533: Programming Languages Spring 2016
Closures and Streams cs784(Prasad) L11Clos
COP4020 Programming Languages
Control Flow Chapter 6.
The Metacircular Evaluator
FP Foundations, Scheme In Text: Chapter 14.
Dynamic Scoping Lazy Evaluation
The Metacircular Evaluator
What would be our focus ? Geometry deals with Declarative or “What is” knowledge. Computer Science deals with Imperative or “How to” knowledge 12/25/2018.
CS 36 – Chapter 11 Functional programming Features Practice
The Metacircular Evaluator (Continued)
CSE S. Tanimoto Explicit Function Application
Explicit Application of Procedures, and Explicit Evaluation
Streams, Delayed Evaluation and a Normal Order Interpreter
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.
6.001 SICP Variations on a Scheme
Dan Grossman / Eric Mullen Autumn 2017
Defining Functions with DEFUN
The structure of programming
Abstraction and Repetition
Today’s topics Abstractions Procedural Data
Functional Programming: Lisp
6.001 SICP Interpretation Parts of an interpreter
COP4020 Programming Languages
The structure of programming
Introduction to the Lab
topics interpreters meta-linguistic abstraction eval and apply
Common Lisp II.
Thinking procedurally
Chapter 15 Functional Programming 6/1/2019.
More Scheme CS 331.
Defining Macros in Scheme
Lists in Lisp and Scheme
Abstraction and Repetition
Presentation transcript:

Abstraction and Repetition Procedures as code abstraction Defining functions in Scheme Recursion Recursion on lists Looping constructs CSE 341 -- S. Tanimoto Procedural Abstraction and Repetition

CSE 341 -- S. Tanimoto Procedural Abstraction and Repetition Code Abstraction Any programming language should provide ways to specify a lot of computation with a little code. Execution sequences that contain a lot of similar subcomputations should be represented using abstractions: An abstraction is a representation of what two or more things have in common. E.g. Two or more iterations of a loop have the sequence of instructions in the loop body in common. E.g., Two square-root computations have the square-root algorithm in common. CSE 341 -- S. Tanimoto Procedural Abstraction and Repetition

Defining Procedures with DEFINE Procedures are the primary abstraction mechanism available in Scheme. (Others are structures and macros -- as well as iteration constructs). Non-built-in procedures are usually defined using the special form DEFINE. > (define (sqr x) (* x x)) > (sqr 9) 81 > (sqr 1.5) 2.25 CSE 341 -- S. Tanimoto Procedural Abstraction and Repetition

Procedures with no arguments A procedure can take zero, one, or more arguments. > (define (say-hello) ; prints a greeting. (display "Hello there!") (newline) ) > (say-hello) Hello there! > CSE 341 -- S. Tanimoto Procedural Abstraction and Repetition

COND (an alternative to IF) The COND form uses of a kind of abstraction from nested IF forms. > (define (small-prime? n) ; Returns T if n is a small prime. (cond ((= n 2) #t) ((= n 3) #t) ((= n 5) #t) ((= n 7) #t) (#t #f) ) ) > (small-prime? 9) #f > (small-prime? 3) #t CSE 341 -- S. Tanimoto Procedural Abstraction and Repetition

CSE 341 -- S. Tanimoto Procedural Abstraction and Repetition Recursive Procedures A procedure may be defined in terms of itself. > (define (factorial n) ; Returns factorial of N. (if (= n 1) 1 (* n (factorial (- n 1))) ) ) > (factorial 5) 120 > (factorial 20) 2432902008176640000 CSE 341 -- S. Tanimoto Procedural Abstraction and Repetition

Primitive List-Manipulating Functions > (define x '(a b c d)) > (car x) ; CAR returns 1st elt of list A > (cdr x) ; CDR returns all but 1st. (B C D) > x (A B C D) ; X has not been changed. > (cons 'a 'b) (A . B) ; CONS combines two things. > (cons 'a ()) (A) ; The result is often a list. > (cons 'z x) (Z A B C D) CSE 341 -- S. Tanimoto Procedural Abstraction and Repetition

CAR, CDR, and their combinations > (set! x '(a b c d)) (A B C D) > (cdr (cdr x)) (C D) > (cddr x) > (caddr x) C ; CADDR selects the 3rd elt. CSE 341 -- S. Tanimoto Procedural Abstraction and Repetition

Recursive Functions of Lists > (define (censor lst) ; Returns LST with no instances of BAD. (cond ((null? lst) ()) ((eq? (car lst) 'BAD) (censor (cdr lst)) ) (#t (cons (car lst) (censor (cdr lst)) ) ) ) ) CENSOR > (censor '(This is a bad bad list)) (THIS IS A LIST) CSE 341 -- S. Tanimoto Procedural Abstraction and Repetition

One-Way Recursion Doesn’t Do Sublists > (censor '(This bad list (has a bad sublist))) (THIS LIST (HAS A BAD SUBLIST)) CSE 341 -- S. Tanimoto Procedural Abstraction and Repetition

Two-Way Recursive Functions > (define (censor2 lst) ; Returns LST with no instances of BAD. (cond ((null? lst) ()) ((not (list? Lst)) lst) ((eq? (car lst) 'BAD) (censor2 (cdr lst)) ) (#t (cons (censor2 (car lst)) (censor2 (cdr lst)) ) ) ) ) CENSOR2 > (censor2 '(This bad list (has a bad sublist))) (THIS LIST (HAS A SUBLIST)) CSE 341 -- S. Tanimoto Procedural Abstraction and Repetition

CSE 341 -- S. Tanimoto Procedural Abstraction and Repetition Looping: DO > (define (promise n) ; Prints a promise N times. (let ((the-promise "I will balance parentheses")) (do ((i 0 (+ i 1))) ((= i n)) (display i) (display ". ") (display the-promise) (newline) ) ) ) > (promise 3) 0. I will balance parentheses. 1. I will balance parentheses. 2. I will balance parentheses. CSE 341 -- S. Tanimoto Procedural Abstraction and Repetition

Looping: DO (2nd example) > (define (print-on-separate-lines lst) ; Prints LST with one line per element. (do ((tmp lst (cdr tmp))) ((null? tmp)) (let ((elt (car tmp))) (display elt) (newline) ) ) ) > (print-on-separate-lines '(lunch around the corner) ) LUNCH AROUND THE CORNER CSE 341 -- S. Tanimoto Procedural Abstraction and Repetition

CSE 341 -- S. Tanimoto Procedural Abstraction and Repetition Pure Functions A pure function is one whose returned value depends only on its arguments (i.e., it possesses referential transparency), and it does not have any side effects. (define (plus3 x)(+ x 3)) ; a pure function (define (plus3-with-y x) ; not a pure function (set! y 3) (+ x y) ) (define (plus3-with-z x) ; not pure unless (+ x z) ) ; z is constant CSE 341 -- S. Tanimoto Procedural Abstraction and Repetition

Functional Programming Pure functional programming is programming using only pure functions. No assignment statements, no loops. CSE 341 -- S. Tanimoto Procedural Abstraction and Repetition