# SF Scala: Martin Odersky, Scala — the Simple Parts

Scala is a non-pure FP (Functional Programming) language with the Object-oriented feature, unlike the pure FP such as Haskell or Clojure (Lisp). It is popular in Android platform as an alternative to Java.

# Functional Programming for the Object Oriented – Øystein Kolsrud

• Imperative
• Object- Oriented
• Functional Programming

Part 2: Example – The 8 Queens Problem

Note: A simpler Haskell coding here.

Morphism: 态射 (Mapping : 映射)

Functor (F): 函子 $F :: C \to D$

Covariant: 协变 $C \to D$

Contravariant: 逆变 $C^{Op} \to D$

Type: 类型

Brian Beckman:

You can understand Monad without too much Category Theory.

Functional Programming = using functions to compose from small functions to very complex software (eg. Nuclear system, driverless car software…).

• Strong Types Safety: detect bugs at compile time.
• Data Protection thru Immutability: Share data safely in Concurrent / Parallel processing.
• Software ‘Componentisation’  ie Modularity : Each function always returns the same result, ease of software reliability testing.

Each “small” function is a Monoid.
f : a -> a (from input of type ‘a‘ , returns type ‘a’)
g: a -> a

compose h from f & g : (strong TYPING !!)
h = f。g : a -> a

[Note] : Object in Category, usually called  Type in Haskell, eg. ‘a’ = Integer)

You already know a Monoid (or Category in general) : eg Clock

1. Objects: 1 2 3 …12 (hours)
2. Arrow (Morphism): rule “+”:
• 7 + 10 = 17 mod 12 = 5
3. Law of Associativity:
x + (y + z) = (x + y) + z
4. Identity (or “Unit”):  (“12”):
x + 12 = 12 + x = x

More general than Monoid is a “Monoidal” Category where: (instead of only single object ‘a’, now more “a b c…”)
f : a -> b
g: b -> c
h = f。g : a -> c

Function under composition Associative rule and with an Identity => Monoid

Monad (M): a  way to manage  the side-effects (I/O, exception , SQL Database, etc) within the Functional Programming way like monoidal categories: ie associative composition, identity.

Remark: For the last 60 years in Software, there have been 2 camps:

1. Bottom-Up Design: from hardware foundation,  build performance-based languages: Fortran, C, C++, C#, Java…
2. Top-Down Design: from Mathematics foundation, build functional languages (Lambda-Calculus, Lisp, Algo, Smalltalk, Haskell…).
3. F# (Microsoft) is the middle-ground between 1 & 2.

Ref: What is a Monad ?

Monad = chaining operations with binding “>>=”

• Possible use: allows to write mini-language, parser…

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