Interlude: Functional Programming CSE 331 Section 2 James Daly.

Slides:



Advertisements
Similar presentations
1 Scheme and Functional Programming Aaron Bloomfield CS 415 Fall 2005.
Advertisements

Chapter 7 Introduction to Procedures. So far, all programs written in such way that all subtasks are integrated in one single large program. There is.
Tail Recursion. Problems with Recursion Recursion is generally favored over iteration in Scheme and many other languages – It’s elegant, minimal, can.
Transformational Grammars The Chomsky hierarchy of grammars Context-free grammars describe languages that regular grammars can’t Unrestricted Context-sensitive.
1 Programming Languages (CS 550) Lecture Summary Functional Programming and Operational Semantics for Scheme Jeremy R. Johnson.
Functional Programming. Pure Functional Programming Computation is largely performed by applying functions to values. The value of an expression depends.
Mutability SWE 332 Fall 2011 Paul Ammann. SWE 3322 Data Abstraction Operation Categories Creators Create objects of a data abstraction Producers Create.
COP4020 Programming Languages Expression and assignment Prof. Xin Yuan.
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.
String and Lists Dr. Benito Mendoza. 2 Outline What is a string String operations Traversing strings String slices What is a list Traversing a list List.
Tail Recursion. Problems with Recursion Recursion is generally favored over iteration in Scheme and many other languages It’s elegant, minimal, can be.
CS 330 Programming Languages 10 / 14 / 2008 Instructor: Michael Eckmann.
Lecture 27 Exam outline Boxing of primitive types in Java 1.5 Generic types in Java 1.5.
VBA Modules, Functions, Variables, and Constants
CS 330 Programming Languages 10 / 11 / 2007 Instructor: Michael Eckmann.
Common Lisp Derek Debruin Mark Larue Vinh Doan. Problem Domains There was a need in the early 1960s to define a programming language whose computational.
Introducing the Erlang Language Why Erlang?  Exemplifies the “you can have code and concurrency that is free from side effects…there is no other way”
Programming in Scala Chapter 1. Scala: both object-oriented and functional Scala blends –object-oriented and –functional programming in a –statically.
Functional Programming
Type Inference: CIS Seminar, 11/3/2009 Type inference: Inside the Type Checker. A presentation by: Daniel Tuck.
Tail Recursion. Problems with Recursion Recursion is generally favored over iteration in Scheme and many other languages – It’s elegant, minimal, can.
Fortran- Subprograms Chapters 6, 7 in your Fortran book.
Chapter 8 High-Level Programming Languages (modified by Erin Chambers)
Functional Programing Referencing material from Programming Language Pragmatics – Third Edition – by Michael L. Scott Andy Balaam (Youtube.com/user/ajbalaam)
9/18/20151 Programming Languages and Compilers (CS 421) Elsa L Gunter 2112 SC, UIUC Based in part on slides by Mattox.
Lists and More About Strings CS303E: Elements of Computers and Programming.
Haskell Chapter 1, Part I. Highly Recommended  Learn you a Haskell for Great Good. Miran Lipovaca.
Functional Programming Universitatea Politehnica Bucuresti Adina Magda Florea
COP3530 Data Structures600 Stack Stack is one the most useful ADTs. Like list, it is a collection of data items. Supports “LIFO” (Last In First Out) discipline.
Λ => Scheme for Rubyists. Scheme History Authors: Guy Steele and Gerald Sussman Structure and Interpretation of Computer Programs (SICP) by Abelson &
Patterns in OCaml functions. Formal vs. actual parameters Here's a function definition (in C): –int add (int x, int y) { return x + y; } –x and y are.
Chapter 8 High-Level Programming Languages. 8-2 Chapter Goals Describe the translation process and distinguish between assembly, compilation, interpretation,
More about Class 靜宜大學資工系 蔡奇偉副教授 ©2011. 大綱 Instance Class Members Class members can be associated with an instance of the class or with the class as a.
Functional Programming With examples in F#. Pure Functional Programming Functional programming involves evaluating expressions rather than executing commands.
Chapter Fifteen: Functional Programming Languages Lesson 12.
Going Functional Primož Gabrijelčič. Functional programming.
David Evans CS200: Computer Science University of Virginia Computer Science Class 17: Mutation M. C. Escher, Day and Night.
Objects & Dynamic Dispatch CSE 413 Autumn Plan We’ve learned a great deal about functional and object-oriented programming Now,  Look at semantics.
Python Functions.
TIVDM2Functional Programming Language Concepts 1 Concepts from Functional Programming Languages Peter Gorm Larsen.
CS 261 – Data Structures Introduction to C Programming.
David Evans CS200: Computer Science University of Virginia Computer Science Class 16: Mutation M. C. Escher, Day and Night.
Memory Management in Java Computer Science 3 Gerb Objective: Understand references to composite types in Java.
Fall 2008Programming Development Techniques 1 Topic 17 Assignment, Local State, and the Environment Model of Evaluation Section 3.1 & 3.2.
Functional Programming IN NON-FUNCTIONAL LANGUAGES.
Interpreters and Higher-Order Functions CSE 413 Autumn 2008 Credit: CSE341 notes by Dan Grossman.
1 Scheme (Section 11.2) CSCI 431 Programming Languages Fall 2003.
CMSC 330: Organization of Programming Languages Operational Semantics.
Functional Programming. Some Functional Languages Lisp Scheme - a dialect of Lisp Haskell Miranda.
Announcements There is a Quiz today. There were problems with grading assignment 2, but they should be worked out today The web page for correcting the.
CSE 341 Section 10 Subtyping, Review, and The Future.
CSE 425: Functional Programming I Programs as Functions Some programs act like mathematical functions –Associate a set of input values from the function’s.
Quiz 3 Topics Functions – using and writing. Lists: –operators used with lists. –keywords used with lists. –BIF’s used with lists. –list methods. Loops.
CSE-321 Programming Languages Introduction to Functional Programming POSTECH March 8, 2006 박성우.
Winter 2016CISC101 - Prof. McLeod1 CISC101 Reminders Quiz 3 this week – last section on Friday. Assignment 4 is posted. Data mining: –Designing functions.
Introduction to Functional Programming Part 1 – The Basic Concepts Winter Young
Macro Processor Design Options Recursive Macro Expansion General-Purpose Macro Processors Macro Processing within Language Translators.
CS314 – Section 5 Recitation 9
String and Lists Dr. José M. Reyes Álamo.
Functional Programming
Tail Recursion.
Expressions and Assignment
Main Points of Haskell Functional programming Laziness
LL and Recursive-Descent Parsing Hal Perkins Autumn 2011
String and Lists Dr. José M. Reyes Álamo.
LL and Recursive-Descent Parsing Hal Perkins Autumn 2009
LL and Recursive-Descent Parsing Hal Perkins Winter 2008
Defining Macros in Scheme
Presentation transcript:

Interlude: Functional Programming CSE 331 Section 2 James Daly

Reminder Homework 2 due Leave on table before you leave Project 2 is out Covers tree sets Due next Friday at midnight Exam 1 will be Thursday, February 26 Review day the preceding Tuesday. 1 sheet of notes

Interlude Covering something completely different today Discussing differences between languages Focus on a very different way of programming Most examples will be in Scala More available at

Greenspun’s Rule “Any sufficiently complicated C or Fortran program contains an ad hoc, informally specified, bug-ridden, slow implementation of half of Common Lisp.” Phillip Greenspun

Morris’s Corollary “… including Common Lisp.” Robert Morris

Java vs C++ Garbage collection No explicit delete calls Memory cleaned up automatically when object no longer used Not cleaned up immediately Better throughput but worse latency Always has smart-pointers (references) Can’t stack-allocate an object Always passes references by value

Java vs C++ Java C++

Programming Paradigms C/C++ and many other languages use an imperative paradigm Focus on how to do something Change of state Procedural programming is a common sub-type Basis is function calls or sub-routines Frequently combined with object-oriented programming

Programming Paradigms In functional programming, the focus is on what rather than how Based on mathematical expressions Avoids changing state / mutable data Output depends only on inputs Basis for Lisp, Scheme, OCaml, and Haskell Source: rosettacode.org/Factorial

Scala Object-functional Compiles to Java bytecode

Functional Ideas First-class Functions / Higher-order Functions Functions can be passes as arguments to other functions Purity Function calls don’t change state Immutable Data Data structures cannot be modified Reference transparency Function results depend only on its arguments

Immutable Data Data elements cannot be modified Return a modified copy instead Con – may have to make extra copies May have to make “defensive” copies anyway Pro – may be able to share some sub-data Very good for multi-threading Don’t need to worry whether your copy is up to date Nobody can change the data on you

Immutable Example

Sharing Data Bulk of the list can be shared

Purity Functions could do things other than return values Change program state (environment) Called side effects Pure functions have no side-effects

Reference Transparency Calling a function with the same arguments yields the same results So Func(x) = Func(y) if x = y Not dependent on the environment Makes it easier to reason about Purity and Immutability ensure the environment doesn’t change

Higher Order Functions Functions can be passed as arguments to other functions Often takes the form of a lambda expression Allows us to make small changes to an algorithm without having to rewrite the whole thing

Higher Order Function Lambda

Higher Order Functions Generic Stack Function takes a T and returns an S

Sorting Source: Scala by Example, by Martin Odersky

Palindromes The empty string is a palindrome A single character is a palindrome awa is a palindrome if w is a palindrome aw and wa are both palindromes if w is a palindrome and a is a special symbol Nothing else is a palindrome

Pattern Matching Empty string Single letter List starting with a List ending with a Anything else

More Pattern Matching Parse tree!

Option Return type for when calculations can fail Two sub-classes Some None Replaces null Don’t need to worry about null in other cases

Cish way

Option Is a collection May only have one item

Functional Way

A lot of them

C++ Version