Presentation is loading. Please wait.

Presentation is loading. Please wait.

More Functional Programming

Similar presentations


Presentation on theme: "More Functional Programming"— Presentation transcript:

1 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

2 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.

3 Miranda is an ML-like language developed in 1985-1986.
Haskell History: Miranda is an ML-like language developed in 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.

4 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."

5 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

6 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.

7 Next time… More Haskell


Download ppt "More Functional Programming"

Similar presentations


Ads by Google