More Functional Programming

Slides:



Advertisements
Similar presentations
Higher-Order Functions and Loops c. Kathi Fisler,
Advertisements

Introduction to Compilation of Functional Languages Wanhe Zhang Computing and Software Department McMaster University 16 th, March, 2004.
CSE341: Programming Languages Lecture 2 Functions, Pairs, Lists Dan Grossman Winter 2013.
Getting started with ML ML is a functional programming language. ML is statically typed: The types of literals, values, expressions and functions in a.
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.
Programming Language Theory Leif Grönqvist The national Graduate School of Language Technology (GSLT) MSI.
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”
Type Inference: CIS Seminar, 11/3/2009 Type inference: Inside the Type Checker. A presentation by: Daniel Tuck.
Haskell Chapter 1, Part I. Highly Recommended  Learn you a Haskell for Great Good. Miran Lipovaca.
Functional Languages. Why? Referential Transparency Functions as first class objects Higher level of abstraction Potential for parallel execution.
Dan Johnson.  Functional language that started development in  Committee designed language  Pure and Lazy  Compiled or Interpreted  Named after.
Chapter Fifteen: Functional Programming Languages Lesson 12.
A Second Look At ML 1. Outline Patterns Local variable definitions A sorting example 2.
Python uses boolean variables to evaluate conditions. The boolean values True and False are returned when an expression is compared or evaluated.
TIVDM2Functional Programming Language Concepts 1 Concepts from Functional Programming Languages Peter Gorm Larsen.
COMP313A Functional Programming (1)
Inside LINQ to Objects How LINQ to Objects work Inside LINQ1.
Error Example - 65/4; ! Toplevel input: ! 65/4; ! ^^ ! Type clash: expression of type ! int ! cannot have type ! real.
Chapter SevenModern Programming Languages1 A Second Look At ML.
Haskell Introduction CSCE 314 Spring CSCE 314 – Programming Studio Historical Background 1930s: Alonzo Church develops the lambda calculus, a simple.
C H A P T E R E I G H T Functional Programming Programming Languages – Principles and Paradigms by Allen Tucker, Robert Noonan.
Miranda Programming Language By Bindu H. Vinay. History of Miranda  Miranda was developed in by David Turner  It is currently being marketed.
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
Lecture 14: Advanced Topic: Functional Programming
Programming Languages 2nd edition Tucker and Noonan
Polymorphic Functions
Functional Programming
Functional Programming
Conditional Expressions
What is a Functional Language?
Midterm recap Total was 80 points Distribution range
CS 3304 Comparative Languages
Principles of programming languages 12: Functional programming
Haskell Chapter 1, Part I.
A lightening tour in 45 minutes
Haskell Chapter 4.
PROGRAMMING IN HASKELL
PROGRAMMING IN HASKELL
PROGRAMMING IN HASKELL
PROGRAMMING IN HASKELL
Stack Data Structure, Reverse Polish Notation, Homework 7
Conditions and Ifs BIS1523 – Lecture 8.
Computer Science 312 Haskell Lists 1.
PROGRAMMING IN HASKELL
Fundamentals of Programming
An aggregation mechanism
FP Foundations, Scheme In Text: Chapter 14.
Passing Parameters by value
Functions Chapter 9 Copyright © 2008 W. W. Norton & Company.
String and Lists Dr. José M. Reyes Álamo.
Winter 2018 With thanks to: Dan Grossman / Eric Mullen
PROGRAMMING IN HASKELL
CSCE 314: Programming Languages Dr. Dylan Shell
Lisp, Then and Now.
PROGRAMMING IN HASKELL
Fundamentals of Functional Programming
RECURSION Haskell.
PROGRAMMING IN HASKELL
HIGHER ORDER FUNCTIONS
CSE 3302 Programming Languages
CSCE 314: Programming Languages Dr. Dylan Shell
PROGRAMMING IN HASKELL
Functional Programming and Haskell
PROGRAMMING IN HASKELL
PROGRAMMING IN HASKELL
CSE341: Programming Languages Lecture 2 Functions, Pairs, Lists
Introducing Modularity
Functional Programming and Haskell
Presentation transcript:

More Functional Programming C H A P T E R E I G H T More Functional Programming Programming Languages – Principles and Paradigms by Allen Tucker, Robert Noonan

Haskell History: In the late 70's, John Backus put forward the idea of a "pure", higher order programming language with syntax similar to combinatory logic. He defined the language FP, which has been very influential for the further development of functional languages. An important idea in FP is that of higher order functions, functions that take functions as arguments or return them as results. Researchers at the University of Edinburgh defined the language ML ("Meta-Language") for this purpose. ML features type inference, the ability to infer types for all parts of the program without there being any explicit type declarations.

Miranda is an ML-like language developed in 1985-1986. Haskell History: Miranda is an ML-like language developed in 1985-1986. It is one of the first lazy, or non-strict functional languages. Such languages try to defer evaluation of expressions until the result is needed. This gives a different semantic to the language than if "eager" evaluation is used, and makes it possible to use features such as potentially infinite data structures. Another functional language is Erlang, a functional language with concurrent processes. Erlang was developed at Ericsson primarily for telecom applications, but it is a general programming language.

Haskell History: This variety of functional programming languages led to some confusion and although each one has its specific features and advantages the need for a standard emerged. Haskell, first introduced in 1987, was an attempt to create a standard, non-strict, higher order functional language to achieve a consistent base for research The current version is Haskell 98… "Haskell has indeed evolved continuously since its original publication. By the middle of 1997, there had been four iterations of the language design (the latest at that point being Haskell 1.4). At the 1997 Haskell Workshop in Amsterdam, it was decided that a stable variant of Haskell was needed; this stable language is the subject of this Report, and is called Haskell 98."

Lots of information is available from http://www.haskell.org. Haskell Features: Many features of Haskell originate in ML, and the languages have quite similar "look-and-feel". A novel invention in Haskell is type classes, a structured form of overloading that is reminiscent of how method names are overloaded in object-oriented languages. The very rich typing system is an essential feature leading to programming in Haskell being called “typeful programming”. Another essential feature is lazy evaluation, which has some interesting implications. Lots of information is available from http://www.haskell.org.

Haskell Example: We will now take a look at a Haskell version of QuickSort, which might look a something like this. qsort [] = [] qsort (x:xs) = qsort less ++ [x] ++ qsort more where less = filter (<x) xs more = filter (>=x) xs  The function (<x) should be read out "the function 'less than x'" and will return True if an element passed to it is less than x (notice how we put the expression "<x” in parenthesis and sent it off to the function). All elements that pass the test are output from the filter function and put inside less. In a same way (>=x) is used to filter the list for all elements larger than or equal to x. The sorted list is produced by sorting all elements that are smaller than x and putting that in front of x, then we sort all elements larger than x and put those at the end. We do this by using the list concatenation operator ++. Notice that x is not a list so the ++ operator won't work on it alone, which is why we make it a singleton-list by putting it inside brackets. We use the standard prelude function filter, the line less = filter (<x) xs will use filter (<x) xs to filter the list xs. You can see that we actually pass the function which will be used to filter the list to filter, an example of higher order functions. We will need to use the head and tail of the list later, we can actually extract this very elegantly by using pattern matching. You can think of it as naming the contents of the list. This can be done on any data construct, not just a list. The fist definition matches against [] which in Haskell is the empty list (a list of 1,2 and 3 is [1,2,3] so an empty list is just two brackets). If we try to sort an empty list, the result will be an empty list. The second definition pattern matches against a list with at least one element. It does this by using (x:xs) for its argument. : is the "cons" operator in Haskell, so 1 : [1,2,3] returns [1,1,2,3]. Pattern matching against (x:xs)  is a match against the list with the head x and the tail xs . How are the lists less and more computed? We don't care about sequencing in Haskell, so we've defined them below the function using the where notation (which allows any definitions to use the parameters of the function to which they belong). So the function reads "To sort the list, sandwich the head between the sorted list of all elements smaller than the head, and the sorted list of all elements larger than the head". Notice how little time it takes to get an understanding about what the function does. The function definitions in Haskell explain what it computes, not how. Haskell code is elegant and intuitive! First we see that we have two definitions of the functions. This is called pattern matching and it will test the argument passed to the function top-to-bottom and use the first one that matches.

Next time… More Haskell