Parallel and Concurrent Haskell (1)

2016 

1-1: Introduction

Everything in Haskell is PURE (function), including side effects (print, I/O such as open files, update data, …)

“Pure”: f (x) = a, regardless of ‘x’ value may change, always returns the result ‘a’

https://youtu.be/N6sOMGYsvFA

2.1 Function

f a b = function f, arg a & b

sqDist (x, y ) = x^2 + y^2
main = print $ sqDist (3, 4)
dist pt  = sqrt  $ sqDist pt

dist = sqrt . sqDist

Note: pronounce “.” as “AFTER

id  :: a -> a [Signature] # Polymorphic
id  x = x [Implementation]

flop :: (a, b) -> (b, a)
flop p = = (snd p,fst p)

flop’ (x,y) = (y,x)

Example:
print $ flop (5, “Hello!”)

Currying 
sqDist :: Double ->Double -> Double
Equivalent to: sqDist 1 arg but return a function (Double -> Double)
sqDist :: Double -> (Double -> Double)

sqDist x y = x^2 + y^2

Equiv: sqDist (x,y) # but difficult to partial application.

Partial Application (pass fewer args)
sqDist 0 = Double -> Double

sqdistfromzero
:: Double -> Double
sqdistfromzero = sqDist 0

2.2 Functions

Function (+) ::  D -> D -> D
inc  x  = 1 + x ~ (+) ::  1 + x

Section (Partial appl) : inc  = (+ 1)

To be continued … (Part 2)

Parallel and Concurrent Haskell: http://www.youtube.com/playlist?list=PLbgaMIhjbmEm_51-HWv9BQUXcmHYtl4sw

<b>Read Free Online Book:</b>
http://chimera.labs.oreilly.com/books/1230000000929/index.html
<a href=”https://tomcircle.files.wordpress.com/2017/07/20170712_200456.png”><img src=”https://tomcircle.files.wordpress.com/2017/07/20170712_200456.png&#8221; alt=”” class=”wp-image-13955 alignnone size-full” width=”1064″ height=”1262″></a>

Advertisements

Higher Order Function

[Source] https://www.quora.com/What-are-the-mathematical-explanations-for-higher-order-functions-Functional-programming/answer/Bartosz-Milewski?share=d265e0ac&srid=ZyHj

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.