Programming Languages

Similar documents
Functional Languages. Hwansoo Han

COP4020 Programming Languages. Functional Programming Prof. Robert van Engelen

LECTURE 16. Functional Programming

Scheme. Functional Programming. Lambda Calculus. CSC 4101: Programming Languages 1. Textbook, Sections , 13.7

Functional Programming. Pure Functional Languages

11/6/17. Functional programming. FP Foundations, Scheme (2) LISP Data Types. LISP Data Types. LISP Data Types. Scheme. LISP: John McCarthy 1958 MIT

Chapter 11 :: Functional Languages

4/19/2018. Chapter 11 :: Functional Languages

Functional Programming. Pure Functional Languages

A Brief Introduction to Scheme (II)

Functional Languages. CSE 307 Principles of Programming Languages Stony Brook University

CS 314 Principles of Programming Languages

Organization of Programming Languages CS3200/5200N. Lecture 11

Fall 2018 Discussion 8: October 24, 2018 Solutions. 1 Introduction. 2 Primitives

Scheme Quick Reference

Functional Programming. Pure Functional Programming

Functional Programming

Summer 2017 Discussion 10: July 25, Introduction. 2 Primitives and Define

Functional Programming. Big Picture. Design of Programming Languages

SCHEME 8. 1 Introduction. 2 Primitives COMPUTER SCIENCE 61A. March 23, 2017

Scheme Quick Reference

CS 314 Principles of Programming Languages. Lecture 16

Documentation for LISP in BASIC

Typed Racket: Racket with Static Types

Fundamentals of Artificial Intelligence COMP221: Functional Programming in Scheme (and LISP)

Principles of Programming Languages Topic: Functional Programming Professor L. Thorne McCarty Spring 2003

Spring 2018 Discussion 7: March 21, Introduction. 2 Primitives

Fall 2017 Discussion 7: October 25, 2017 Solutions. 1 Introduction. 2 Primitives

Essentials of Programming Languages Language

Essentials of Programming Languages Language

CPS 506 Comparative Programming Languages. Programming Language Paradigm

CSc 520 Principles of Programming Languages

Computer Science 21b (Spring Term, 2015) Structure and Interpretation of Computer Programs. Lexical addressing

User-defined Functions. Conditional Expressions in Scheme

Chapter 15. Functional Programming Languages

Principles of Programming Languages 2017W, Functional Programming

Programming Language Pragmatics

SCHEME 7. 1 Introduction. 2 Primitives COMPUTER SCIENCE 61A. October 29, 2015

CSC 533: Programming Languages. Spring 2015

Project 2: Scheme Interpreter

An Introduction to Scheme

Functional Programming Languages (FPL)

FP Foundations, Scheme

CSE 341 Section Handout #6 Cheat Sheet

Chapter 15 Functional Programming Languages

Scheme in Scheme: The Metacircular Evaluator Eval and Apply

CS61A Midterm 2 Review (v1.1)

Modern Programming Languages. Lecture LISP Programming Language An Introduction

Scheme Tutorial. Introduction. The Structure of Scheme Programs. Syntax

Principles of Programming Languages COMP251: Functional Programming in Scheme (and LISP)

The Typed Racket Guide

Announcements. The current topic: Scheme. Review: BST functions. Review: Representing trees in Scheme. Reminder: Lab 2 is due on Monday at 10:30 am.

Typed Scheme: Scheme with Static Types

Recursive Functions of Symbolic Expressions and Their Computation by Machine Part I

Imperative languages

Scheme: Strings Scheme: I/O

Programming Systems in Artificial Intelligence Functional Programming

Haskell 98 in short! CPSC 449 Principles of Programming Languages

Programming Languages

SOFTWARE ARCHITECTURE 6. LISP

Functional Programming and Haskell

Scheme: Data. CS F331 Programming Languages CSCE A331 Programming Language Concepts Lecture Slides Monday, April 3, Glenn G.

Lambda Calculus see notes on Lambda Calculus

Introduction to Scheme

SCHEME AND CALCULATOR 5b

CS 314 Principles of Programming Languages

6.034 Artificial Intelligence February 9, 2007 Recitation # 1. (b) Draw the tree structure corresponding to the following list.

SCHEME 10 COMPUTER SCIENCE 61A. July 26, Warm Up: Conditional Expressions. 1. What does Scheme print? scm> (if (or #t (/ 1 0)) 1 (/ 1 0))

Functional Languages. Programming Language Pragmatics. DOI: /B Copyright 2009 by Elsevier Inc. All rights reserved.

CS 320 Homework One Due 2/5 11:59pm

Introduction to Functional Programming in Racket. CS 550 Programming Languages Jeremy Johnson

Functional Programming Lecture 1: Introduction

Lambda Calculus and Lambda notation in Lisp II. Based on Prof. Gotshalks notes on Lambda Calculus and Chapter 9 in Wilensky.

Streams, Delayed Evaluation and a Normal Order Interpreter. CS 550 Programming Languages Jeremy Johnson

CS 314 Principles of Programming Languages

6.184 Lecture 4. Interpretation. Tweaked by Ben Vandiver Compiled by Mike Phillips Original material by Eric Grimson

Comp 311: Sample Midterm Examination

Why do we need an interpreter? SICP Interpretation part 1. Role of each part of the interpreter. 1. Arithmetic calculator.

(Func&onal (Programming (in (Scheme)))) Jianguo Lu

cs61amt2_4 CS 61A Midterm #2 ver March 2, 1998 Exam version: A Your name login: cs61a- Discussion section number TA's name

More Scheme CS 331. Quiz. 4. What is the length of the list (()()()())? Which element does (car (cdr (x y z))) extract from the list?

Introduction to Typed Racket. The plan: Racket Crash Course Typed Racket and PL Racket Differences with the text Some PL Racket Examples

6.037 Lecture 4. Interpretation. What is an interpreter? Why do we need an interpreter? Stages of an interpreter. Role of each part of the interpreter

Quick announcement. Midterm date is Wednesday Oct 24, 11-12pm.

Lambda Calculus. CS 550 Programming Languages Jeremy Johnson

Functional Programming Languages (FPL)

Deferred operations. Continuations Structure and Interpretation of Computer Programs. Tail recursion in action.

Building a system for symbolic differentiation

Lecture 09: Data Abstraction ++ Parsing is the process of translating a sequence of characters (a string) into an abstract syntax tree.

Lecture #24: Programming Languages and Programs

COP4020 Programming Assignment 1 - Spring 2011

Administrivia. Simple data types

Scheme: Expressions & Procedures

MORE SCHEME. 1 What Would Scheme Print? COMPUTER SCIENCE MENTORS 61A. October 30 to November 3, Solution: Solutions begin on the following page.

6.001: Structure and Interpretation of Computer Programs

INTRODUCTION TO SCHEME

SCHEME The Scheme Interpreter. 2 Primitives COMPUTER SCIENCE 61A. October 29th, 2012

Example Scheme Function: equal

CSE 413 Languages & Implementation. Hal Perkins Winter 2019 Structs, Implementing Languages (credits: Dan Grossman, CSE 341)

Turtles All The Way Down

Transcription:

Programming Languages Tevfik Koşar Lecture - XIII March 2 nd, 2006 1 Roadmap Functional Languages Lambda Calculus Intro to Scheme Basics Functions Bindings Equality Testing Searching 2 1

Functional Languages Functional languages make heavy use of subroutines (more than Van Neumann languages or any other language class) Examples: Scheme Lisp Miranda Haskell Sisal ph ML 3 Lambda Calculus Lambda Calculus: inspiration for functional programming Based on parameterized expressions (each parameter introduced by the occurrence of the letter λ) Used to compute by substituting parameters into expressions Output of a program is defined as a mathematical function of the inputs, with no notion of internal state and, and thus no side effects Unless explicit use of assignment (set! function) 4 2

Functional Programming Concepts Features of functional programming languages which are often missing in other languages; first class function values: values that can be passed as parameters to subroutines or returned from a subroutine. high-order functions: functions which take other functions as arguments or return a function as a result. extensive polymorphism: allow a function to be used on a class of arguments list types and operators: recursion structured function returns constructors for structures objects garbage collection 5 Intro to Scheme Scheme uses Cambridge Polish notation for expressions (preorder). Eg. (+ 3 4) First argument inside the left parenthesis is the function, the remaining are its arguments Scheme interpreter runs a read-eval-print loop: >> (+3 4) 7 >> 8 8 >> ((+ 3 4)) Error! 6 3

Intro to Scheme Load function: >> (load my_scheme_program ) Quote: (instead of evaluating) >> (quote (+ 3 4)) (+ 3 4) >> (+ 3 4) (+ 3 4) 7 Conditional statements ( if <comp> <expr1> <expr2>) >> (if (> a 0) (+ a 2) (- a 2) ) a+2 or a-2 >> (if (> a 0) (+ a 2) (- a foo ) ) Error! >> (cond 3 ((< 3 2) 1) ((< 4 3) 2) (else 3)) 8 4

Function definitions User defined functions: >> (define min (lambda (a b) (if (< a b) a b) ) ) >> (min x y) >> (min 24 45) Predefined functions: (boolean? x) (char? x) (string? x) (symbol? x) (number? x) (pair? x) (list? x) #t or #f 9 Bindings >> (let ( (a 3) (b 4) (square (lambda (x) (* x x))) (plus +)) (sqrt (plus (square a) (square b))) ) 5 >> (let ((a 3)) (let ((a 4) (b a)) (+ a b))) 7 10 5

Bindings let: does not allow recursive calls all at once visibility at the end of the declaration letrec: allows recursive calls let*: one at a time visibility 11 Bindings >> (letrec ((fact (lambda (n) if (= n 1) 1 ( * n (fact (- n 1) )))))) fact 5)) 120 12 6

Lists and Numbers car: returns the head of a list cdr: returns the rest of the list (everything after the head) cons: joins a head to the rest of the list Take the list (2 3 4) >> (car `(2 3 4)) 2 >> (cdr `(2 3 4)) (3 4) >> (cons 1 `(2 3 4)) (1 2 3 4) 13 Lists and Numbers null? predicate determines whether its argument is the empty list >>(null? (cdr `(2))) #t 14 7

Equality Testing and Searching eq? tests whether its arguments refer to the same object eqv? tests whether its arguments are semantically equivalent equal? tests whether its arguments have the same recursive structure with semantically equivalent leaves 15 Equality Testing eq? : Return `#t' if X and Y are the same object, except for numbers and characters. For example, >>(define x (vector 1 2 3)) >>(define y (vector 1 2 3)) >>(eq? x x) => #t >>(eq? x y) => #f 16 8

Equality Testing eqv? : Return `#t' if X and Y are the same object, or for characters and numbers the same value. On objects except characters and numbers, `eqv?' is the same as `eq?' above, it's true if X and Y are the same object. If X and Y are numbers or characters, `eqv?' compares their type and value. An exact number is not `eqv?' to an inexact number (even if their value is the same). >> (eqv? 3 (+ 1 2)) => #t >> (eqv? 1 1.0) => #f 17 Equality Testing equal? : Return `#t' if X and Y are the same type, and their contents or value are equal. For a pair, string, vector or array, `equal?' compares the contents, and does so using the same `equal?' recursively, so a deep structure can be traversed. >> (equal? (list 1 2 3) (list 1 2 3)) => #t >> (equal? (list 1 2 3) (vector 1 2 3)) => #f For other objects, `equal?' compares as per `eqv?' above, which means characters and numbers are compared by type and value (and like `eqv?', exact and inexact numbers are not `equal?', even if their value is the same). >> (equal? 3 (+ 1 2)) => #t >> (equal? 1 1.0) => #f 18 9

Searching memq memv member -> uses eq? -> uses eqv? -> uses equal? Take an element and a list as an argument, return the longest suffix of the list beginning with the element >> (memq `z `(x y z w)) (z w) >> (memq `(z) `(x y (z) w)) #f >> (member `(z) `(x y (z) w)) ((z) w) 19 Useful Links An Introduction to Scheme and its Implementation http://www.federated.com/~jim/schintrov14/schintro_toc.html The Scheme Homepage http://www.swiss.ai.mit.edu/projects/scheme/ Scheme Interpreter (MIT/GNU) http://www.gnu.org/software/mit-scheme/ 20 10

Midterm will cover: Ch 1.1-1.6 Ch 2.1-2.3 Ch 3.1-3.3 Ch 4.1-4.6 Ch 6.1-6.7 Ch 10.1-10.5 Scheme 21 11