Skip to content
Snippets Groups Projects
Select Git revision
  • 03cf90d7313640e53bc778f378c54e113c5605f0
  • main default protected
2 results

04-first-functions.md

Blame
  • Code owners
    Assign users and groups as approvers for specific file changes. Learn more.

    First Functions

    Let's take a look at some predefined basic functions, how to use them and learn how we can define our first own functions.

    Defining Values

    x = 42 defines the variable x to hold the value 42.

    Any variable can only be defined once1!

    We can also use truth values: True and False (capitalized!)

    Basic Calculation

    Basic calculations work as expected

    • 1 * 2
    • 2 / 2
    • 3 + 1
    • 1 - 3

    3 + 1 * 20 vs. (3 + 1) * 20

    Truth values

    • True && False
    • True || False
    • not False

    Comparisons

    Comparisons also work as expected.

    • 5 == 5
    • 3 > 2
    • 1 >= 10
    • 2 /= 2 (note that not !=)

    Define our own

    Let's define some functions of our own.

    double x = x * 2
    
    six = double 3
    
    isThree x = x == 3
    
    isOneMore x y = x - 1 == y
    
    result = isOneMore 3 6
    1. Except in GHCi where defining a value again overwrites previous definitions for convenience