CS 36 – Chapter 11 Functional programming Features Practice

Slides:



Advertisements
Similar presentations
C-LISP. LISP 2 Lisp was invented by John McCarthy in 1958 while he was at the Massachusetts Institute of Technology (MIT).John McCarthyMassachusetts Institute.
Advertisements

1 Copyright © 1998 by Addison Wesley Longman, Inc. Chapter 14 Functional Programming Languages - The design of the imperative languages is based directly.
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.
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-1 An Introduction to Scheme March Introduction A mid-1970s dialect of LISP, designed to be a cleaner, more modern, and simpler version than.
1 Functional programming Languages And a brief introduction to Lisp and Scheme.
Functional Programming Languages
Copyright © 2006 The McGraw-Hill Companies, Inc. Programming Languages 2nd edition Tucker and Noonan Chapter 14 Functional Programming It is better to.
Chapter 15 Functional Programming Languages. Copyright © 2007 Addison-Wesley. All rights reserved. 1–2 Introduction Design of imperative languages is.
ISBN Chapter 15 Functional Programming Languages Mathematical Functions Fundamentals of Functional Programming Languages Introduction to.
Symbolic Expressions (S Expressions) Syntax: Opening and Closing parenthesis having elements in between. List represented in LISP: (A B2 C3 Shahid) (A.
Functional programming: LISP Originally developed for symbolic computing First interactive, interpreted language Dynamic typing: values have types, variables.
ISBN Chapter 15 Functional Programming Languages.
ISBN Chapter 15 Functional Programming Languages.
LISP 1.5 and beyond A very quick tour. Data Atoms (symbols) including numbers – All types of numbers including Roman! (well, in the early days) – Syntactically.
1 (Functional (Programming (in (Scheme)))) Jianguo Lu.
© Kenneth C. Louden, Chapter 11 - Functional Programming, Part I: Concepts and Scheme Programming Languages: Principles and Practice, 2nd Ed. Kenneth.
Functional Programing Referencing material from Programming Language Pragmatics – Third Edition – by Michael L. Scott Andy Balaam (Youtube.com/user/ajbalaam)
Functional Programming in Scheme
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.
CS 330 Programming Languages 11 / 21 / 2006 Instructor: Michael Eckmann.
CS 152: Programming Language Paradigms February 17 Class Meeting Department of Computer Science San Jose State University Spring 2014 Instructor: Ron Mak.
Introduction to Scheme Lectures on The Scheme Programming Language, 2 nd Ed. R. Kent Dybvig.
CS 330 Programming Languages 11 / 13 / 2008 Instructor: Michael Eckmann.
CS 363 Comparative Programming Languages Functional Languages: Scheme.
Introduction to Scheme. Lisp and Scheme Lisp: List processor language –Full recursion –Conditional expression –Extensibility –Interactive Scheme: one.
Lisp Functional Language or Applicative Language –Achieves its effect by applying functions, either recursively or through composition Powerful, expressive,
18-October-2002cse Symbols © 2002 University of Washington1 Symbols CSE 413, Autumn 2002 Programming Languages
ISBN Chapter 15 Functional Programming Languages.
1 Chapter 15 © 2002 by Addison Wesley Longman, Inc Introduction - The design of the imperative languages is based directly on the von Neumann architecture.
ISBN Chapter 15 Functional Programming Languages.
CS535 Programming Languages Chapter - 10 Functional Programming With Lists.
Scheme Profs Tim Sheard and Andrew Black CS 311 Computational Structures.
Milos Hauskrecht (PDF) Hieu D. Vu (PPT) LISP PROGARMMING LANGUAGE.
Introduction to LISP Atoms, Lists Math. LISP n LISt Processing n Function model –Program = function definition –Give arguments –Returns values n Mathematical.
1 FP Foundations, Scheme In Text: Chapter Chapter 14: FP Foundations, Scheme Mathematical Functions Def: A mathematical function is a mapping of.
ISBN Chapter 15 Functional Programming Languages.
CSE 425: Functional Programming I Programs as Functions Some programs act like mathematical functions –Associate a set of input values from the function’s.
Comparative Programming Languages Functional programming with Lisp/Scheme.
ISBN Chapter 15 Functional Programming Languages.
1 Outline Review Introduction to LISP Symbols and Numbers Lists Writing LISP Functions LISPWorks.
Functional Programming
CS314 – Section 5 Recitation 9
Functional Programming
CS314 – Section 5 Recitation 10
Functional Programming Languages
Functional Programming Languages
Chapter 11 - Functional Programming, Part I: Concepts and Scheme
Functional Programming
History of Computing – Lisp
CS 326 Programming Languages, Concepts and Implementation
CS 326 Programming Languages, Concepts and Implementation
CS 326 Programming Languages, Concepts and Implementation
Scheme : variant of LISP
Chapter 15 – Functional Programming Languages
Lists in Lisp and Scheme
Functional Programming Languages
FP Foundations, Scheme In Text: Chapter 14.
Functional Programming Languages
Announcements Quiz 5 HW6 due October 23
COP4020 Programming Languages
Modern Programming Languages Lecture 18 Fakhar Lodhi
15.2 Mathematical Functions
Presentation transcript:

CS 36 – Chapter 11 Functional programming Features Practice Here’s a Scheme interpreter I found… http://sourceforge.net/projects/sisc Chapter 11? We’re in bankruptcy!

Functional programming Function calls dominate, not merely present Based on mathematical syntax, not efficient code Usually interpreted, not compiled Usually typeless… we manipulate numbers and string lists: “symbolic computation” Languages Lisp, Scheme, ML, Haskell

Comparison Imperative Functional Assignment statements Memory locations State of a machine Functional Expression evaluation Function calls Less attention to rep’n, implementation

The culture Working with functional language: interpreter. Loves to evaluate things interactive, immediate results like using adding machine, cash register, calculator Example… If you say “8 – 3”, you get answer. What would Java compiler tell you? But, what was so good about imperative languages: compiling (translating), wait & enter 2nd command to run?

Source code A functional “program” is a sequence of expressions constant variable (to be evaluated immediately) list (just to display like a constant) function call Use the same syntax for lists and function calls (turkey ham tuna) (- 8 3) Recursion instead of loops 

Samples Prefix expressions (+ (* 3 4) 5 1) returns 18 define: variables for later use (define a 12) (define b 5) (+ a b) returns 17 lambda: Creating a function (lambda (x) (* 2 x)) Use both “define” and “lambda” to define function with name: (define double ) (double 55) returns 110

Functions Many functions in Scheme/Lisp need to be recursive. Factorial? (define fact (lambda (x) (* x (fact (- x 1)))) )

Conditionals Use cond to implement if-else (cond (expr value) ... (#t value)) Return value corresponding to first true expression we encounter. (cond ((< 5 0) -1) (= 5 0) 0) (#t 1))

Using cond (define sign (lambda (x) (cond ((< x 0) -1) (= x 0) 0) (#t 1) ) (sign 400) returns 1 (sign -333) returns -1 (sign 0) returns 0 Now we should be able to write a factorial function 

Function on list Recall the built-in functions car and cdr. “caddr” is abbreviation of (car (cdr (cdr x))) (define third (lambda (x) (caddr x)) ) (third '(sugar cereal pasta asparagus treacle)) returns pasta Note that we need a quote for a list-constant or an atom from a list. x means evaluate the variable x. 'x means don’t evaluate x.

Defining a list (define list1 '(1 2 3)) (define list2 '((a b) (e f) (x y))) list1 returns (1 2 3) list2 returns ((a b) (e f) (x y)) Use cons to add one element to a list, or append for combining two lists. (cons 8 list1) does the same as (append ‘(8) list1) For review, what are: (car list2) (caar list2) (cadar list2) Appending to list2: (append ‘((g h)) list2)

Multiple variables Add 2 numbers Add a list of numbers (define sum (lambda (x y) (+ x y)) ) (sum 8 4) returns 12 Add a list of numbers (define sumlist (lambda (x) (+ (car x) (sumlist (cdr x)))) Why is this wrong?

Better sumlist When working with lists, it’s often helpful to use comparisons such as: (null? list) to see if a list is empty (eq? x y) to see if two atoms are equal (define sumlist (lambda (x) (cond ((null? x) 0) (#t (+ (car x) (sumlist (cdr x)))) ) (sumlist '(5 4 7 3 2 6 1 8 10 9)) returns 55

Creating functions in Scheme/Lisp List manipulation Numerical examples Database application Property list Association list

Review Built-in functions Cambridge Polish notation: (f x), not f(x) car, cdr, cons cond define, lambda null?, eq? (not to confuse with =) display Cambridge Polish notation: (f x), not f(x) Recursion replaces iteration Elegant, compact, focus only on evaluation

Examples Let’s begin with finding the length of a list. (define length (lambda (L) (cond ((null? L) 0) (#t (+ 1 (length (cdr L)))) ) (length '(Klaatu barada nikto))  returns 3 Next: we can write a similar function to count how many times a certain word appears in a list.

Further examples Basic list manipulation (number_matches word L) (get_word L n) (get_last_word L) (replace_first L word) (replace_cp) (replace_word) (reverse L) Number lists (abs L) ; more in lab  “Database” functions (get_age R) (set_age R new_age) (get_salary R) (set_salary R new_salary)