UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering Introduction to Functional Programming Notes for CSCE 190 Based on Sebesta,

Slides:



Advertisements
Similar presentations
0 LECTURE 5 LIST COMPREHENSIONS Graham Hutton University of Nottingham.
Advertisements

Functional Programming Languages
0 PROGRAMMING IN HASKELL Chapter 1 - Introduction.
UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering CSCE 330 Programming Language Structures Chapter 1: Introduction Fall 2006.
Chapter 15 Other Functional Languages. Copyright © 2007 Addison-Wesley. All rights reserved. Functional Languages Scheme and LISP have a simple syntax.
0 PROGRAMMING IN HASKELL Chapter 6 - Recursive Functions Most of this should be review for you.
UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering CSCE 330 Programming Language Structures Haskell Fall 2005 Marco Valtorta
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.
0 PROGRAMMING IN HASKELL Chapter 6 - Recursive Functions.
ISBN Chapter 15 Functional Programming Languages.
UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering CSCE 330 Programming Language Structures Literate Programming in Haskell Fall.
Comp 205: Comparative Programming Languages Imperative Programming Languages Functional Programming Languages Semantics Other Paradigms Lecture notes,
ISBN Chapter 15 Functional Programming Languages.
Gary MarsdenSlide 1University of Cape Town Functional programming in Clean Gary Marsden Semester 2 – 2000.
0 PROGRAMMING IN HASKELL An Introduction Based on lecture notes by Graham Hutton The book “Learn You a Haskell for Great Good” (and a few other sources)
ISBN Chapter 15 Functional Programming Languages.
0 REVIEW OF HASKELL A lightening tour in 45 minutes.
ISBN Chapter 15 Functional Programming Languages.
1 COMP313A Functional Programming (1). 2 Main Differences with Imperative Languages Say more about what is computed as opposed to how Pure functional.
0 PROGRAMMING IN HASKELL Chapter 7 - Defining Functions, List Comprehensions.
1 Programming Languages and Paradigms Functional Programming.
Project 1 Due Date: September 25 th Quiz 4 is due September 28 th Quiz 5 is due October2th 1.
Computer Science Department Data Structure & Algorithms Lecture 8 Recursion.
Chapter Fifteen: Functional Programming Languages Lesson 12.
ISBN Chapter 15 Functional Programming Languages.
CS 363 Comparative Programming Languages Functional Languages: Scheme.
Comparative Programming Languages Language Comparison: Scheme, Smalltalk, Python, Ruby, Perl, Prolog, ML, C++/STL, Java, Haskell.
ISBN Chapter 15 Functional Programming Languages.
ISBN Chapter 15 Functional 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.
COMP313A Functional Programming (1)
Cs1321 December 6, 2001 Review. What is computer science? What's an algorithm? Processes and programs Overview of some programming language concepts Functional.
Lee CSCE 314 TAMU 1 CSCE 314 Programming Languages Haskell: More on Functions and List Comprehensions Dr. Hyunyoung Lee.
UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering CSCE 190 Programming Language Paradigms Fall 2014
UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering CSCE 330 Programming Language Structures Operational Semantics (Slides mainly.
Erik Meijer FP101x - Functional Programming Programming in Haskell - Introduction.
Lecture 16: Advanced Topic: Functional Programming CS5363 Compiler and Programming Languages.
ISBN Chapter 15 Functional Programming Languages.
1 PROGRAMMING IN HASKELL An Introduction Based on lecture notes by Graham Hutton The book “Learn You a Haskell for Great Good” (and a few other sources)
Functional Programming
CS314 – Section 5 Recitation 9
Functional Programming
CSCE 190 Programming Language Paradigms
Functional Programming Languages
Chapter 15 :Functional Programming Languages
Functional Programming Languages
What is a Functional Language?
History of Computing – Lisp
Midterm recap Total was 80 points Distribution range
A lightening tour in 45 minutes
PROGRAMMING IN HASKELL
PROGRAMMING IN HASKELL
Functional Programming Languages
Fundamentals of Functional Programming Languages
Computer Science 312 Haskell Lists 1.
FP Foundations, Scheme In Text: Chapter 14.
Functional Programming Languages
Procedural vs Functional Style
Functional Programming Languages
PROGRAMMING IN HASKELL
PROGRAMMING IN HASKELL
CSE 3302 Programming Languages
CSCE 314: Programming Languages Dr. Dylan Shell
15.2 Mathematical Functions
Chapter 15 Functional Programming 6/1/2019.
Functional Programming Languages
Presentation transcript:

UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering Introduction to Functional Programming Notes for CSCE 190 Based on Sebesta, Hutton, Ullman Marco Valtorta

UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering Language Families Imperative (or Procedural, or Assignment-Based) Functional (or Applicative) Logic (or Declarative)

UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering Imperative Languages Mostly influenced by the von Neumann computer architecture Variables model memory cells, can be assigned to, and act differently from mathematical variables Destructive assignment, which mimics the movement of data from memory to CPU and back Iteration as a means of repetition is faster than the more natural recursion, because instructions to be repeated are stored in adjacent memory cells

UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering Functional Languages Model of computation is the lambda calculus (of function application) No variables or write-once variables No destructive assignment Program computes by applying a functional form to an argument Program are built by composing simple functions into progressively more complicated ones Recursion is the preferred means of repetition

UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering Logic Languages Model of computation is the Post production system Write-once variables Rule-based programming Related to Horn logic, a subset of first-order logic AND and OR non-determinism can be exploited in parallel execution Almost unbelievably simple semantics Prolog is a compromise language: not a pure logic language

UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering What is a Functional Language? Functional programming is style of programming in which the basic method of computation is the application of functions to arguments; A functional language is one that supports and encourages the functional style. Opinions differ, and it is difficult to give a precise definition, but generally speaking:

UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering Example Summing the integers 1 to 10 in Java: total = 0; for (i = 1; i  10; ++i) total = total+i; The computation method is variable assignment. 7

UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering Example Summing the integers 1 to 10 in Haskell: sum [1..10] The computation method is function application. 8

UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering Simple Function Definition Examples from Sebesta fact 0 = 1 fact n = n * fact (n -1) fact1 = prod [1..n] fib 0 = 1 fib 1 = 1 fib (n + 2) = fib (n + 1) + fib n

UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering Guards and otherwise fact n | n == 0 = 1 | n > 0 = n * fact (n – 1) fact will not loop forever for a negative argument! sub n | n < 10 = 0 | n > 100 = 2 | otherwise = 1

UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering Local scope: where quadratic_root a b c = [minus_b_over_2a – root_part_over_2a, minus_b_over_2a + root_part_over_2a] where minus_b_over_2a = -b / (2.0 * a) root_part_over_2a = sqrt(b ^ 2 – 4.0 * a * c) / (2.0 * a)

UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering List comprehensions, generators, and tests List comprehensions are used to define lists that represent sets, using a notation similar to traditional set notation: [body | qualifiers], e.g., [n * n * n | n <- [1..50]] defines a list of cubes of the numbers from 1 to 50 Qualifiers can be generators (as above) or tests The following function returns the list of factors of n factors n = [i | i <- [1.. n div 2], n mod i == 0]

UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering 13 A Taste of Haskell f [] = [] f (x:xs) = f ys ++ [x] ++ f zs where ys = [a | a  xs, a  x] zs = [b | b  xs, b > x] ? 13

UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering Quicksort The quicksort algorithm for sorting a list of integers can be specified by the following two rules: The empty list is already sorted; Non-empty lists can be sorted by sorting the tail values  the head, sorting the tail values  the head, and then appending the resulting lists on either side of the head value.

UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering Using recursion, this specification can be translated directly into an implementation: qsort :: [Int]  [Int] qsort [] = [] qsort (x:xs) = qsort smaller ++ [x] ++ qsort larger where smaller = [a | a  xs, a  x] larger = [b | b  xs, b  x] This is probably the simplest implementation of quicksort in any programming language! Note:

UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering For example (abbreviating qsort as q): q [3,2,4,1,5] q [2,1]++ [3] ++q [4,5] q [1]q []++ [2] ++q []q [5]++ [4] ++ [1] [] [5]

UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering Lazy evaluation Lazy evaluation allows infinite data structures, e.g.: positives = [0..] squares = [n * n | n <- [0..]] member squares 16 member [] b = False member (a:x) b = (a == b) || member x b member2 (m:x) n | m < n = member2 x n | m == n true otherwise = False