# Functional Programming with Kotlin

3) Higher–Order Function, Closure

# Higher Order Function

As Tikhon Jelvis explained in his response, functions map sets to sets, and functions themselves form sets. This is the essence of the untyped lambda calculus. Unfortunately, untyped lambda calculus suffers from the Kleene–Rosser paradox (later simplified to Curry’s paradox).

This paradox can be removed by introducing types, as in the typed lambda calculus. Simple types are equivalent to sets, but  in order to pass a function as an argument to another function (or return one), we have to give this function a type. To really understand what a function type is, you need to look into category theory.

The categorical model for the typed lambda calculus is a category in which objects are types and morphism are functions. So if you want to have higher order functions, you have to be able to represent morphisms as objects — in other words, create a type for functions. This is possible only if the category is cartesian closed. In such a category you can define product types and exponential types. The latter correspond to function types.

So that’s a mathematical explanation for higher order.

# Functional Programming for the Object Oriented – Øystein Kolsrud

Part 1: Compare 3 paradigms:

• Imperative
• Object- Oriented
• Functional Programming

Introduction to Haskell

Part 2: Example – The 8 Queens Problem

Note: A simpler Haskell coding here.

# Programming and Math

Category Theory (CT) is like Design Pattern, only difference is CT is a better mathematical pattern which you can prove, also it has no “SIDE-EFFECT” and with strong Typing.

The examples use Haskell to explain the basic category theory : product, sum, isomorphism, fusion, cancellation, functor…

# BM Category Theory : Motivation and Philosophy

Object-Oriented  has 2 weaknesses for Concurrency and Parallel programming :

1. Hidden Mutating States;
2. Data Sharing.

Category Theory (CT): a higher abstraction of all different Math structures : Set , Logic, Computing math, Algebra… =>

$\boxed {\text {CT reveals the way how our brain works by analysing, reasoning about structures !}}$

Our brain works by:  1) Abstraction 2) Composition 3) Identity (to identify)

What is a Category ?
1) Abstraction:

•  Objects
• Morphism (Arrow)

2) Composition: Associative
3) Identity

Notes:

• Small  Category with “Set” as object.
• Large Category without Set as object.
• Morphism is a Set : “Hom” Set.

Example in Programming
:

• Object : Types Set
• Morphism : Function “Sin” converts degree to R: $\sin \frac {\pi}{2} = 1$

Note: We just look at the Category “Types Set” from external Macroview, “forget ” what it contains, we only know the “composition” (Arrows) between the Category “Type Set”, also “forget” what these Arrows (sin,cosin, tgt, etc) actually are, we only study these arrows’ behavior (Associativity).

2.1 : Function of Set, Morphism of Category

Set: A function is

• Surjective (greek: epic / epimorphism 满射),
• Injective (greek : monic / monomorphism 单射)

Category:  [Surjective]

g 。f = h 。f
=> g = h (Right Cancellation )

2.2 Monomorphism

f 。g = f 。h
=> g = h
(Left cancellation)

$\boxed { \text {Epimorphism + Monomorphism =? Isomorphism }}$

NOT Necessary !! Reason ( click here):

In Haskell, 2 foundation Types: Void, Unit

Void = False
Unit ( ) = True

Functions : absurd, unit
absurd :: Void -> a (a = anything)
unit :: a -> ()

[to be continued 3.1 ….]