Intro

Motivation: “lambda calculus” → any computable problem can be solved by composing one or more “pure” functions.

Every functions must take an argument and every function must return a value. Functions are considered “pure” and have no side effects.(i.e. no variables would be changed ) so functions with the same input would always produce the same output.

All variables in functional programming language are immutable, and functions can be stored in variables and passed as arguments.

<aside> 💡 Pure Function Definition A function that has two properties:

  1. given a specific input x, the function always returns the same output y
  2. It doesn’t modify any data beyond initializing local variables required to compute its output

</aside>

Compare to imperative programming, multi-threading is easy and the order of execution is less important.

Since functions have no side effects, lazy evaluation is used — a variable is only calculated when it need to be used

Haskell

square x = x*x

hypot a b = sqrt(square a + square b)

First rule first, Haskell uses indentation like python. It is a statically typed language, and it uses type inference to figure out the type we are writing.

Primitive Data Types

And if we don’t want to use the type inference and do explicit types

nerds = 150::Int

:t nerd  --gives out the type

nerds / 10 -- ERROR. Division does not support integers
nerds `div` 10 -- this would work

nerds + (-1) -- Unary operator needs to be enclosed by a bracket

Composite Data Types