JavaScript
      Haskell

         2008-01-24
      UJIHISA, Tatsuhiro
who's this?
• ujihisa
• Osaka Univ. M2
• Fuzzy Rough Sets
• nayutaya -  http://www.nayutaya.co.jp


• LiveCoding, ldev, Ha...
from js to hs
Ruby       natural   class-based OO



                       prototype-based
JavaScript
                        OO, funct...
simplicity
• process serially
• assignment
• loop
• implicit cast
simplicity
• process serially
• assignment
• loop
• implicit cast
no variables
var a = 1;
var b = function() { return 1 };




a = 1
b = 1
no re-assignment
var a = 1;
var a = 2;




a = 1
a = 2
-- Multiple declarations of `Main.a'
var f = function() {
  var i = 0;
  return function() {
    return i++;
  }
};
var g = f();
g(); //=> 0
g(); //=> 1
g(); /...
principle
write less code
• principle: as possible as you
 should write less code
• development,
 maintenance,
 ...always.
notation
aaa(bbb(quot;hahahaquot;, 3)));




aaa (bbb quot;hahahaquot; 3)

aaa $ bbb quot;hahahaquot; 3
window.addEventListener(
  quot;loadquot;,
  function() { alert(quot;aquot;) })




main = putStrLn quot;aquot;
var f = function(a, b) {
   return a + b
};
f(8, 7) //=> 15




f a b = a + b
f 8 7 -- 15
var f = function(a) {
   var g = function(b) {...};
   return g(a);
};




f a = g a
  where g b = ...
var fib = function(n)    {
   return (n == 1) ? 1   :
          (n == 2) ? 1   :
            fib(n-2) +   fib(n-1);
};



...
(function(a) { return a + 1 })

(function(a) { return a + 1 })(3)
//=> 4




(a -> a + 1)

(a -> a + 1) 3 -- 4
a + b
(+) a b

add a b
a `add` b
List Processing

           Haskell
             vs
JavaScript with prototype.js
[1, 2, 3].map(function(i) {
  return i + 1;
}) //=> [2, 3, 4]




map (i -> i + 1) [1, 2, 3]
-- [2, 3, 4]
[1, 2, 3, 4, 5].first() //=> 1
[1, 2, 3, 4, 5][3]      //=> 4




head [1, 2, 3, 4, 5] -- 1
[1, 2, 3, 4, 5] !! 3 -- 4
$R(1, 10).toArray()
//=> [1, 2, ..., 10]

$R(1, Infinity).toArray()
//...




[1..10]

[1..]
head [1..]      -- 1
head tail [1..] -- 2
[1..] !! 10     -- 11
curry
var f = function(a) {
  return function(b) {
    return function(c) {
      return a + b + c }}};
f(3)(5)(7) //=> 15




f...
f a b = a + b
f a b = (+) a b
f = (+)

addOne   a   = 1 + a
addOne   a   = (+) 1 a
addOne   =   (+) 1
addOne   =   (1 +)

...
list comprehension
tmp = [];
[1, 2].each(function(x) {
  [10, 20].each(function(y) {
     tmp.push(x + y)
  })
});




[ x + y | x <- [1, 2],...
tmp = [];
 $R(1, Infinity).each(function(x) {
   if (x % 2 == 0 && x < 10) {
     tmp.push(x)
   }
 });




[ x | x <- [1....
fib again

                     h (+) fib (tail fib)
fib = 1 : 1 : zipWit
how to use
• sudo port install ghc
 • HotMac :-)
• runghc a.hs
• ghc a.hs && ./a.out
• ghci
for what?
• draft
• step up
• production
Monadius
HSP
Haskell
Scheme
 Prolog
•   lazy evaluation
•   monad
•   type inference
thank you!
From Javascript To Haskell
Upcoming SlideShare
Loading in …5
×

From Javascript To Haskell

8,836 views

Published on

Kanasan.JS - JS#2

Published in: Economy & Finance, Technology
0 Comments
12 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
8,836
On SlideShare
0
From Embeds
0
Number of Embeds
42
Actions
Shares
0
Downloads
81
Comments
0
Likes
12
Embeds 0
No embeds

No notes for slide

From Javascript To Haskell

  1. 1. JavaScript Haskell 2008-01-24 UJIHISA, Tatsuhiro
  2. 2. who's this? • ujihisa • Osaka Univ. M2 • Fuzzy Rough Sets • nayutaya - http://www.nayutaya.co.jp • LiveCoding, ldev, Haskell, On Lisp...
  3. 3. from js to hs
  4. 4. Ruby natural class-based OO prototype-based JavaScript OO, functional Haskell simple functional
  5. 5. simplicity • process serially • assignment • loop • implicit cast
  6. 6. simplicity • process serially • assignment • loop • implicit cast
  7. 7. no variables var a = 1; var b = function() { return 1 }; a = 1 b = 1
  8. 8. no re-assignment var a = 1; var a = 2; a = 1 a = 2 -- Multiple declarations of `Main.a'
  9. 9. var f = function() { var i = 0; return function() { return i++; } }; var g = f(); g(); //=> 0 g(); //=> 1 g(); //=> 2 g(); //=> 3 g(); //=> 4
  10. 10. principle
  11. 11. write less code • principle: as possible as you should write less code • development, maintenance, ...always.
  12. 12. notation
  13. 13. aaa(bbb(quot;hahahaquot;, 3))); aaa (bbb quot;hahahaquot; 3) aaa $ bbb quot;hahahaquot; 3
  14. 14. window.addEventListener( quot;loadquot;, function() { alert(quot;aquot;) }) main = putStrLn quot;aquot;
  15. 15. var f = function(a, b) { return a + b }; f(8, 7) //=> 15 f a b = a + b f 8 7 -- 15
  16. 16. var f = function(a) { var g = function(b) {...}; return g(a); }; f a = g a where g b = ...
  17. 17. var fib = function(n) { return (n == 1) ? 1 : (n == 2) ? 1 : fib(n-2) + fib(n-1); }; fib 1 = 1 fib 2 = 1 fib n = fib (n-2) + fib (n-1)
  18. 18. (function(a) { return a + 1 }) (function(a) { return a + 1 })(3) //=> 4 (a -> a + 1) (a -> a + 1) 3 -- 4
  19. 19. a + b (+) a b add a b a `add` b
  20. 20. List Processing Haskell vs JavaScript with prototype.js
  21. 21. [1, 2, 3].map(function(i) { return i + 1; }) //=> [2, 3, 4] map (i -> i + 1) [1, 2, 3] -- [2, 3, 4]
  22. 22. [1, 2, 3, 4, 5].first() //=> 1 [1, 2, 3, 4, 5][3] //=> 4 head [1, 2, 3, 4, 5] -- 1 [1, 2, 3, 4, 5] !! 3 -- 4
  23. 23. $R(1, 10).toArray() //=> [1, 2, ..., 10] $R(1, Infinity).toArray() //... [1..10] [1..]
  24. 24. head [1..] -- 1 head tail [1..] -- 2 [1..] !! 10 -- 11
  25. 25. curry
  26. 26. var f = function(a) { return function(b) { return function(c) { return a + b + c }}}; f(3)(5)(7) //=> 15 f a b c = a + b + c f a b = (c -> a + b + c) f a = (b -> (c -> a + b + c)) f = (a -> (b -> (c -> a + b + c))) f 3 5 7 -- 15
  27. 27. f a b = a + b f a b = (+) a b f = (+) addOne a = 1 + a addOne a = (+) 1 a addOne = (+) 1 addOne = (1 +) map (1 +) [1, 2, 3]
  28. 28. list comprehension
  29. 29. tmp = []; [1, 2].each(function(x) { [10, 20].each(function(y) { tmp.push(x + y) }) }); [ x + y | x <- [1, 2], y <- [10, 20]] {x + y|x ∈ X, y ∈ Y }
  30. 30. tmp = []; $R(1, Infinity).each(function(x) { if (x % 2 == 0 && x < 10) { tmp.push(x) } }); [ x | x <- [1..], x `mod` 2 == 0, x < 10] {x|x ∈ N, x ≡ 0 mod 2, x < 10}
  31. 31. fib again h (+) fib (tail fib) fib = 1 : 1 : zipWit
  32. 32. how to use • sudo port install ghc • HotMac :-) • runghc a.hs • ghc a.hs && ./a.out • ghci
  33. 33. for what? • draft • step up • production
  34. 34. Monadius
  35. 35. HSP
  36. 36. Haskell Scheme Prolog
  37. 37. • lazy evaluation • monad • type inference
  38. 38. thank you!

×