Programming Comparisons: True Lambda


scheme
((lambda(n)
  ((lambda(fact)
    (fact fact n))
  (lambda (ft k)
    (if (= k 1) 1
        (* k (ft ft (- k 1))))))) 10)
ruby
lambda {|n|
  lambda {|fact|
    fact[fact, n] }[
  lambda {|ft, k|
    if k == 1 then 1
    else k * ft[ft, k-1] end }]}[10]
perl
sub {my($n)=@_;
  sub {my($fact)=@_;
    $fact->($fact, $n);}->(
  sub {my($ft, $k)=@_;
    if ($k == 1) { 1; }
    else { $k * $ft->($ft, $k - 1); }})}->(10);
ocaml
(fun n ->
  (fun fact -> fact (Obj.magic fact) n)
    (fun ft k ->
      if k = 1 then 1
      else k * (ft (Obj.magic ft) (k - 1)))) 10
lisp
(funcall #'(lambda(n)
   (funcall #'(lambda(fact) (funcall fact fact n))
      #'(lambda(ft k)
        (if (= k 1) 1
           (* k (funcall ft ft (- k 1))))))) 10)
python
(lambda n:
  (lambda fact: 
    fact(fact, n))(
      lambda ft, k: k == 1 and 1 or k * ft(ft, k-1) )
  )(10)
false
10 [
  [
    $1=$
    [ @% ]?
   ~[ $@$@1-@!* ]?
  ] [ $@\! ]!
]!
javascript
(n =>
  (fact =>
    fact(fact, n))(
      (ft, k) =>
        k == 1 ? 1
        : k * ft(ft, k-1)))(10)
smalltalk
[:n|
  [:fact|
    fact value: fact value: n]
  value: [:ft :k|
    (k = 1) ifTrue:  [ 1 ]
            ifFalse: [ k * (ft value: ft value: (k - 1)) ]]] value: 10
java
/*
class Lambda {
  public int intToInt(final int n)               { return 0; };
  public int objToInt(Lambda fact)               { return 0; };
  public int objIntToInt(Lambda ft, final int k) { return 0; };
}
*/
(new Lambda() { public int intToInt(final int n) {
  return ( new Lambda() { public int objToInt(Lambda fact) {
    return fact.objIntToInt(fact, n); }}).objToInt(
      new Lambda() { public int objIntToInt(Lambda ft, final int k) {
        if (k == 1) return 1;
        else return k * ft.objIntToInt(ft, k-1); }}); }}).intToInt(10);
haskell
-- newtype Mu a = Roll { unroll :: Mu a -> a }
(\n -> 
  (\fact -> fact (Roll fact) n)
  (\ft k -> if k == 1 then 1 else k*(unroll ft ft (k-1)))) 10
erlang
(fun(N) ->
  (fun(Fact) ->
    Fact(Fact, N) end)(
  fun(Ft, K) ->
    case K of 1 -> 1;
              _ -> K * Ft(Ft, K - 1) end end) end)(10)
clojure
((fn [n]
  ((fn [fact]
    (fact fact n))
  (fn [ft k]
    (if (= k 1) 1
        (* k (ft ft (- k 1))))))) 10)
coffeescript
((n) ->
  ((fact) ->
    fact fact, n)(
    (ft, k) ->
      if k == 1 then 1
      else k * ft ft, k-1))(10)