10 steps to Functional Programming with Elan

Getting started

To undertake functional programming (FP) in Elan, you should first be reasonably familiar with procedural programming (PP) with Elan, and ideally, with object-oriented programming (OOP) with Elan. Then, switch to the functional profile from the menu at the top of the IDE. This changes two things:

  1. The list of demo programs will now show only programs that use FP. A very good place to start is to look at the Snake - functional demo, which is functionally identical to the (procedural) original, but it written using FP design and coding patterns.
  2. It will change some of the options available on the new code menus in the editor: The rationale for disallowing some of the instructions you are used to in PP or OOP, and how to use the new instructions, will become clear as you read through this document.

In the following sections we will describe 10 principles that define the essence of FP, illustrated with example code - where possible from the demo programs.

Important. Every textbook on FP differs slightly in the definition of core principles, and every language that supports FP differs in the rules that it enforces. The following 10 principles are based on widely recognised best practices in FP. When coding with Elan, in whichever of the supported languages - Python, VB, C#, or Java (or the Elan Reference Language) - these principles are, wherever possible, enforced by the Elan tooling. The resulting code is always valid syntax for the chosen language - but may depend upon common library methods defined in the Elan library. Also, if you go on to write code in any of those languages outside Elan, you will find that the same principles are not necessarily enforced. Though that might appear to offer you more freedom, sticking voluntarily to the principles that you learned in Elan will result in better code and fewer problems.

Principle 1: Functions must be pure

A pure function observes the following rules:

Dedicated Functional Programming languages — such as Haskell — enforce these rules automatically. Common 'multi-paradigm' languages — including Python, VB, C#, and Java — do not enforce them. However, if any of those languages are used within Elan, then the Elan editor enforces the rules automatically.

This means that every function you see within Elan, and every function you have ever written within Elan (in Python, VB, C#, or Java), is a pure function. That is the reason why, even when you are using the procedural or object-oriented profiles and you are within a function, you cannot print or call a procedure, nor can you make use of system methods such as input, random, or clock. This experience gives you a huge advantage in transitioning to FP.

Principle 2: As much of the code as possible should be in functions

When using FP in Python, VB, C#, or Java, every program still needs a main routine, which is necessarily procedural in nature, because it cannot work without dependencies on the system, and it generates side effects such as output to a display.

To avoid your main routine becoming difficult to read, and to avoid duplicated code, you may still define procedures (which also, inherently, use procedural programming to manage input/output and other system operations). But this second principle of FP implies that the main together with any necessary procedures should be kept as small as possible:

Most procedural programs contain some functions, but a program written within the discipline of FP should consist almost entirely of functions.

This may seem logical when a program is obviously heavily dependent upon calculation, logic, or data transformation — as, for example, the Best Fit and Wordle solver demo programs. But what about an interactive game, where most of the logic is concerned with managing the user input and the display? Consider the two demo programs: Snake - procedural and Snake - procedural. The two programs behave identically when run. We recommend that you take the time to explore the these two programs in detail,but here's a summary of the key differences:

Snake - procedural Snake - functional
Total instructions, excluding tests 72 115
Number of procedures, including main 4 1
Number of functions 4 21 (incl. function methods within Classes)
Instructions within procedures, including main 47 = 65% 11 = 10%
Instructions within functions 25 = 35% 104 = 90%

Principle 3: Every function can and should be unit tested

The idea of writing automated tests isn't restricted to Functional Programming. The tests offered by Elan are known as 'unit tests', and are designed to test functions.

Such tests are extremely easy to write, and run automatically when the program is loaded from a file, and then run again after every edit that results in code that compiles.

Unit tests give you huge confidence that your code does what it is supposed to do, and that subsequent changes, whether as a result of debugging or of implementing new requirements, do not break existing functionality. Without unit tests, bugs might go unnoticed and surface only later and less obviously.

On some platforms it is possible to write automated tests that also cover procedures (including the main routine). These are sometimes referred to as 'end to end tests', or 'UI tests' because they can simulate a user interacting with the program. But such tests are much harder to write than unit tests, and notoriously 'brittle' — meaning that a tiny change to code can break many existing tests and take hours to fix. Changes external to your program, such as a routine upgrade to the browser or operating system, can easily render such tests invalid. Elan does not currently support those kind of tests.

Note, however, that outside Elan, Python, VB, C#, and Java, have unit testing frameworks that can theoretically allow unit tests to run any method. This can be dangerous because running the tests might, without your awareness, be updating a file or database, or sending Internet messages. Unit tests are safe only when applied to safe (i.e. pure) functions.

It follows that the higher the percentage of a program's total instructions that exist within functions, the higher the percentage of your code that can be safely unit tested. The table below shows the percentage of instructions covered by tests for two of the demo programs that offer both procedural and functional implementations. You can verify this information by examining the demo code.

Snake - procedural Snake - functional Life - procedural Life - functional
Total instructions, excluding tests 72 115 78 76
Instructions covered by tests 53% 90% 73% 90%

Note on demo program Best Fit

Principle 4: A function consists of a 'return', with optional let instructions

In the FP demos you will see that a significant number of functions simply returnreturnreturnreturnreturn an expression to be evaluated, for example:

+function gameOvername?(g as Gameparameter definitions?) returns BooleanType? 1 return g.body.contains(g.head) or hasHitEdge(g)value or expression?2 end function
+def gameOvername?(g: Gameparameter definitions?) -> boolType?: # function1 return g.body.contains(g.head) or hasHitEdge(g)value or expression?2
+static boolType? gameOvername?(Game gparameter definitions?) { // function1 return g.body.contains(g.head) || hasHitEdge(g)value or expression?;2 } // function
+Function gameOvername?(g As Gameparameter definitions?) As BooleanType? 1 Return g.body.contains(g.head) Or hasHitEdge(g)value or expression?2 End Function
+static boolType? gameOvername?(Game gparameter definitions?) { // function1 return g.body.contains(g.head) || hasHitEdge(g)value or expression?;2 } // function

Other functions precede the returnreturnreturnreturnreturn by one or more let statements, for example

+function hasHitEdgename?(g as Gameparameter definitions?) returns BooleanType? 1 let xname? be g.head.xvalue or expression?2 let yname? be g.head.yvalue or expression?3 return (x is -1) or (y is -1) or (x is 40) or (y is 30)value or expression?4 end function
+def hasHitEdgename?(g: Gameparameter definitions?) -> boolType?: # function1 xname? = g.head.xvalue or expression? # let2 yname? = g.head.yvalue or expression? # let3 return (x == -1) or (y == -1) or (x == 40) or (y == 30)value or expression?4
+static boolType? hasHitEdgename?(Game gparameter definitions?) { // function1 var xname? = g.head.xvalue or expression?; // let2 var yname? = g.head.yvalue or expression?; // let3 return (x == -1) || (y == -1) || (x == 40) || (y == 30)value or expression?;4 } // function
+Function hasHitEdgename?(g As Gameparameter definitions?) As BooleanType? 1 Dim xname? = g.head.xvalue or expression? ' let2 Dim yname? = g.head.yvalue or expression? ' let3 Return (x = -1) Or (y = -1) Or (x = 40) Or (y = 30)value or expression?4 End Function
+static boolType? hasHitEdgename?(Game gparameter definitions?) { // function1 var xname? = g.head.xvalue or expression?; // let2 var yname? = g.head.yvalue or expression?; // let3 return (x == -1) || (y == -1) || (x == 40) || (y == 30)value or expression?;4 } // function

A let statement works like a variable definition except that the named value created may not be reassigned. But it differs from a constant in that:

In the four languages supported by Elan, let instruction are really just disguised variable definitions, executed sequentially. But in a pure FP language such as Haskell, OCAML, or F#, let instructions are executed lazily, meaning that within a function, execution starts with the returned expression, and only evaluates the sub-expressions defined in let instructions when - indeed if they are needed. So getting used to let instructions leaves you better prepared to move onto a pure FP language at a future point.

A let statement defines a 'sub-expression' that will be used in another expression, for one (or more) of these reasons:

When running in the functional profile, let statement is the only type of instruction that you may add within a function, and the only type in addition to an assert that may be added into a test. (You can still, of course, add comments.)

Principle 5: Selection is implemented using 'if expressions'

When running in the Functional profile, you cannot add an if instruction into a function. So how do you implement 'selection' and 'iteration'? Selection is handled by means of a 'conditional expression', whereas iteration will be addressed under Principle 6 .

All of the languages supported by Elan have their own custom syntax for conditional expressions. For simplicity, and understandability, Elan offers an if function that is used identically in all the languages. (Incidentally, the if function works the same way as it does in an Excel spreadsheet). Here are some examples of conditional expressions from the demo programs:

+function betterOfname?(wc1 as WordCount, wc2 as WordCount, possAnswers as List<of String>parameter definitions?) returns WordCountType? 1 let isBettername? be wc2.count < wc1.countvalue or expression?2 let isEqualAndPossAnswername? be (wc2.count is wc1.count) and possAnswers.contains(wc2.word)value or expression?3 return if(isBetter or isEqualAndPossAnswer, wc2, wc1)value or expression?4 end function
+def betterOfname?(wc1: WordCount, wc2: WordCount, possAnswers: list[str]parameter definitions?) -> WordCountType?: # function1 isBettername? = wc2.count < wc1.countvalue or expression? # let2 isEqualAndPossAnswername? = (wc2.count == wc1.count) and possAnswers.contains(wc2.word)value or expression? # let3 return if(isBetter or isEqualAndPossAnswer, wc2, wc1)value or expression?4
+static WordCountType? betterOfname?(WordCount wc1, WordCount wc2, List<string> possAnswersparameter definitions?) { // function1 var isBettername? = wc2.count < wc1.countvalue or expression?; // let2 var isEqualAndPossAnswername? = (wc2.count == wc1.count) && possAnswers.contains(wc2.word)value or expression?; // let3 return if(isBetter || isEqualAndPossAnswer, wc2, wc1)value or expression?;4 } // function
+Function betterOfname?(wc1 As WordCount, wc2 As WordCount, possAnswers As List(Of String)parameter definitions?) As WordCountType? 1 Dim isBettername? = wc2.count < wc1.countvalue or expression? ' let2 Dim isEqualAndPossAnswername? = (wc2.count = wc1.count) And possAnswers.contains(wc2.word)value or expression? ' let3 Return if(isBetter Or isEqualAndPossAnswer, wc2, wc1)value or expression?4 End Function
+static WordCountType? betterOfname?(WordCount wc1, WordCount wc2, List<String> possAnswersparameter definitions?) { // function1 var isBettername? = wc2.count < wc1.countvalue or expression?; // let2 var isEqualAndPossAnswername? = (wc2.count == wc1.count) && possAnswers.contains(wc2.word)value or expression?; // let3 return if(isBetter || isEqualAndPossAnswer, wc2, wc1)value or expression?;4 } // function
+function nextCellValuename?(grid as List<of List<of Int>>, x as Int, y as Intparameter definitions?) returns IntType? 1 let livename? be willLive(grid[x][y], liveNeighbours(grid, x, y))value or expression?2 return if(live, black, white)value or expression?3 end function
+def nextCellValuename?(grid: list[list[int]], x: int, y: intparameter definitions?) -> intType?: # function1 livename? = willLive(grid[x][y], liveNeighbours(grid, x, y))value or expression? # let2 return if(live, black, white)value or expression?3
+static intType? nextCellValuename?(List<List<int>> grid, int x, int yparameter definitions?) { // function1 var livename? = willLive(grid[x][y], liveNeighbours(grid, x, y))value or expression?; // let2 return if(live, black, white)value or expression?;3 } // function
+Function nextCellValuename?(grid As List(Of List(Of Integer)), x As Integer, y As Integerparameter definitions?) As IntegerType? 1 Dim livename? = willLive(grid[x][y], liveNeighbours(grid, x, y))value or expression? ' let2 Return if(live, black, white)value or expression?3 End Function
+static intType? nextCellValuename?(List<List<int>> grid, int x, int yparameter definitions?) { // function1 var livename? = willLive(grid[x][y], liveNeighbours(grid, x, y))value or expression?; // let2 return if(live, black, white)value or expression?;3 } // function

The if function requires three arguments to be provided, in the following order:

  1. The condition, which may compare two values, or it may call another function that returns a Boolean value
  2. The value to be returned by the if function when the condition evaluates to true
  3. The value to be returned by the if function when the condition evaluates to false

Thus the if function performs a role somewhat similar to an if instruction, but differs from it in these important respects:

Although if functions can be nested, nesting to more than one level can become very hard to read. When required, it is often better to delegate to a new function to perform part of the logic.

Principle 6: Iteration may be achieved using recursion

There are two ways to implement iteration within a function:

Either by using 'recursion', i.e. a recursive function (described here), or by the approach described in Principle 8 .

A recursive function is one that at some point evaluates itself, and is best understood by example. In the Recursive Functions demo, the factorial function is a good place to start:

+function factorialname?(n as Intparameter definitions?) returns IntType? 1 return if(n < 2, 1, n*factorial(n - 1))value or expression?2 end function
+def factorialname?(n: intparameter definitions?) -> intType?: # function1 return if(n < 2, 1, n*factorial(n - 1))value or expression?2
+static intType? factorialname?(int nparameter definitions?) { // function1 return if(n < 2, 1, n*factorial(n - 1))value or expression?;2 } // function
+Function factorialname?(n As Integerparameter definitions?) As IntegerType? 1 Return if(n < 2, 1, n*factorial(n - 1))value or expression?2 End Function
+static intType? factorialname?(int nparameter definitions?) { // function1 return if(n < 2, 1, n*factorial(n - 1))value or expression?;2 } // function

It shows these important aspects of designing a recursive function:

Principle 7: Lists and other data structures are never mutated

In procedural programming a List may be mutated either by reassigning an indexed value, for example:

reassign mySubjects[3]variableName? to "Geography"value or expression?0
mySubjects[3]variableName? = "Geography"value or expression? # reassign variable0
mySubjects[3]variableName? = "Geography"value or expression?; // reassign variable0
mySubjects[3]variableName? = "Geography"value or expression? ' reassign variable0
mySubjects[3]variableName? = "Geography"value or expression?; // reassign variable0
or by calling one of the available procedure methods on on it such as:
call mySubjects.appendprocedureName?("Music"arguments?)0
mySubjects.appendprocedureName?("Music"arguments?) # call procedure0
mySubjects.appendprocedureName?("Music"arguments?); // call procedure0
mySubjects.appendprocedureName?("Music"arguments?) ' call procedure0
mySubjects.appendprocedureName?("Music"arguments?); // call procedure0
but in FP this is not permitted. Data structures are never to be mutated. Instead, you may call a function that returns a copy of a data structure with specified differences from the original. For example, you may write:
let myNewSubjects be mySubjects.withSet(3, "Geography") Code does not parse as Elan.
let myNewSubjects be mySubjects.withSet(3, "Geography") Code does not parse as Elan.
let myNewSubjects be mySubjects.withSet(3, "Geography") Code does not parse as Elan.
let myNewSubjects be mySubjects.withSet(3, "Geography") Code does not parse as Elan.
let myNewSubjects be mySubjects.withSet(3, "Geography") Code does not parse as Elan.
or
let myNewSubjects be mySubjects.withAppend("Music") Code does not parse as Elan.
let myNewSubjects be mySubjects.withAppend("Music") Code does not parse as Elan.
let myNewSubjects be mySubjects.withAppend("Music") Code does not parse as Elan.
let myNewSubjects be mySubjects.withAppend("Music") Code does not parse as Elan.
let myNewSubjects be mySubjects.withAppend("Music") Code does not parse as Elan.

At first sight this may seem awkward, strange and certainly inefficient. But though you might never have thought about it in these terms, this is what you have always done when manipulating a StringstrstringStringString. You can never actually mutate a string, such as converting it to upper case or trimming off the leading spaces. You have to write, for example:

let myNewString be myString.upperCase() Code does not parse as Elan.
let myNewString be myString.upperCase() Code does not parse as Elan.
let myNewString be myString.upperCase() Code does not parse as Elan.
let myNewString be myString.upperCase() Code does not parse as Elan.
let myNewString be myString.upperCase() Code does not parse as Elan.

When you want to apply operations to all items in a List you need to use iteration, which means using either recursion or Higher-order Functions and within that technique make use of the various with.. methods. The sum function in the Recursive Functions demo calculates the sum of all items in any List of floating point numbers (the tests included in the demo show example results):

+function sumname?(li as List<of Float>parameter definitions?) returns FloatType? 1 return if(li.length() is 0, 0.0, li.head() + sum(li.tail()))value or expression?2 end function
+def sumname?(li: list[float]parameter definitions?) -> floatType?: # function1 return if(li.length() == 0, 0.0, li.head() + sum(li.tail()))value or expression?2
+static doubleType? sumname?(List<double> liparameter definitions?) { // function1 return if(li.length() == 0, 0.0, li.head() + sum(li.tail()))value or expression?;2 } // function
+Function sumname?(li As List(Of Double)parameter definitions?) As DoubleType? 1 Return if(li.length() = 0, 0.0, li.head() + sum(li.tail()))value or expression?2 End Function
+static doubleType? sumname?(List<double> liparameter definitions?) { // function1 return if(li.length() == 0, 0.0, li.head() + sum(li.tail()))value or expression?;2 } // function

Note the use of these two dot function calls on the List lilililili(see Dot syntax in Language Reference):

In FP, algorithms to process lists are often written in terms of the 'head' and 'tail' of the list. You could just write e.g. li[0]li[0]li[0]li[0]li[0] and li.substring(1, li.length())li.substring(1, li.length())li.substring(1, li.length())li.substring(1, li.length())li.substring(1, li.length()) instead, with the same result. But using the head and tail methods makes your code easier to read and may make your intention clearer.

The next example combines the previous two ideas:

in a function to reverse the order or items in a List:
+function reversename?(li as List<of Float>parameter definitions?) returns List<of Float>Type? 1 return if(li.length() < 2, li, reverse(li.tail()).withAppend(li.head()))value or expression?2 end function
+def reversename?(li: list[float]parameter definitions?) -> list[float]Type?: # function1 return if(li.length() < 2, li, reverse(li.tail()).withAppend(li.head()))value or expression?2
+static List<double>Type? reversename?(List<double> liparameter definitions?) { // function1 return if(li.length() < 2, li, reverse(li.tail()).withAppend(li.head()))value or expression?;2 } // function
+Function reversename?(li As List(Of Double)parameter definitions?) As List(Of Double)Type? 1 Return if(li.length() < 2, li, reverse(li.tail()).withAppend(li.head()))value or expression?2 End Function
+static List<double>Type? reversename?(List<double> liparameter definitions?) { // function1 return if(li.length() < 2, li, reverse(li.tail()).withAppend(li.head()))value or expression?;2 } // function

A more advanced example is the Merge Sort demo, where:

Principle 8: A function may be passed into, or returned by, another function

The alternative to writing your own recursive function is to use Higher-order Functions (see HoFs in Library Reference). A HoF is a function that takes in another function as a parameter (the more common case), or one that returns a function as its result. Under the covers, HoFs are written using recursion, but you don't see this.

There are many possible HoFs, but three of them — filter, map and reduce — are so widely used to process lists in FP, and in many different ways, that they are sometimes referred to as the 'Holy Trinity of Functional Programming'. They can be used individually or in combination. The purpose and operation of each is explained below.

All of the languages supported by Elan have their own versions of filter, map and reduce that are similar in intent but have differing syntax, and sometimes different names (e.g. 'fold' in place of 'reduce').The Elan library has its own version of these functions which work the same way in each language.

Start by loading and running the demo program Map, Filter, Reduce, and look at the test:

+test test_Map_Filter_Reducetest_name? 1 let numsname? be [2.22, 5.37, 8.97, 7.53, 8.2, 9.43, 7.74, 7.03, 9.62, 2.5]value or expression?2 assert nums.filter(lessThan5)actual (computed) value? is [2.22, 2.5]expected value? not run3 assert nums.map(cube)actual (computed) value? is [10.94, 154.85, 721.73, 426.96, 551.37, 838.56, 463.68, 347.43, 890.28, 15.63]expected value? not run4 assert nums.reduce(1.0, product).floor()actual (computed) value? is 81480107expected value? not run5 assert nums.filter(lessThan5).map(inverse)actual (computed) value? is [0.45, 0.4]expected value? not run6 assert nums.filter(lessThan5).map(inverse).map(toString).reduce("results: ", concat)actual (computed) value? is "results: 0.45|0.4|"expected value? not run7 end test
+def test_Map_Filter_Reducetest_name?(self) -> None: 1 numsname? = [2.22, 5.37, 8.97, 7.53, 8.2, 9.43, 7.74, 7.03, 9.62, 2.5]value or expression? # let2 self.assertEqual(nums.filter(lessThan5)actual (computed) value?, [2.22, 2.5]expected value?) not run3 self.assertEqual(nums.map(cube)actual (computed) value?, [10.94, 154.85, 721.73, 426.96, 551.37, 838.56, 463.68, 347.43, 890.28, 15.63]expected value?) not run4 self.assertEqual(nums.reduce(1.0, product).floor()actual (computed) value?, 81480107expected value?) not run5 self.assertEqual(nums.filter(lessThan5).map(inverse)actual (computed) value?, [0.45, 0.4]expected value?) not run6 self.assertEqual(nums.filter(lessThan5).map(inverse).map(toString).reduce("results: ", concat)actual (computed) value?, "results: 0.45|0.4|"expected value?) not run7
+[TestMethod] static void test_Map_Filter_Reducetest_name?() { 1 var numsname? = [2.22, 5.37, 8.97, 7.53, 8.2, 9.43, 7.74, 7.03, 9.62, 2.5]value or expression?; // let2 Assert.AreEqual([2.22, 2.5]expected value?, nums.filter(lessThan5)actual (computed) value?) not run3 Assert.AreEqual([10.94, 154.85, 721.73, 426.96, 551.37, 838.56, 463.68, 347.43, 890.28, 15.63]expected value?, nums.map(cube)actual (computed) value?) not run4 Assert.AreEqual(81480107expected value?, nums.reduce(1.0, product).floor()actual (computed) value?) not run5 Assert.AreEqual([0.45, 0.4]expected value?, nums.filter(lessThan5).map(inverse)actual (computed) value?) not run6 Assert.AreEqual("results: 0.45|0.4|"expected value?, nums.filter(lessThan5).map(inverse).map(toString).reduce("results: ", concat)actual (computed) value?) not run7 } // test
+<TestMethod> Sub test_Map_Filter_Reducetest_name?() 1 Dim numsname? = {2.22, 5.37, 8.97, 7.53, 8.2, 9.43, 7.74, 7.03, 9.62, 2.5}value or expression? ' let2 Assert.AreEqual({2.22, 2.5}expected value?, nums.filter(lessThan5)actual (computed) value?) not run3 Assert.AreEqual({10.94, 154.85, 721.73, 426.96, 551.37, 838.56, 463.68, 347.43, 890.28, 15.63}expected value?, nums.map(cube)actual (computed) value?) not run4 Assert.AreEqual(81480107expected value?, nums.reduce(1.0, product).floor()actual (computed) value?) not run5 Assert.AreEqual({0.45, 0.4}expected value?, nums.filter(lessThan5).map(inverse)actual (computed) value?) not run6 Assert.AreEqual("results: 0.45|0.4|"expected value?, nums.filter(lessThan5).map(inverse).map(toString).reduce("results: ", concat)actual (computed) value?) not run7 End Sub
+@Test static void test_Map_Filter_Reducetest_name?() { 1 var numsname? = [2.22, 5.37, 8.97, 7.53, 8.2, 9.43, 7.74, 7.03, 9.62, 2.5]value or expression?; // let2 assertEquals([2.22, 2.5]expected value?, nums.filter(lessThan5)actual (computed) value?) not run3 assertEquals([10.94, 154.85, 721.73, 426.96, 551.37, 838.56, 463.68, 347.43, 890.28, 15.63]expected value?, nums.map(cube)actual (computed) value?) not run4 assertEquals(81480107expected value?, nums.reduce(1.0, product).floor()actual (computed) value?) not run5 assertEquals([0.45, 0.4]expected value?, nums.filter(lessThan5).map(inverse)actual (computed) value?) not run6 assertEquals("results: 0.45|0.4|"expected value?, nums.filter(lessThan5).map(inverse).map(toString).reduce("results: ", concat)actual (computed) value?) not run7 } // test

Each of the three HoFs is applied to a List by a 'dot method call'. In this example each is applied to the same List (of floating point numbers), though the subject List may be of any Type. Because they are all dot methods, they can easily be chained in dot sequences, as shown.

Each HoF is passed the name of a function that is defined elsewhere in the program. The function name is given as an argument, without brackets or arguments, e.g. lessThan5. For filter and map, this is the only argument; for reduce there is another argument. Taking each HoF in turn:

filter

map

reduce

filter and map methods may be chained, and in any order, provided that the item Type of each resulting List is of the Type expected by the next one.

Because reduce transforms a List into a single value, it is typically used in isolation or as the last of a chain. It can, of course, be followed in the chain by a simpler function (such as round).

Lambdas

In the examples above the function being passed into the HoF (using its name) is defined as a 'standalone' (global) function elsewhere in the program. This pattern has two advantages:>/p>

However, there are many circumstances where the function you need to pass into a HoF, is unlikely to be used in any different context, and where the function is so simple that it hardly warrants its own test — especially if the whole expression that uses the HoF is being tested.

A lambda is a function that is defined 'inline' as an argument in a function call. For example, the expression from the example above:

nums.filter(lessThan5)
nums.filter(lessThan5)
nums.filter(lessThan5)
nums.filter(lessThan5)
nums.filter(lessThan5)
could be written as:
nums.filter(lambda x as Int => x < 5)
nums.filter(lambda x: int: x < 5)
nums.filter(int x => x < 5)
nums.filter(Function (x As Integer) x < 5)
nums.filter((int x) -> x < 5)

thereby eliminating the need to define the lessThan5 function for one use only.

There is a whole branch of mathematics devoted to 'lambda calculus', which in turn provided the theoretical underpinning for the concept of function programming (FP). And lambdas have capabilities that are both more subtle and more sophisticated than can be described here, including the idea of 'closure' (which you can look up if you are ready to go much deeper — and which is supported in Elan). However, you should note that, because of the need for compatibility across all the supported languages, Elan has a restricted implementation of HoFs and of 'function passing' in general, but it is sufficient to grasp the key principles of Functional Programming. To go further you would need to switch to a pure FP language such as Haskell. Currently Elan does not permit (in any of the supported languages):

Using HoFs to iterate over a 2D List

If you are using Elan's Block Graphics, or have another need to process a 2D List (such as working with matrices or tabular data), and you want to do this in pure functions, you have the challenge of how to iterate over two dimensions — something that is straightforward in procedural programming by means of 'nested loops'. It is certainly possible to iterate over two dimensions of a List using HoFs within a single expression, but until you are fluent in such programming, the recommended approach is to break up the task into an 'inner' function that processes just a single column (or row, if preferred) of the 2D structure and an 'outer' function that processes each of the columns (or rows).

This is exemplified in the following functions, taken from the Life - functional demo, which together perform a role equivalent to the nextGeneration procedure in the Life - procedural demo:

+function nextGenerationname?(grid as List<of List<of Int>>parameter definitions?) returns List<of List<of Int>>Type? 1 let colsname? be range(0, 40)value or expression?2 return cols.map(lambda x as Int => nextCol(grid, x))value or expression?3 end function +function nextColname?(grid as List<of List<of Int>>, x as Intparameter definitions?) returns List<of Int>Type? 4 let colname? be grid[x]value or expression?5 let rowsname? be range(0, 30)value or expression?6 return rows.map(lambda y as Int => nextCellValue(grid, x, y))value or expression?7 end function
+def nextGenerationname?(grid: list[list[int]]parameter definitions?) -> list[list[int]]Type?: # function1 colsname? = range(0, 40)value or expression? # let2 return cols.map(lambda x: int: nextCol(grid, x))value or expression?3 +def nextColname?(grid: list[list[int]], x: intparameter definitions?) -> list[int]Type?: # function4 colname? = grid[x]value or expression? # let5 rowsname? = range(0, 30)value or expression? # let6 return rows.map(lambda y: int: nextCellValue(grid, x, y))value or expression?7
+static List<List<int>>Type? nextGenerationname?(List<List<int>> gridparameter definitions?) { // function1 var colsname? = range(0, 40)value or expression?; // let2 return cols.map(int x => nextCol(grid, x))value or expression?;3 } // function +static List<int>Type? nextColname?(List<List<int>> grid, int xparameter definitions?) { // function4 var colname? = grid[x]value or expression?; // let5 var rowsname? = range(0, 30)value or expression?; // let6 return rows.map(int y => nextCellValue(grid, x, y))value or expression?;7 } // function
+Function nextGenerationname?(grid As List(Of List(Of Integer))parameter definitions?) As List(Of List(Of Integer))Type? 1 Dim colsname? = range(0, 40)value or expression? ' let2 Return cols.map(Function (x As Integer) nextCol(grid, x))value or expression?3 End Function +Function nextColname?(grid As List(Of List(Of Integer)), x As Integerparameter definitions?) As List(Of Integer)Type? 4 Dim colname? = grid[x]value or expression? ' let5 Dim rowsname? = range(0, 30)value or expression? ' let6 Return rows.map(Function (y As Integer) nextCellValue(grid, x, y))value or expression?7 End Function
+static List<List<int>>Type? nextGenerationname?(List<List<int>> gridparameter definitions?) { // function1 var colsname? = range(0, 40)value or expression?; // let2 return cols.map((int x) -> nextCol(grid, x))value or expression?;3 } // function +static List<int>Type? nextColname?(List<List<int>> grid, int xparameter definitions?) { // function4 var colname? = grid[x]value or expression?; // let5 var rowsname? = range(0, 30)value or expression?; // let6 return rows.map((int y) -> nextCellValue(grid, x, y))value or expression?;7 } // function

Note the use of range in both functions to create colscolscolscolscols and rowsrowsrowsrowsrows respectively, which are just Lists of numbers e.g. [0, 1, 2, .. , 39]. The map HoFs iterate over these column and row numbers using them to access the appropriate portions of the gridgridgridgridgrid, which is a 2D List<of List<of Int>>list[list[int]]List<List<int>>List(Of List(Of Integer))List<List<int>>.

Principle 9: User-defined Classes should be immutable

It is possible, indeed highly desirable, to have user-defined Types (i.e. Classes) in FP. However, in true FP, an instance of a Class would never be mutated. So, when running under the functional profile, you may define Classes and add properties and function methods, but you may not define any procedure method. (In Object-oriented Programming you would use procedure methods to mutate an instance or do anything that cannot be done by a function.)

But this raises a question: how are we to make changes to an instance of a Class?. The answer is similar to that for updating a List, String, or other standard data structure within a function (described in Principle 7 ): you need define with.. methods that return a copy of the instance with specified changes. For example, in the Snake - functional demo, the Class GameGameGameGameGame has several with.. methods, one of which is shown here:

+function withHeadname?(value as Squareparameter definitions?) returns GameType? 1 let copyOfThisname? be copy(this)value or expression?2 reassign copyOfThis.headvariableName? to valuevalue or expression?3 return copyOfThisvalue or expression?4 end function
+def withHeadname?(value: Squareparameter definitions?) -> GameType?: # function1 copyOfThisname? = copy(self)value or expression? # let2 copyOfThis.headvariableName? = valuevalue or expression? # reassign variable3 return copyOfThisvalue or expression?4
+static GameType? withHeadname?(Square valueparameter definitions?) { // function1 var copyOfThisname? = copy(this)value or expression?; // let2 copyOfThis.headvariableName? = valuevalue or expression?; // reassign variable3 return copyOfThisvalue or expression?;4 } // function
+Function withHeadname?(value As Squareparameter definitions?) As GameType? 1 Dim copyOfThisname? = copy(Me)value or expression? ' let2 copyOfThis.headvariableName? = valuevalue or expression? ' reassign variable3 Return copyOfThisvalue or expression?4 End Function
+static GameType? withHeadname?(Square valueparameter definitions?) { // function1 var copyOfThisname? = copy(this)value or expression?; // let2 copyOfThis.headvariableName? = valuevalue or expression?; // reassign variable3 return copyOfThisvalue or expression?;4 } // function
Note that it:

Although a with.. method is a pure function, the standard form of a with.. method, like the one above, must be created using the with method instruction template, which is offered only within a Class, and only when using the functional profile.

The reason is that only within the with.. method are you allowed to reassign a property, and this may only be on the a value named copyOfThiscopyOfThiscopyOfThiscopyOfThiscopyOfThis. You cannot, for example, edit it to say e.g. reassign this.xvariableName? to valuevalue or expression?0self.xvariableName? = valuevalue or expression? # reassign variable0this.xvariableName? = valuevalue or expression?; // reassign variable0Me.xvariableName? = valuevalue or expression? ' reassign variable0this.xvariableName? = valuevalue or expression?; // reassign variable0, since that would make changes observable from outside the function.

Note, in the same demo, that the Class SquareSquareSquareSquareSquare does not define any with.. methods because there is no need within the program to update any instance of Square — instead, a new instance is created, specifying the xxxxx and yyyyy coordinates as needed. For this reason the method named withNewApple does not need to be able to reassign a property on a copy, but does need to do other work. It has been written using the standard function method instruction template.

Principle 10: Generating Random numbers within a function requires a special pattern

Warning: This is an advanced technique and we recommend not attempting to use it until you are fairly comfortable with all the previous Principles.

In procedural programming you use the 'system methods' random (for a floating point number in the range 0 to 1) or randInt for an integer in a specified range. You may use these system methods within the main routine or a procedure, and you may pass these random values into a function, but you may not generate one or more random values within a function. When writing games and simulations that rely heavily on generating random numbers this can make it very difficult to achieve the primary objective of doing all data transformations within functions. Fortunately, there is a mechanism to generate random numbers within functions. Before looking at how to use it, it is important to understand the following:

The 'functional Random' pattern, works within these constraints. If you want to generate one or more random numbers within a function, you must pass in the 'state' needed to derive that next random value, and you must pass the updated state to the next place where a new random is required, either inside or outside the function. This is done using an instance of the Type RandomRandomRandomRandomRandom (note the capital R, which distinguishes this from the procedural methods) which may be thought of as an instance of a 'Random Number Generator'.

To see how RandomRandomRandomRandomRandom works, we will first explore it within a tiny program intended to show the result of rolling a dice 10 times:

+main 1 variable rngname? set to new Random()value or expression?2 +for iitem? in range(1, 10)source? 3 call printprocedureName?(rollDice(rng)arguments?)4 end for end main +function rollDicename?(rng as Randomparameter definitions?) returns IntType? 5 return rng.asInt(1, 6)value or expression?6 end function
+def main() -> None: 1 rngname? = Random()value or expression? # variable definition2 +for iitem? in range(1, 10)source?: 3 printprocedureName?(rollDice(rng)arguments?)4 +def rollDicename?(rng: Randomparameter definitions?) -> intType?: # function5 return rng.asInt(1, 6)value or expression?6 main()
+static void main() { 1 var rngname? = new Random()value or expression?;2 +foreach (iitem? in range(1, 10)source?) { 3 printprocedureName?(rollDice(rng)arguments?);4 } // foreach } // main +static intType? rollDicename?(Random rngparameter definitions?) { // function5 return rng.asInt(1, 6)value or expression?;6 } // function
+Sub main() 1 Dim rngname? = New Random()value or expression? ' variable definition2 +For Each iitem? In range(1, 10)source? 3 printprocedureName?(rollDice(rng)arguments?)4 Next i End Sub +Function rollDicename?(rng As Randomparameter definitions?) As IntegerType? 5 Return rng.asInt(1, 6)value or expression?6 End Function
+static void main() { 1 var rngname? = new Random()value or expression?;2 +foreach (iitem? in range(1, 10)source?) { 3 printprocedureName?(rollDice(rng)arguments?);4 } // foreach } // main +static intType? rollDicename?(Random rngparameter definitions?) { // function5 return rng.asInt(1, 6)value or expression?;6 } // function
If you run this program you will see that it generates the same value (namely 3) each time! We need to change it so that after each roll the variable rngrngrngrngrng is updated to the next value of the Random number generator. So changing just the main routine to the following will generate a more random looking sequence:
+main 1 variable rngname? set to new Random()value or expression?2 +for iitem? in range(1, 10)source? 3 call printprocedureName?(rollDice(rng)arguments?)4 reassign rngvariableName? to rng.nextGen()value or expression?5 end for end main
+def main() -> None: 1 rngname? = Random()value or expression? # variable definition2 +for iitem? in range(1, 10)source?: 3 printprocedureName?(rollDice(rng)arguments?)4 rngvariableName? = rng.nextGen()value or expression? # reassign variable5 main()
+static void main() { 1 var rngname? = new Random()value or expression?;2 +foreach (iitem? in range(1, 10)source?) { 3 printprocedureName?(rollDice(rng)arguments?);4 rngvariableName? = rng.nextGen()value or expression?; // reassign variable5 } // foreach } // main
+Sub main() 1 Dim rngname? = New Random()value or expression? ' variable definition2 +For Each iitem? In range(1, 10)source? 3 printprocedureName?(rollDice(rng)arguments?)4 rngvariableName? = rng.nextGen()value or expression? ' reassign variable5 Next i End Sub
+static void main() { 1 var rngname? = new Random()value or expression?;2 +foreach (iitem? in range(1, 10)source?) { 3 printprocedureName?(rollDice(rng)arguments?);4 rngvariableName? = rng.nextGen()value or expression?; // reassign variable5 } // foreach } // main

Now when you run the same program it produces random looking values, but always in the same sequence. This effect can be very useful for testing code, but it makes for very predictable games!

To overcome this predictability, initialise the random generator from the system clock (given that it changes 1,000 times per second) which will make it almost impossible to predict the sequence:

+main 1 variable rngname? set to new Random()value or expression?2 call rng.initialiseFromClockprocedureName?(arguments?)3 +for iitem? in range(1, 10)source? 4 call printprocedureName?(rollDice(rng)arguments?)5 reassign rngvariableName? to rng.nextGen()value or expression?6 end for end main
+def main() -> None: 1 rngname? = Random()value or expression? # variable definition2 rng.initialiseFromClockprocedureName?(arguments?) # call procedure3 +for iitem? in range(1, 10)source?: 4 printprocedureName?(rollDice(rng)arguments?)5 rngvariableName? = rng.nextGen()value or expression? # reassign variable6 main()
+static void main() { 1 var rngname? = new Random()value or expression?;2 rng.initialiseFromClockprocedureName?(arguments?); // call procedure3 +foreach (iitem? in range(1, 10)source?) { 4 printprocedureName?(rollDice(rng)arguments?);5 rngvariableName? = rng.nextGen()value or expression?; // reassign variable6 } // foreach } // main
+Sub main() 1 Dim rngname? = New Random()value or expression? ' variable definition2 rng.initialiseFromClockprocedureName?(arguments?) ' call procedure3 +For Each iitem? In range(1, 10)source? 4 printprocedureName?(rollDice(rng)arguments?)5 rngvariableName? = rng.nextGen()value or expression? ' reassign variable6 Next i End Sub
+static void main() { 1 var rngname? = new Random()value or expression?;2 rng.initialiseFromClockprocedureName?(arguments?); // call procedure3 +foreach (iitem? in range(1, 10)source?) { 4 printprocedureName?(rollDice(rng)arguments?);5 rngvariableName? = rng.nextGen()value or expression?; // reassign variable6 } // foreach } // main

This now works well. But if we are going to have to go back out to the main routine between each use of RandomRandomRandomRandomRandom, we might as well have used the procedural methods in the first place. The following example shows something that could not be done that way: creating a List of random dice-throws entirely within a function:

+main 1 variable rngname? set to new Random()value or expression?2 call rng.initialiseFromClockprocedureName?(arguments?)3 call printprocedureName?(randomList(rng).item_0arguments?)4 end main +function randomListname?(rng as Randomparameter definitions?) returns (List<of Int>, Random)Type? 5 let liname? be new List<of Int>()value or expression?6 let itemsname? be range(0, 10)value or expression?7 return items.reduce((li, rng), appendDiceThrow)value or expression?8 end function +function appendDiceThrowname?(tup as (List<of Int>, Random), row as Intparameter definitions?) returns (List<of Int>, Random)Type? 9 let colname? be tup.item_0value or expression?10 let rngname? be tup.item_1value or expression?11 return (col.withAppend(rng.asInt(1, 6)), rng.nextGen())value or expression?12 end function
+def main() -> None: 1 rngname? = Random()value or expression? # variable definition2 rng.initialiseFromClockprocedureName?(arguments?) # call procedure3 printprocedureName?(randomList(rng).item_0arguments?)4 +def randomListname?(rng: Randomparameter definitions?) -> tuple[list[int], Random]Type?: # function5 liname? = list[int]()value or expression? # let6 itemsname? = range(0, 10)value or expression? # let7 return items.reduce((li, rng), appendDiceThrow)value or expression?8 +def appendDiceThrowname?(tup: tuple[list[int], Random], row: intparameter definitions?) -> tuple[list[int], Random]Type?: # function9 colname? = tup.item_0value or expression? # let10 rngname? = tup.item_1value or expression? # let11 return (col.withAppend(rng.asInt(1, 6)), rng.nextGen())value or expression?12 main()
+static void main() { 1 var rngname? = new Random()value or expression?;2 rng.initialiseFromClockprocedureName?(arguments?); // call procedure3 printprocedureName?(randomList(rng).item_0arguments?);4 } // main +static (List<int>, Random)Type? randomListname?(Random rngparameter definitions?) { // function5 var liname? = new List<int>()value or expression?; // let6 var itemsname? = range(0, 10)value or expression?; // let7 return items.reduce((li, rng), appendDiceThrow)value or expression?;8 } // function +static (List<int>, Random)Type? appendDiceThrowname?((List<int>, Random) tup, int rowparameter definitions?) { // function9 var colname? = tup.item_0value or expression?; // let10 var rngname? = tup.item_1value or expression?; // let11 return (col.withAppend(rng.asInt(1, 6)), rng.nextGen())value or expression?;12 } // function
+Sub main() 1 Dim rngname? = New Random()value or expression? ' variable definition2 rng.initialiseFromClockprocedureName?(arguments?) ' call procedure3 printprocedureName?(randomList(rng).item_0arguments?)4 End Sub +Function randomListname?(rng As Randomparameter definitions?) As (List(Of Integer), Random)Type? 5 Dim liname? = New List(Of Integer)()value or expression? ' let6 Dim itemsname? = range(0, 10)value or expression? ' let7 Return items.reduce((li, rng), appendDiceThrow)value or expression?8 End Function +Function appendDiceThrowname?(tup As (List(Of Integer), Random), row As Integerparameter definitions?) As (List(Of Integer), Random)Type? 9 Dim colname? = tup.item_0value or expression? ' let10 Dim rngname? = tup.item_1value or expression? ' let11 Return (col.withAppend(rng.asInt(1, 6)), rng.nextGen())value or expression?12 End Function
+static void main() { 1 var rngname? = new Random()value or expression?;2 rng.initialiseFromClockprocedureName?(arguments?); // call procedure3 printprocedureName?(randomList(rng).item_0arguments?);4 } // main +static (List<int>, Random)Type? randomListname?(Random rngparameter definitions?) { // function5 var liname? = new List<int>()value or expression?; // let6 var itemsname? = range(0, 10)value or expression?; // let7 return items.reduce((li, rng), appendDiceThrow)value or expression?;8 } // function +static (List<int>, Random)Type? appendDiceThrowname?((List<int>, Random) tup, int rowparameter definitions?) { // function9 var colname? = tup.item_0value or expression?; // let10 var rngname? = tup.item_1value or expression?; // let11 return (col.withAppend(rng.asInt(1, 6)), rng.nextGen())value or expression?;12 } // function

In Life - functional you can find this pattern employed to create a randomised initial grid of random black or white blocks.


Introduction to Functional Programming with Elan go to the top