Learning Elm Part 0

Intro To Elm

Header   ignore

What is Elm?

Elm is a functional programming language for building front-end web applications

Elm Repl

  • Although we will primarily program using stabl.rocks, I recommend installing elm on your local machine to use the elm repl
  • Go to https://guide.elm-lang.org/install/elm.html and download and run the elm installer
  • To use the repl, open up a terminal/command prompt and enter elm repl

Functional Programming

  • A paradigm that emphasizes the use of higher order functions to construct programs as expressions
  • This is in contrast to imperative programming which constructs programs as a sequence of statements

Functional vs Imperative

  • Elm
function =
    let
        xs = [1,2,3]
    in sum (map (\x -> x + 1) xs)
  • Python
function():
    xs = [1,2,3]
    result = 0
    for x in xs:
        x = x + 1
        result = result + x
    return result

Function Application

  • In Elm, function application is denoted using space, ex:

       function var1 var2
    
  • Moreover function application is assumed to have higher priority than all other operators, ex:

       sin 0+2 = (sin 0) + 2
    

Examples

Mathematics Elm
\(f(x)\) \(f \quad x\)
\(f(x,y)\) \(f \quad x \quad y\)
\(f(g(x))\) \(f \quad (g \quad x)\)
\(f(x,g(y))\) \(f \quad x \quad (g \quad y)\)
\(f(x)g(y)\) \(f \quad x \quad * \quad g \quad y\)

Lists

  • Allows grouping together of values
  • Lists are created by putting values inside square brackets seperated by commas
  • For example, [1,5,3,2]

The Core Library

Elm has core libraries (i.e predefined functions) that are fequently used

Some Useful List Functions

  • Reverse a list

        List.reverse [1,2,3,4]
    
  • Find the length of a list

        List.length [1,2,3,4]
    
  • Sum the elements of a list (of numbers)

        List.sum [1,1,1]
    

Some Useful List Functions

  • Take n elements from a list

        List.take 2 [1,2,3,4]
    
  • Remove n elements from a list

        List.drop 2 [1,2,3,4]
    
  • Challenge find the nth element of a list (as a singleton list)

Some Useful List Functions

  • Retrieve the first element of a list

        List.head [1,2,3,4]
    
  • Remove the first element from a list

        List.tail [1,2,3,4]
    
  • Challenge try retrieving the nth element of a list (as a single value not a list)
  • What does this return?

Dealing with Maybe types

  • Replace a Maybe value with a default value

        Maybe.withDefault 0 (List.head [])
    
  • Chain two functions together

        Maybe.andThen List.head (List.tail [1,2,3])
    

Function Piping

  • The pipe operator can be used as an alternative to brackets

         List.tail [1,2,3]
             |> Maybe.andThen List.head
    

Creating Your Own Functions

  • You can create and reuse your own functions

        index0 xs n = Maybe.withDefault 0 (List.head (List.drop n xs))
    

Syntax Rules

Syntax states the rules for forming valid expressions

Naming Requirements

  • Function and argument (variable) names must begin with a lower-case letter. For example:

          myFun     fun1     arg_2    x'
    
  • By convention, lists usually have an s suffix on their name. For example :

          xs    ns   nss
    

Local Variables - Let Expressions

  • Local variables in Elm are primarily for making your code more readable and code reuse.
  • Consider the following code

        poly x = (x+1) * (x+1)
    
  • To reuse the computation \((x+1)\) we can define a local variable

        poly x = let y = x+1
                 in y * y
    

Local Variables - Let

Please note, let and where expressions are EXPRESSIONS NOT STATEMENTS

Definition 1          Definition 2
f = let x = 5         f = let z = y + x
        y = x + 6             x  = 5
        z = y + x             y = x + 6
    in z                  in z
-- Definitions Are Equivalent