freizl / dive-into-haskell

Dive into Haskell
11 stars 0 forks source link

Haskell Introduction - a taste of Haskell #4

Closed freizl closed 10 years ago

freizl commented 11 years ago

% A Taste Functional Programming with Haskell % Haisheng, Wu % App, 2014

AGENDA

In computer science, functional programming is a programming paradigm that treats computation as the evaluation of mathematical functions and avoids state and mutable data.

Functional Programming (Cont.)

  • Treats computation as a series of mathematical functions (functional style)
  • Subset of compositional programming (pipe in Unix?)
  • Avoids states and mutable data.
  • Immutable
  • Purity
  • Functions a first class
  • functions can be a argument (HOF)
  • functions can be the return value
  • Abstraction
  • Mathematics model (when solving problems)
  • Currying
  • Recursive / Tail Call Optimisation

    Expressions v.s Statements

Summing the integers 1 to 10 in Java

int total = 0;
for (i = 1; i <= 10; ++i) {
   total = total+i;
}
sum [1..10]

Haskell Features

Haskell in one slide

Haskell in one slide

by SPJ


Purely functional

f(x) = x^2 + 1

High order functions

map :: (a->b) -> [a] -> [b]
foldl :: (a -> b -> a) -> a -> [b] -> a

multiTwo x = x * 2
cal xs = map multiTwo xs

Currying

-- foldl :: (a -> b -> a) -> a -> [b] -> a

add x y = x + y

f = foldl add     -- f :: b -> [b] -> b
g = foldl add 0   -- g :: [b] -> b

sum = foldl add 0

bind function in JavaScript


Function composition


(.) :: (b -> c) -> (a -> b) -> a -> c
f x = x + 2
g x = x * x
h = f . g

Statically typed


Pattern matching

fac :: Int -> Int
fac 0 = 1
fac n = n * fac (n-1)

Practical example

Trivial Abstraction example

function reverse (aList) {
  var result = [];
  for (item in aList) {
    result = result.unshift(item);
  }
  return result;
}

function reduceL (fn, aList, init) {
  if (aList.length > 0) {
     return reduceL(fn, aList.slice(1), fn(init, aList[0]));
  } else {
     return init;
  }
}
function _pushR (xs, y) {
   xs.unshift(y);
   return xs;
}

function reverse (xs) { 
   return reduceL(_pushR, xs, []);
}
function multi(xs) {
    return reduceL(function (x,y) { return x*y; }, xs, 1);
}

notes: code written in JavaScript


The ancient RC process

I would like to make change to search-ecomm_13_12.js and I need to

  1. Figure out all TML files that refer to search-ecomm_13_12.js
  2. P4 edit those TML files
  3. Update the reference path
    • from .../resources/mojito/.../search-ecomm_13_12.js
    • to .../RC/mojito/.../search-RC.js
  4. Copy
    • from .../resources/mojito/.../search-ecomm_13_12.js
    • to .../RC/mojito/.../search-RC.js
  5. p4 add it
  6. Make change to the search-RC.js

Can a tool help me out like:

a-simple-tool --resource search-ecomm_13_12.js --branch pb_intl_eu

The ancient RC process - Types

Translate the scenario straightforwardly

findTMLs   :: Directory -> IO [TML]
grepTMLs   :: ResourceName -> [TML] -> IO [TML]
updateTMLs :: ResourceName -> [TML] -> IO [TML]
createRC   :: Directory -> ResourceName -> IO ()

runP4 :: CMD -> IO ()

The ancient RC process - Show me the code

module Main where
main :: IO ()
main = print "Hello, Haskell"
  1. run it
runghc hello-world.hs

Resources

Haskellers say "If your code compiles, you're 99% done."
No Silver bullet!
freizl commented 11 years ago

A few first review comments:

freizl commented 10 years ago

Yet another slide - introduction to Haskell in google docs

Laziness is a virtue: an introduction to functional programming in Haskell

freizl commented 10 years ago

What is a functional language by Robert Harper

freizl commented 10 years ago

函数式编程扫盲篇 函数式思维: 为什么函数式编程越来越受关注 函数式编程

freizl commented 10 years ago

Some words