Local Definitions, Scope, Functional Abstraction, and Polymorphism.

Slides:



Advertisements
Similar presentations
Higher-Order Functions and Loops c. Kathi Fisler,
Advertisements

1 Programming Languages (CS 550) Lecture Summary Functional Programming and Operational Semantics for Scheme Jeremy R. Johnson.
Getting started with ML ML is a functional programming language. ML is statically typed: The types of literals, values, expressions and functions in a.
Lambda Calculus and Lisp PZ03J. Lambda Calculus The lambda calculus is a model for functional programming like Turing machines are models for imperative.
Clearly Visual Basic: Programming with Visual Basic 2008 Chapter 9 Decisions, Decisions, Decisions.
331 Final Fall Details 3:30-5:30 Monday, December 16 ITE 229 (this room!) Comprehensive with more emphasis on material since the midterm Look at.
Lists CS 5010 Program Design Paradigms “Bootcamp” Lesson 4.1 TexPoint fonts used in EMF. Read the TexPoint manual before you delete this box.: AAA 1 ©
Week 11 Recap CSE 115 Spring Want to write a programming language? You’ll need three things: You’ll need three things: –Sequencing –Selection Polymorphism.
Ormap, andmap, and filter CS 5010 Program Design Paradigms “Bootcamp” Lesson 5.3 TexPoint fonts used in EMF. Read the TexPoint manual before you delete.
Rewriting your function using map and foldr CS 5010 Program Design Paradigms “Bootcamp” Lesson 5.5 TexPoint fonts used in EMF. Read the TexPoint manual.
Recursion. Definitions I A recursive definition is a definition in which the thing being defined occurs as part of its own definition Example: A list.
Introduction Even though the syntax of Scheme is simple, it can be very difficult to determine the semantics of an expression. Hacker’s approach: Run it.
Subsets. Subsets are sort of like nested Russian dolls: the subset “fits inside” the set.
Local Definitions and Scope. Definitions We've seen global definitions: (define answer 42) ;; "42" is RHS (define (f x) (+ 1 x)) Today we'll look at local.
PPL Syntax & Formal Semantics Lecture Notes: Chapter 2.
Functional Programming Element of Functional Programming.
C++ Object Oriented 1. Class and Object The main purpose of C++ programming is to add object orientation to the C programming language and classes are.
Functional Programing Referencing material from Programming Language Pragmatics – Third Edition – by Michael L. Scott Andy Balaam (Youtube.com/user/ajbalaam)
CS 330 Programming Languages 11 / 21 / 2006 Instructor: Michael Eckmann.
ดร.สุรศักดิ์ มังสิงห์ SPU, Computer Science Dept.
Chapter Twenty-ThreeModern Programming Languages1 Formal Semantics.
Formal Semantics Chapter Twenty-ThreeModern Programming Languages, 2nd ed.1.
Generalizing Similar Functions CS 5010 Program Design Paradigms “Bootcamp” Lesson 5.1 TexPoint fonts used in EMF. Read the TexPoint manual before you delete.
Prepared by: Elsy Torres Shajida Berry Siobhan Westby.
Chapter 12 Object Oriented Design.  Complements top-down design  Data-centered view of design  Reliable  Cost-effective.
Python uses boolean variables to evaluate conditions. The boolean values True and False are returned when an expression is compared or evaluated.
331 Final Fall Details 3:30-5:30 Friday December 17 th LH4 Comprehensive with more emphasis on material since the midterm Study example finals and.
Abstraction: Procedures as Parameters CMSC Introduction to Computer Programming October 14, 2002.
Rewriting your function using map and foldr CS 5010 Program Design Paradigms “Bootcamp” Lesson TexPoint fonts used in EMF. Read the TexPoint manual.
ITEC 109 Lecture 11 While loops. while loops Review Choices –1 st –2 nd to ?th –Last What happens if you only use ifs? Can you have just an else by itself?
Cs1321 December 6, 2001 Review. What is computer science? What's an algorithm? Processes and programs Overview of some programming language concepts Functional.
Basic Scheme February 8, 2007 Compound expressions Rules of evaluation Creating procedures by capturing common patterns.
CMSC 330: Organization of Programming Languages Operational Semantics a.k.a. “WTF is Project 4, Part 3?”
Lecture on Set! And Local CS 2135 Copyright Kathi Fisler, 2002 This material requires Advanced Language Level.
Error Example - 65/4; ! Toplevel input: ! 65/4; ! ^^ ! Type clash: expression of type ! int ! cannot have type ! real.
Types and Programming Languages
Scope: What’s in a Name? CMSC Introduction to Computer Programming October 16, 2002.
COMP 412, FALL Type Systems II C OMP 412 Rice University Houston, Texas Fall 2000 Copyright 2000, Robert Cartwright, all rights reserved. Students.
1 FP Foundations, Scheme In Text: Chapter Chapter 14: FP Foundations, Scheme Mathematical Functions Def: A mathematical function is a mapping of.
Sometimes Structural Recursion Isn't Enough CS 5010 Program Design Paradigms “Bootcamp” Lesson 8.1 TexPoint fonts used in EMF. Read the TexPoint manual.
CS CS1321: Introduction to Programming Georgia Institute of Technology College of Computing Lecture 16 October 18, 2001 Fall Semester.
CSE 425: Functional Programming I Programs as Functions Some programs act like mathematical functions –Associate a set of input values from the function’s.
Today’s Agenda ML Development Workflow –Emacs –Using use –The REPL More ML –Shadowing Variables –Debugging Tips –Boolean Operations –Comparison Operations.
CSC 243 – Java Programming, Spring, 2014 Week 4, Interfaces, Derived Classes, and Abstract Classes.
PPL Syntax & Formal Semantics Lecture Notes: Chapter 2.
IF Statements flowcharts and pseudocode Please open the speaker notes - they contain additional information!
Generalizing Similar Functions
Basic Scheme February 8, 2007 Compound expressions Rules of evaluation
CS 326 Programming Languages, Concepts and Implementation
Representing Sets (2.3.3) Huffman Encoding Trees (2.3.4)
6.001 SICP Object Oriented Programming
CS 326 Programming Languages, Concepts and Implementation
Racket CSC270 Pepper major portions credited to
September 4, 1997 Programming Languages (CS 550) Lecture 6 Summary Operational Semantics of Scheme using Substitution Jeremy R. Johnson TexPoint fonts.
PRINCIPALES OF OBJECT ORIENTED PROGRAMMING
Case Study: Undefined Variables
CSE 341: Programming Languages Section 1
The Metacircular Evaluator
FP Foundations, Scheme In Text: Chapter 14.
Coding Concepts (Basics)
The Metacircular Evaluator (Continued)
Streams, Delayed Evaluation and a Normal Order Interpreter
Let's Play "What's the Question"
6.001 SICP Variations on a Scheme
If-statements & Indefinite Loops
6.001 SICP Interpretation Parts of an interpreter
topics interpreters meta-linguistic abstraction eval and apply
Generalizing Similar Functions
Rewriting your function using map and foldr
C++ Object Oriented 1.
Presentation transcript:

Local Definitions, Scope, Functional Abstraction, and Polymorphism

Definitions We've seen global definitions: (define answer 42) ;; "42" is RHS (define (f x) (+ 1 x)) Today we'll look at local definitions: (local ((define answer 42) (define (f x) (+ 1 x))) (f answer)) ;; body

Formalities What does the BNF for local definitions look like? ::= … | (local (def + ) exp) Indentation style Semantics (informally)

Why "local"? Avoiding namespace pollution Example: Our insert sort function This is an example of encapsulation How useful is this concept? Reuse name, avoid complex contracts, organize, hiding implementation detail Avoiding repeated work Consider: ;; last-occurrence: ;; x-value, [posn] -> y-value or false

Variables and Scope Recall: (local ((define answer 1 42) (define (f 2 x 3 ) (+ 1 x 4 ))) (f 5 answer 6 )) Variable occurrences: 1-6 Binding (or defining) occurrences: 1,2,3 Use occurrences: 4,5,6 Scopes: 1:(all of local statement), 2:(all of local statement), 3:(+1 x)

Renaming Recall: (local ((define answer 1 42) (define (f 2 x 3 ) (+ 1 x 4 ))) (f 5 answer 6 )) ? variables can be safely renamed But only ? can be renamed in (define answer 1 42) (define (f 2 x 3 ) (+ 1 x 4 ))

Abstracting Designs “The elimination of repetitions is the most important step in the (program) editing process” – Textbook Lots of “cut and paste” is generally a bad thing. Anyone have this experience? “Abstractions” avoid this problem

Similarities in Functions Simple example: ;; contains-doll? : los -> boolean ;; to determine whether alos contains ;; the symbol 'doll (define (contains-doll? alos) (cond [(empty? alos) false] [else (or (symbol=? (first alos) 'doll) (contains-doll? (rest alos)))]))

Similarities in Functions Simple example: ;; contains-car? : los -> boolean ;; to determine whether alos contains ;; the symbol 'car (define (contains-car? alos) (cond [(empty? alos) false] [else (or (symbol=? (first alos) 'car) (contains-car? (rest alos)))]))

Similarities in Functions Fix: ;; contains? : symbol, los -> boolean ;; to determine whether alos contains ;; the symbol s (define (contains? s alos) (cond [(empty? alos) false] [else (or (symbol=? (first alos) s) (contains? s (rest alos)))]))

Similarities in Functions Fix: What used to be redundant code is now (define (contains-doll? alos) (contains? 'doll alos)) (define (contains-car? alos) (contains? 'car alos)) We’ll take this idea and run with it

It will get real cool up here… Second example: ;; below : lon number -> lon ;; to construct a list of those numbers ;; in alon that are below t (define (below alon t) (cond [(empty? alon) empty] [else (cond [(< (first alon) t) (cons (first alon) (below (rest alon) t))] [else (below (rest alon) t)])]))

It will get real cool up here… Second example: ;; above : lon number -> lon ;; to construct a list of those numbers ;; in alon that are above t (define (above alon t) (cond [(empty? alon) empty] [else (cond [(> (first alon) t) (cons (first alon) (above (rest alon) t))] [else (above (rest alon) t)])]))

It will get real cool up here… Fix: ;; filter : comparison, lon number -> lon ;; to construct a list of those numbers n ;; in alon such that (test t n) is true (define (filter test alon t) (cond [(empty? alon) empty] [else (cond [(test (first alon) t) (cons (first alon) (filter test (rest alon) t))] [else (filter test (rest alon) t)])])) How can you even use this thing?

It will get real cool up here… Fix: The magic moment: (define (below alon t) (filter > alon t)) (define (below alon t) (filter < alon t)) Both functions will work just as before

Similarity in Types Good news: You already know this: [number], [symbol], [bool] have similar definitions We’ve already been using [X] Defining [X] requires is variables at the level of types…the X in [X]! The book uses the intuitive (listOf X) Often called polymorphism, or generics.

Final thought Function abstraction gives a lot of expressivity Polymorphism does the same Together, both work really well Things will continue to get better as we add abstraction mechanisms to our language