Imperative languages

Similar documents
Functional Programming. Big Picture. Design of Programming Languages

CPS 506 Comparative Programming Languages. Programming Language Paradigm

It is better to have 100 functions operate one one data structure, than 10 functions on 10 data structures. A. Perlis

CSC312 Principles of Programming Languages : Functional Programming Language. Copyright 2006 The McGraw-Hill Companies, Inc.

FP Foundations, Scheme

Chapter 15. Functional Programming Languages

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

COP4020 Programming Languages. Functional Programming Prof. Robert van Engelen

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

Functional Programming. Pure Functional Languages

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

Chapter 15. Functional Programming Languages

Lisp Basic Example Test Questions

LECTURE 16. Functional Programming

Functional Programming. Pure Functional Programming

Functional Programming. Pure Functional Languages

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

CS 314 Principles of Programming Languages

Functional Programming

15. Functional Programming

Functional Languages. Hwansoo Han

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

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

Organization of Programming Languages CS3200/5200N. Lecture 11

According to Larry Wall (designer of PERL): a language by geniuses! for geniuses. Lecture 7: Haskell. Haskell 98. Haskell (cont) - Type-safe!

CSCE 314 TAMU Fall CSCE 314: Programming Languages Dr. Flemming Andersen. Haskell Functions

Functional Programming and Haskell

Example Scheme Function: equal

Shell CSCE 314 TAMU. Haskell Functions

Programming Languages

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

Functional Programming Languages (FPL)

Programming Languages

CS 314 Principles of Programming Languages

Functional programming in LISP

CSc 372. Comparative Programming Languages. 2 : Functional Programming. Department of Computer Science University of Arizona

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

Functional Programming. Functional Programming

Chapter 15. Functional Programming Languages ISBN

Programming Systems in Artificial Intelligence Functional Programming

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

CMSC 331 Final Exam Section 0201 December 18, 2000

Principles of Programming Languages 2017W, Functional Programming

Concepts of Programming Languages

Logic - CM0845 Introduction to Haskell

Introduction to Functional Programming in Haskell 1 / 56

Week 4: Functional Programming

Principles of Programming Languages

Modern Programming Languages. Lecture LISP Programming Language An Introduction

ITERATORS AND STREAMS 9

Chapter 15 Functional Programming Languages

Introduction to LISP. York University Department of Computer Science and Engineering. York University- CSE V.

Functional Programming in Scheme

Haskell Overview II (2A) Young Won Lim 8/9/16

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

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

Functional Programming

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

Lecture 19: Functions, Types and Data Structures in Haskell

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

Lecture #13: Type Inference and Unification. Typing In the Language ML. Type Inference. Doing Type Inference

SOFTWARE ARCHITECTURE 6. LISP

Functional Programming Languages (FPL)

Documentation for LISP in BASIC

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

CS 320: Concepts of Programming Languages

Box-and-arrow Diagrams

User-defined Functions. Conditional Expressions in Scheme

Imperative, OO and Functional Languages A C program is

Functional Programming Lecture 1: Introduction

Lisp. Versions of LISP

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

Haskell: Lists. CS F331 Programming Languages CSCE A331 Programming Language Concepts Lecture Slides Friday, February 24, Glenn G.

CS 11 Haskell track: lecture 1

11/6/17. Outline. FP Foundations, Scheme. Imperative Languages. Functional Programming. Mathematical Foundations. Mathematical Foundations

Haskell through HUGS THE BASICS

INTRODUCTION TO HASKELL

Functional Programming

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

CMSC 331 Final Exam Section 0201 December 18, 2000

Haskell: From Basic to Advanced. Part 2 Type Classes, Laziness, IO, Modules

CSC 533: Programming Languages. Spring 2015

Haskell Overview II (2A) Young Won Lim 8/23/16

Lambda Calculus see notes on Lambda Calculus

PROGRAMMING IN HASKELL. Chapter 5 - List Comprehensions

Functional Logic Programming Language Curry

Vectors in Scheme. Data Structures in Scheme

Polymorphic lambda calculus Princ. of Progr. Languages (and Extended ) The University of Birmingham. c Uday Reddy

Processadors de Llenguatge II. Functional Paradigm. Pratt A.7 Robert Harper s SML tutorial (Sec II)

Chapter 11 :: Functional Languages

A general introduction to Functional Programming using Haskell

Haskell An Introduction

COMP80 Lambda Calculus Programming Languages Slides Courtesy of Prof. Sam Guyer Tufts University Computer Science History Big ideas Examples:

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))

n n Try tutorial on front page to get started! n spring13/ n Stack Overflow!

Software Paradigms (Lesson 4) Functional Programming Paradigm

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

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

An Explicit-Continuation Metacircular Evaluator

PROGRAMMING IN HASKELL. Chapter 2 - First Steps

Transcription:

Imperative languages Von Neumann model: store with addressable locations machine code: effect achieved by changing contents of store locations instructions executed in sequence, flow of control altered by jumps imperative language: variable corresponds to store location instructions executed in sequence, flow of control altered by conditional and loop statements efficient implementation since close to design of conventional computers 10.1

Functional languages computational model: lambda calculus mathematical functions: domain, range functional languages achieve effect by applying functions functional vs. imperative languages store location assignment statement vs. application of a function side-effects aliasing referential transparency (you can replace function calls with their results without changing the functionality) (allows for memoization: basically caching for the sake of future function calls) 10.2

Program structure what, not how primitive functions mechanism to define new functions from old max a b = if a < b then b else a min a b = if a < b then a else b difference a b c = max a (max b c) min a (min b c) considerations: synchronization simple solutions symbolic manipulation list-processing 10.3

Features of functional languages higher-order functions can accept functions as parameters can return functions as results comp f g = \x -> f (g x) recursion as a basic principle application of rewrite rule: function call replaced by code body run-time overhead garbage collection 10.4

Lists Lisp: atoms, lists, functions atoms symbolic numeric lists each element is an atom or another list (ALPHA BETA GAMMA) (12 14 16) ((A B) (HELLO THERE) 94) 10.5

ALPHA 12 BETA 14 GAMMA nil 16 nil A B nil HELLO 94 nil THERE nil 10.6

Operations on lists list operations head ALPHA is the head of (ALPHA BETA GAMMA) tail (BETA GAMMA) is the tail of (ALPHA BETA GAMMA) cons cons(head(x), tail(x)) = x head(cons(a,x)) = a tail(cons(a,x)) = x recursion ismember x a = if a==[] then False else if x==head a then True else ismember x (tail a) 10.7

Higher order functions function map (f, x) is begin if x = nil then return nil; else return cons(f(head(x)), map (f, tail(x))); endif; end map; function timesten (x) is begin return 10 * x; end timesten; alist (17 24 59) map(timesten, alist) (170 240 590) 10.8

Lisp - functional core function application symbolic expressions (S-expressions) lambda expressions naming new functions: DEF interactive system prefix notation all expressions are parenthesised 10.9

predefined functions CAR, CDR QUOTE short form ATOM NULL MAPCAR special forms NIL, T COND 10.10

(TIMES 10 2) (LAMBDA (X) (TIMES 10 X)) (DEF TIMESTEN (LAMBDA (X) (TIMES 10 X))) (TIMESTEN 2) ((LAMBDA (X) (TIMES 10 X)) 2) (TIMES 10 2) 20 (MAPCAR TIMESTEN (17 24 59)) (DEF ISMEMBER (LAMBDA (X A) (COND ((NULL A) NIL) ((EQ X (CAR A)) T) (T (ISMEMBER X (CDR A))) ) )) 10.11

Lisp - Example Derivative: Given an S-expression such as: (TIMES 4 (POWER X 3)) the aim is to produce the S-expression that represent its derivative with respect to X, that is, in this case, to produce the list: (TIMES 12 (POWER X 2)) (DEF THIRD (LAMBDA (Y) )) (CAR (CDR (CDR Y))) 10.12

(DEF DERIVE (LAMBDA (Y) (COND ((EQ (CAR Y) POWER) (LIST TIMES (THIRD Y) (LIST POWER X (DIFFERENCE (THIRD Y) 1)) )) ((EQ (CAR Y) TIMES) (LIST TIMES (TIMES (CAR (CDR Y)) (THIRD (THIRD Y))) (THIRD (DERIVE (THIRD Y))) )) (T ERROR) ) )) 10.13

Scope rules dynamic scope: identifier bound to most recent definition free variable example: (DEF BASE (LAMBDA (F A) (PLUS (F 10) A) )) (DEF TWODIGIT (LAMBDA (A B) (BASE (LAMBDA (C) (TIMES A C)) B) )) The result of (TWODIGIT 2 3) is not 23. 10.14

Scope rules (TWODIGIT 2 3) (BASE (LAMBDA (C) (TIMES A C)) 3) most recent A = 2 (PLUS ((LAMBDA (C) (TIMES A C)) 10) 3) most recent A = 3 (PLUS ((TIMES 3 10) 3) (PLUS 30 3) 33 This problem would not have arisen if the atom A had not been used in two different contexts; for example if BASE had been defined as: (DEF BASE (LAMBDA (F X) (PLUS (F 10) X) )) Be careful!!! 10.15

Lisp - miscellaneous No language standard Scheme popular dialect meaningful identifiers, reserved words block structure, static scope rules (DEFINE TIMESTEN (LAMBDA (X) (* 10 X))) (DEFINE ISMEMBER (LAMBDA (X A) (COND ((NULL? A) #F) ((EQ? X (CAR A)) #T) (ELSE (ISMEMBER X (CDR A))) ) )) 10.16

Lisp - miscellaneous Imperative features assignment: SET, SETQ (SETQ A B) = (SET A B) (SET A C) PROG, LOOP, GO (DEF SUMLIST (LAMBDA (X) (PROG (TOTAL) (SETQ TOTAL 0) LOOP (COND ((NULL X) (RETURN TOTAL)) (T (SETQ TOTAL (PLUS TOTAL (CAR X))) )) (SETQ X (CDR X)) (GO LOOP) ))) 10.17

FP systems purely functional data values: atoms or sequences each element of a sequence is an atom or a sequence no variables functions can have only one parameter (may be a sequence) application of function G:X functional forms APPLYTOALL composition: o condition: (P -> H;G) construction: [F1, F1,, FN] : X 10.18

FP systems - example DEF ISMEMBER = (NULL SEC -> F; (EQ [HEAD, HEAD SEC] -> T; ISMEMBER [HEAD, TAIL SEC])) ISMEMBER : <3,<7,3,9>> 10.19

NULL SEC : <3,<7,3,9>> NULL : (SEC : <3,<7,3,9>>) NULL : <7,3,9> false EQ [HEAD, HEAD SEC] : <3,<7,3,9>> EQ : ([HEAD, HEAD SEC] : <3,<7,3,9>>) EQ : <HEAD : <3,<7,3,9>>, HEAD SEC : <3,<7,3,9>>> EQ : <3, HEAD : <7,3,9>> EQ : <3, 7> false ISMEMBER [HEAD, TAIL SEC] : <3,<7,3,9>> ISMEMBER : ([HEAD, TAIL SEC] : <3,<7,3,9>>) ISMEMBER : <HEAD : <3,<7,3,9>>, TAIL SEC : <3,<7,3,9>>> ISMEMBER : <3, TAIL : <7,3,9>> ISMEMBER : <3, <3,9>> 10.20

Features of Haskell: static typing type inference higher-order functions polymorphic functions pattern matching list comprehension lazy evaluation Classes (overloading) type operators monads modules Haskell 10.21

Example factorial :: Integer -> Integer factorial 0 = 1 factorial (n+1) = (n+1)*(factorial n) Integer (unbounded) vs. Int (bounded, at least 2 29 2 29-1) factorial 100 933262154439441526816992388562667004907159682643816 214685929638952175999932299156089414639761565182862 536979208272237582511852109168640000000000000000000 00000 :: Integer 10.22

Identity id :: a -> a id x = x Composition Polymorphic functions (.) :: (b -> c) -> (a -> b) -> a -> c f. g = \x -> f (g x) Curry/Uncurry curry :: ((a, b) -> c) -> a -> b -> c curry f x y = f (x,y) uncurry :: (a -> b -> c) -> ((a, b) -> c) uncurry f p = f (fst p) (snd p) 10.23

Lists [a] data type of lists with elements from a. head, tail [], (:) length :: [a] -> Int length [] = 0 length (_:l) = 1 + length l map :: (a -> b) -> [a] -> [b] map f [] = [] map f (x:xs) = f x : map f xs filter :: (a -> Bool) -> [a] -> [a] filter p [] = [] filter p (x:xs) p x = x : filter p xs otherwise = filter p xs 10.24