# Tag Archives: Functional programming

# What Makes Functional and Object-oriented Programming Equal

http://codinghelmet.com/articles/what-makes-functional-and-object-oriented-programming-equal

**Comparison of 4 Functional Programming (FP : F#, Lisp/Clojure Haskell, Scala,… ) concepts in Object-Oriented (OO: C#, C++, Java,… ) :**

[Not covered here]: There are other FP techniquesin OO:lackingFunctor(FoldMap),Monad, etc.

1. **Function:**

2. **Closure**** (variable binding) **

3. **Currying**

4. **Function** **Composition**

…

…**Conclusion**:

# A Programmer’s Regret: Neglecting Math at University – Adenoid Adventures

Advanced Programming needs Advanced Math: eg.

Video Game **Animation**: Verlet Integration

**AI**: Stats, Probability, Calculus, Linear Algebra

**Search Engine** : PageRank: Linear Algebra

**Abstraction** in Program “Polymorphism” : Monoid, Category, Functor, Monad

Program “**Proof**” : Propositions as Types, HoTT

https://awalterschulze.github.io/blog/post/neglecting-math-at-university/

Abstraction: Monoid, Category

Category

# Functional Programming : Type Theory

# Python in Functional Programming Style

Functional Programming has the following key styles:

1) **Lambda function:**

2) **Map, Filter, Reduce**

# A Functional Programmer’s Guide to Homotopy Type Theory (HoTT)

Since April 2019 until I re-visit this Youtube video on 12 August 2019, I can now totally understand his speech after a pause of 4 months by viewing other related Youtube (below prerequisite) videos on Category Theory, Type Theory, Homotopy Type Theory.

That is the technique of self-study:

- First go through the whole video,
- Don’t understand? view other related simpler videos.
- Repeat 1.

**Prerequisite** knowledge:

- Homotopy
- Type Theory
- Homotopy Type Theory
- Bijection = Isomorphism
- Functional Programming in Category Theory Concept: Monad & Applicative

**Two Key Takeaway Points:**

- In the Homotopy “Space” :
**Programs are points**in the space,**Paths are Types.** **“Univalence Axiom”**: Paths Induce Bijection, vice versa.

…

…

…

# Knowing Monads Through The Category Theory

https://dev.to/juaneto/knowing-monads-through-the-category-theory-1mea

While Mathematicians like to talk non-sensical abstract idea, Informaticians want to know how to apply the idea concretely:

**Mathematical Parlance**:

**Monad = ****Monoid**** +Endofunctor**

**Monoid = Identity + Associative**

**Endo-functor = functor between 2 ****same**** categories **

**IT Parlance:**

**Monad** is a ‘function’ to **wrap the ‘side effects’** (exception errors, I/O,… ) so that function composition in ‘**pipeline**‘ chained operation sequence is still possible in **pure** **FP** (Functional Programming, which forbids side-effects).

Some common Monads: ‘Maybe’, ‘List’, ‘Reader’…

This allows monads to simplify a wide range of problems, like handling potential undefined values (with the `Maybe`

monad), or keeping values within a flexible, well-formed list (using the `List`

monad). With a monad, a programmer can turn a complicated sequence of functions into a succinct pipeline that abstracts away additional data management, control flow, or side-effects.^{[2][3]}

__Exploring Monads in Scala Collections__

# The Evolution of Software

# Monads – FP’s answer to Immutability

**Introduction**:

- The curse of Immutability in Functional Programming – no “Looping” (recursion ok), no Date, no Random, …no I/O …
- Monad is the Savior of “No Side Effect: IO Monads

**Promise of Monads (A)**

**Promise of Monads (B)**

# Alejandro Serrano: Category Theory Through Functional Programming

(**Part 1/3) – λC 2017**

What is Category ?

Objects

Morphism (Arrows )

Rule1: Associative

Rule 2: Identity

A <– C –> B

Product of Categories : A x B

Unique

Sum of Categories: A + B

Unique

(Either a b)

**Co-Product**

Reverse all arrows.

Unique

**Functor F: C-> D**

Mapping of all objects (A, B) in categories C,D

Mapping of arrows f

f : A -> B

Ff : FA -> FB (preservation)

F Id = Id

F (f。g) = Ff。Fg

Example:

Constant C -> F

FC = k

Ff = Id

**Arrow Functor F: C -> D**

For any object A in C,

F A = D -> A

(Functional Type is also Type)

Functors compose !

**Category of categories:**

Objects: categories

Arrows : Functors

Haskell Category (Hask) is always Endo-Functor, ie Category Hask to itself.

Mapping of arrows.

Mapping of Objects = predefined

(Part 2/3) – λC 2017

(part 3/3) – λC 2017

# Mathematical Functions vs Programming Functions

# 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.

# You should learn Functional Programming in 2017

WhatsApp is written in Erlang – a Functional Programming Language. It supports 900 million users worldwide with only 50 programmers.

# 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

# BM Category Theory : Motivation and Philosophy

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

- Hidden Mutating States;
- Data Sharing.

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

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

What is a Category ?

1) Abstraction:

ObjectsMorphism (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:

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)

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 -> ()

unit :: a -> ()

[to be continued 3.1 ….]

# Recommended Reading for Functional Programming

Functional Programming, especially Haskell, requires the Math foundation in Abstract Algebra, and Category.

http://reinh.com/notes/posts/2014-07-25-recommended-reading-material.html