25. “It's not difficult at all,
proc {|x, y, z| x + y + z }.curry
returns the proc object equivalent to
proc {|x| proc {|y| proc {|z| x + y + z } } }
See?”
matz.
25
26. Why do this in Ruby?
plus_five =proc{|x,y,z|x+y+z }.curry.call(2).call(3)
plus_five[10] #=> 15
26
30. -module(patternmatch).
-export([run/0]).
run() ->
tupleassign(),
pmatch(),
invalid().
invalid() ->
Str1 = quot;have a nice dayquot;,
Str2 = quot;have another nice dayquot;,
Str1 = Str2. %% this is an error!
tupleassign() ->
MyTuple = {1,2,3},
{A,B,C} = MyTuple, %% this assigns values to unbound A, B and C
io:format(quot;A:~p B:~p C:~p ~nquot;,[A,B,C]).
pmatch() ->
Str1 = quot;la la laquot;,
case Str1 of
quot;fooquot; ->
io:format(quot;uh oh ~nquot;);
3 ->
io:format(quot;wrong type ~nquot;);
quot;la la laquot; ->
io:format(quot;We have a match ~nquot;);
_ ->
io:format(quot;like an else ~nquot;)
end.
30
31. Built in Assertions /
Single Assignment
invalid() ->
Str1 = quot;have a nice dayquot;,
Str2 = quot;have another nice dayquot;,
Str1 = Str2. %% this is an error!
31
32. Multiple Assignment
tupleassign() ->
%% create a tuple
MyTuple = {1,2,3},
%% assign values to unbound A, B and C
{A,B,C} = MyTuple,
%% print the result
io:format(quot;A:~p B:~p C:~p ~nquot;,[A,B,C]).
32
33. Switch Statement
Str1 = quot;la la laquot;,
case Str1 of
quot;fooquot; ->
io:format(quot;uh oh ~nquot;);
3 ->
io:format(quot;wrong type ~nquot;);
quot;la la laquot; ->
io:format(quot;We have a match ~nquot;);
_ ->
io:format(quot;like an else ~nquot;)
end.
33
48. PROGRAM: (2)
(in-ns 'main)
(clojure/refer 'clojure)
(defn main [args]
(def x (list 1 2 3))
(def y (conj x quot;helloquot; quot;therequot; quot;worldquot;))
(println quot;list is: [quot; (apply str (interpose quot;, quot; y)) quot;]quot;)
)
PRODUCES:
list is: [ world, there, hello, 1, 2, 3 ]
48
49. Erlang Lists (3)
append(List1, List2) -> List3
Types:
List1 = List2 = List3 = [term()]
Returns a new list List3 which is made from the elements
of List1 followed by the elements of List2.
49
54. Operation Type Signature
atomically STM a -> IO a
retry STM a
orElse STM a -> STM a -> STM a
newTVar a -> STM (TVar a)
readTVar TVar a -> STM a
writeTVar TVar a -> a -> STM ()
54
55. (4)
module Main where
<< ... imports omitted ... >>
main = do shared <- atomically $ newTVar 0
before <- atomRead shared
putStrLn $ quot;Before: quot; ++ show before
forkIO $ 25 `timesDo` (dispVar shared >> milliSleep 20)
forkIO $ 10 `timesDo` (appV ((+) 2) shared >> milliSleep 50)
forkIO $ 20 `timesDo` (appV pred shared >> milliSleep 25)
milliSleep 800
after <- atomRead shared
putStrLn $ quot;After: quot; ++ show after
where timesDo = replicateM_
milliSleep = threadDelay . (*) 1000
atomRead = atomically . readTVar
dispVar x = atomRead x >>= print
appV fn x = atomically $ readTVar x >>= writeTVar x . fn
55
91. No time for:
Polymorphism
Lists
Tuples
Guards
Accumulators
Continuations
Comprehensions
Fault Tolerance
Type Systems
Process Linking
Recursion (TCO)
Higher Order Functions
91
103. Books
Beautiful Code (Oram and Wilson)
Real World Haskell (O’Sullivan, Stewart and Goerzen)
Programming Erlang (Armstrong)
Foundations of F# (Pickering)
Expert F# (Syme, Granicz and Cisternino)
F# for Scientists (Harrop)
Programming in Haskell (Hutton)
Coming Soon
Programming Scala (Subramaniam)
Programming Clojure (Halloway)
103
104. The Web
http://www.erlang.org
http://www.haskell.org
http://www.clojure.org
http://www.trapexit.org
http://www.scala-lang.org
http://lambda-the-ultimate.org
http://pragmaticstudio.com/erlang
http://en.wikibooks.org/wiki/Haskell
http://book.realworldhaskell.org/read
http://www.lisperati.com/fringedc.html
http://research.microsoft.com/fsharp/fsharp.aspx
104
106. References
1) Example shamelessly stolen from page 42 of
“Programming Erlang” by Joe Armstrong.
2) Shamelessly stolen from Clojure doc
3) From Erlang stdlib documentation
4) Shamelessly stolen from
http://www.haskell.org/haskellwiki/Simple_STM_example
5) Example from “Beautiful Code” by Oram and Wilson
106