Functional Programming code snippets

Bar = add 5 5

Is a program which first reduces the expression add 5 5 to produce the value 10

Discussion

So FP is magic ? :P If it can turn add 5 6 into 10

Discussion

 Functional programming (often abbreviated FP) is the process of
building software by composing pure functions , avoiding shared
state, mutable data, and side-effects . Functional programming is
declarative rather than imperative , and application state flows through
pure functions. Contrast with object oriented programming, where
application state is usually shared and colocated with methods in
objects.

Discussion

But that is only if you are working with mutations, which are avoided/restricted in Rust. Let declarations (let x = 5;) are not expressions in Rust.

Discussion

alien program

Discussion

x => 0

Discussion

In JS terms: () => 3 + 2

Discussion

let lazyAdd = () => x => () => y => () => x + y

Discussion

E.g. error function

Discussion

In Haskell, there is a unit type () whose only value is ()

Discussion

Yeah, undefined part is bottom type

Discussion

undefined :: a

Discussion

Error function is of type error :: String -> a

Discussion

Suppose you have a function f of type Int -> Int
But somebody calls it with undefined. Should the program crash before computing the function or only when the function uses its argument?

Discussion

In rust we have a macro unimplemented!()

Discussion

The article provided example f ㅗ = ㅗ (not real bottom symbol tho)

Discussion

So in SML f ㅗ is always a but not in haskell, right?

Discussion

In Haskell it would be only if the function tries to use its argument

Discussion

In SML f would never terminate

Discussion

This implementation uses pattern matching, so it would be in both cases in Haskell too

Discussion

If & was implemented like this:
x & y = if x then y else x
then we could possibly not evaluate y

Discussion

Btw, in Haskell they named it &&

Discussion

I made a connection with Rust. In Rust we have a builtin "never type", named !. The function which exits the process is fn exit(status: i32) -> !

Discussion