Computer Eng. Software Lab II 242-203, Semester 2, 2015-2016 Who I am: Andrew Davison CoE, WiG Lab Office Functional Programming.

Slides:



Advertisements
Similar presentations
Programming with App Inventor Computing Institute for K-12 Teachers Summer 2012 Workshop.
Advertisements

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.
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.
Lisp II. How EQUAL could be defined (defun equal (x y) ; this is how equal could be defined (cond ((numberp x) (= x y)) ((atom x) (eq x y)) ((atom y)
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.
Lisp II. How EQUAL could be defined (defun equal (x y) ; this is how equal could be defined (cond ((numberp x) (= x y)) ((atom x) (eq x y)) ((atom y)
Chapter 3 Functional Programming. Outline Introduction to functional programming Scheme: an untyped functional programming language.
Recursion (define tell-story (lambda () (print ‘’Once upon a time there was a mountain. ‘’) (print ‘’On the mountain, there was a temple. ‘’) (print ‘’In.
1 Functional programming Languages And a brief introduction to Lisp and Scheme.
Lisp. Versions of LISP Lisp is an old language with many variants –LISP is an acronym for List Processing language Lisp is alive and well today Most modern.
Functional programming: LISP Originally developed for symbolic computing Main motivation: include recursion (see McCarthy biographical excerpt on web site).
CS 330 Programming Languages 11 / 20 / 2007 Instructor: Michael Eckmann.
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.
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.
1 (Functional (Programming (in (Scheme)))) Jianguo Lu.
Comp. Eng. SW Lab II: FP with Scheme 1 Computer Eng. Software Lab II , Semester 2, Who I am: Andrew Davison CoE, WiG Lab Office.
Introduction to Python Basics of the Language. Install Python Find the most recent distribution for your computer at:
1 Lisp Functions –Built-in functions –Defining functions –Function Evaluation and Special Forms defun, if Control statements –Conditional if, cond –Repetition.
Functional Programming in Scheme
CSC3315 (Spring 2009)1 CSC 3315 Programming Paradigms Scheme Language Hamid Harroud School of Science and Engineering, Akhawayn University
Nonvisual Arrays and Recursion by Chris Brown under Prof. Susan Rodger Duke University June 2012.
Functional Programming in Scheme and Lisp. Overview In a functional programming language, functions are first class objects. You can create them, put.
CS 330 Programming Languages 11 / 21 / 2006 Instructor: Michael Eckmann.
CS 326 Programming Languages, Concepts and Implementation Instructor: Mircea Nicolescu Lecture 7.
Functional Programming and Lisp. Overview In a functional programming language, functions are first class objects. In a functional programming language,
CS 330 Programming Languages 11 / 13 / 2008 Instructor: Michael Eckmann.
Functional Programming in Scheme and Lisp.
COP4020 Programming Languages Functional Programming Prof. Xin Yuan.
Programming Fundamentals 2: Simple/ F II Objectives – –give some simple examples of Java applications and one applet 2. Simple Java.
I Power Higher Computing Software Development Development Languages and Environments.
Milos Hauskrecht (PDF) Hieu D. Vu (PPT) LISP PROGARMMING LANGUAGE.
Introduction to Objective Caml. General comments ML is a purely functional language--there are (almost) no side effects There are two basic dialects of.
1 FP Foundations, Scheme In Text: Chapter Chapter 14: FP Foundations, Scheme Mathematical Functions Def: A mathematical function is a mapping of.
1. Starting 1 Let’s Learn Saenthong School, January – February 2016 Teacher: Aj. Andrew Davison, CoE, PSU Hat Yai Campus
1 Introduction to Functional Programming in Racket CS 270 Math Foundations of CS Jeremy Johnson.
1 Outline Review Introduction to LISP Symbols and Numbers Lists Writing LISP Functions LISPWorks.
CS314 – Section 5 Recitation 9
Functional Programming
CS314 – Section 5 Recitation 10
Additional Scheme examples
Functional Programming Languages
Conditional Expressions
CS 550 Programming Languages Jeremy Johnson
CS 326 Programming Languages, Concepts and Implementation
Lists in Lisp and Scheme
PROGRAMMING IN HASKELL
PROGRAMMING IN HASKELL
CS 270 Math Foundations of CS Jeremy Johnson
Env. Model Implementation
Microsoft Access Illustrated
Introduction to Functional Programming in Racket
PROGRAMMING IN HASKELL
The Metacircular Evaluator
FP Foundations, Scheme In Text: Chapter 14.
CS 36 – Chapter 11 Functional programming Features Practice
The Metacircular Evaluator (Continued)
PROGRAMMING IN HASKELL
Higher Order Functions
6.001 SICP Variations on a Scheme
Introduction to Functional Programming in Racket
Announcements Quiz 5 HW6 due October 23
List and list operations (continue).
6.001 SICP Interpretation Parts of an interpreter
PROGRAMMING IN HASKELL
topics interpreters meta-linguistic abstraction eval and apply
Mu Editor – New User Cheat Sheet – CircuitPython Mode
Mu Editor – New User Cheat Sheet – CircuitPython Mode
Presentation transcript:

Computer Eng. Software Lab II , Semester 2, Who I am: Andrew Davison CoE, WiG Lab Office Functional Programming with Racket Please ask questions 1FP with Racket

Overview 1.Why Racket? 2.Constants (names) 2.Functions 3.Lists 4.cond 5.Recursion 6.No-name Functions: lambda 8. Higher Order Functions 9. Using DrRacket 10. More Information FP with Racket2 continued

1. Why Racket? Racket is one of the simpler functional programming languages – a program is a collection of (math) functions – closeness to maths makes programming easier Racket has plenty of advanced features – e.g. first-class functions function body code can be manipulated as data data can be used as function body code Used in Artficial Intelligence (AI) 3FP with Racket

2. Constants (names) myPi is evaluated then printed 'myPi is not evaluated More details on how to use DrRacket are given in Section 9. myPi is defined here 'pi is pre-defined 4FP with Racket

First Error FP with Racket5

3. Functions A function call is written as: (operator arg 1 arg 2 … arg n ) For example: (+ 2 3)means FP with Racket

Function Call Examples The + operator (and other arithmetic) operators can take any number of arguments. The same as: (2 + 3) * (1 + 2) 7FP with Racket

Test Functions (Predicates) Test functions return boolean values true or false. Their names end with a '?' – e.g. number? char? 8FP with Racket

Defining Functions Similar C code is: int sq(int x) { return x*x; } 9FP with Racket

Similar C code is: int myMax(int x,int y) { if (x > y) return x; else return y; } 10FP with Racket

Combining Functions Racket functions can be easily combined – make a function call the argument of another function function call arguments 11FP with Racket

4. Lists List values are placed inside (list …): – (list )// an empty list – (list 1 2 3)// a list of numbers – (list 'a 'b 'c)// a list of names – (list (list 1 2) (list 2 3 4)) // a list of two lists If the language includes list abbreviations, then (list...) can be written as '(...) – '(1 2 3) is the same as (list 1 2 3) 12FP with Racket

Basic List Operations (null? x) – returns true is x is an empty list; otherwise returns false (car x) – returns the first element (head) of a non- empty list x (cdr x) – returns the tail of a non-empty list x everything except the first element continued 13FP with Racket

(cadr x) – returns the head element of the tail of x (cons h l) – makes a new list where the head is h, the tail is l 14FP with Racket

List Operation Examples 15FP with Racket

plusList plusList pulls out the first two arguments of a list and adds them 16FP with Racket

Another Error FP with Racket17

Other Built-in List Functions (length x) – returns the length of a list x (append x y) – makes a new list by appending list x onto the front of list y (reverse x) – makes a list by reversing the list x and many, many more... continued 18FP with Racket

combining reverse and append 19FP with Racket

5. cond: the multi-branch if Similar to the C code: int sizer(int x) { if (x == 0) return 0; else if (x > 0) return 1; else return -1; } 20FP with Racket

6. Recursion Base case: – the function returns an answer and stops Recursion step: – the body of the function calls itself (with smaller arguments) 21FP with Racket

length Defined recursive step base case 22FP with Racket

33 append Defined 23FP with Racket

member Defined mem? is a predicate (a test function) mem is the wrong name 24FP with Racket

7. No-name Functions: lambda Similar to the C-like code: int ??(int x) { return x *x; } Similar to a C-like function call: ??(3) The language 'level' must be increased for lambda to be supported. 25FP with Racket

Defining Functions (v.2) Similar to the C-like code: sq2 = int ??(int x) { return x * x; } 26FP with Racket

Why have lambda? For simple examples (like ours), there is no need for lambda. lambda becomes very useful in higher order programming, where data and functions need to be translated into each other – e.g. parsing, AI 27FP with Racket

8. Higher Order Functions A higher order function has 1 or more arguments which are function names – higher order functions are also called functionals Higher order functions are very important to advanced functional programming and AI. An ‘Intermediate Student’ feature 28FP with Racket

apply (apply f x) – same as executing (f x) – its first argument, f, is a function name – its second argument, x, is the input for f 29FP with Racket

30FP with Racket

map (map f x) – returns a list by applying the function f to each element of the list x 31FP with Racket

map and plusList plusList is applied to the 4 lists in the input list. 32FP with Racket

9. Using DrRacket Download the installation program for DrRacket from: Software.coe/LAB/FuncProg/ The filename: racket-6.3-i386-win32.exe Installs on Windows 33FP with Racket

Start DrRacket from the Windows Start menu item Racket: definitions go here execute functions here 34FP with Racket

Some Notes Set the language mode to “Beginning Student with List Abbreviations” – under the Language > Choose Language menu item continued 35FP with Racket

Press the "Run" button after adding a new function to the definitions window – this makes the new function visible down in the execution window 36FP with Racket

Create a Racket Program using any text editor 37FP with Racket Don't forget this line.

Load myMax.scm use the File/Open menu item; or double click on the file 38FP with Racket Press the "Run" button for the execution window to be updated at the bottom. Press the "Run" button for the execution window to be updated at the bottom.

Use the myMax Function 39FP with Racket

Alternatively, you can type the function into DrRacket's definition window, and save it from there. – you get colour-coded syntax, indenting (tabbing), and error checking as you type 40FP with Racket

10. More Information 41FP with Racket The "Help/ Racket Documentation" menu item: Very nice

PLT Scheme  Racket PLT Scheme v.5 was renamed to Racket – two websites: in 2010) At the beginner's level (i.e. for us), there's no difference between the two languages. 42FP with Racket