Computer Science.

Abstract Functional programming languages are seen by many as instrumental to effectively utilizing the computational power of multi-core platforms. Fingerprint Functional programming. Computer programming languages. Application programming interfaces API. Computer science. Bricklayer: An authentic introduction to the functional programming language SML. Winter VL. Winter, Victor L. Access to Document A list is a sequence of items of the same type. There are two list constructors, the empty list nil and the cons operator The nil constructor is the list containing nothing, the :: operator takes an item on the left and a list on the right to give a list one longer than the original.

In fact the cons operator is right associative and so the brackets are not required. We can write nil for [3, 2, 1]. Notice how :: is always between an item and a list.

### Shop by category

The operator :: can be used to add a single item to the head of a list. The operator is used to append two lists together. It is a common mistake to confuse an item with a list containing a single item. For example to obtain the list starting with 4 followed by [5,6,7] we may write [5,6,7] or [4] [5,6,7] however 4 [5,6,7] or [4]::[5,6,7] both break the type rules.

To put 4 at the back of the list [5,6,7] we might try [5,6,7] however this breaks the type rules in both the first and the second parameter.

## Programming Languages, Part A

We must use the expression [5,6,7] [4] to get [5,6,7,4]. Now would be a good time to tackle Diversion: Distorting Bitmaps. One of the clever features of ML is that it can work out types in many cases. This is not always trivial. Consider the "reasoning" required to determine the type of a function such as. It is possible to over-ride the type inference mechanism, this may be desirable in the interests of software engineering or necessary where the mechanism cannot cope.

Consider the seemingly simple definition of sum which adds its two inputs:. This masterpiece of interface design is telling us that ML cannot work out if x and y are integers or real numbers. One solution would be to add zero, either integer or real. While this is effective it is scarcely elegant. We can specify the type of either or both parameters as follows. We have already seen how functions may be treated as objects when composing functions.

For example if the functions double and triple are defined we may create a function times6 as the composition of the double and triple. In the first case we explicitly define times6 to be the result of triple when applied to the result of double x. In the second case we apply the composition of triple and double to the parameter x and in the third case we do away with the parameter x altogether.

The function composition operator o has type. It accepts two functions and returns a third. Notice the order of function application is "back to front". The expression f o g is the function f applied to the function g - i.

### Navigation menu

The pre-defined function map applies a function over a list. Curry A function of more than one argument may be implemented as a function of a tuple or a "curried" function. After H B Curry.

Consider the function to add two integers Using tuples. The Curried version of this function is defined without the brackets or comma:. It is a function which takes an integer and returns a function from an integer to an integer.

- SML/NJ Literature!
- Lecture 2: An Introduction to the SML Language.
- Challenges of Conflicting School Reforms : Effects of New American Schools in a High-Poverty District 2002;
- Explore our Catalog?
- Greyhawk: Folk, Feuds, and Factions (part of The City of Greyhawk Boxed Set)(Advanced Dungeons and Dragons, 2nd ed: Greyhawk Adventures).
- Getting Started.

We can give both arguments without using a tuple. Giving one argument results in a "partial evaluation" of the function. For example applying the function add to the number 2 alone results in a function which adds two to its input:. Curried functions can be useful - particularly when supplying function as parameters to other functions. This would be a good time to consider the Diversion: Mandelbrot. In the examples so far we have been able to define functions using a single equation.

## Free Online Course: Programming Languages, Part A from Coursera | Class Central

We may use if then else in ML however pattern matching is preferred. Example: To change a verb from present to past tense we usually add "ed" as a suffix. The function past does this. When a function call is evaluated the system attempts to match the input the actual parameter with each equation in turn. Thus the call past "swim" is matched at the second attempt. The final equation has the free variable x as the formal parameter - this will match with any string not caught by the previous equations.

More on pattern matching later Using recursive functions we can achieve the sort of results which would require loops in a traditional language. Recursive functions tend to be much shorter and clearer. A recursive function is one which calls itself either directly or indirectly. Traditionally, the first recursive function considered is factorial.

## A Gentle Introduction to ML

A mathematician might define factorial as follows 0! This agrees with the definition and also serves as an implementation. To see how this works consider the execution of factorial 3. As 3 cannot be matched with 0 the second equation is used and we bind n to 3 resulting in. This generates a further call to factorial before the multiplication can take place. In evaluating factorial 2 the second equation is used but this time n is bound to 2.

The expression factorial 0 is dealt with by the first equation - it returns the value 1. We can now "unwind" the recursion. Note that in practice execution of this function requires stack space for each call and so in terms of memory use the execution of a recursive program is less efficient than a corresponding iterative program. As functional advocates we take a perverse pride in this.

It is very easy to write a non-terminating recursive function. To stop a non terminating function press control C. Be warned that some functions consume processing time and memory at a frightening rate.

Do not execute the function:. There are many useful in-built string and mathematical functions. In many versions of ML you can view these by opening the right structure. For example to see all the standard string functions enter:. A word of warning. If you open a structure you will lose the overloaded functions. For example you can usually use length on strings or lists, following the open String; statement you can no longer apply length to a list.

List processing and pattern matching Example : sum of a list Consider the function sum which adds all the elements of a list. There are two basic patterns for a list - that is there are two list constructors, :: and nil. The symbol :: is called cons, it has two components, nil is the empty list We can write equations for each of these constructors with completely general components. The empty list is easy - sum of all the elements in the empty list is zero.

In the cons case we need to consider the value of sum h::t. Where h is the head of the list - in this case an integer - and t is the tail of the list - i. In constructing recursive functions we can assume that the function works for a case which is in some sense "simpler" than the original. This leap of faith becomes easier with practice. In this case we can assume that function sum works for t.