2009/08/10

[KPF]第4回勉強会をやってきた

取り急ぎ。

「SchemeとJavaScriptで数字遊び」と題してライブコーディングをやるつもりでしたが、途中でテンパってしまって自ら強制終了というなんともヒドイ醜態をさらしてきましたorz

ライブコーディングも中途半端だったので取り急ぎコードをさらします。

 

download

 

 

var inc = function (n)
{
  return ++n;
};

var dec = function (n)
{
  return n - 1;
};

var add = function (n, m)
{
  return m == 0
    ? n
    : inc(add(n, dec(m)));
};

var sub = function (n, m)
{
  return m == 0
    ? n
    : dec(sub(n, dec(m)));
};

var bgr = function (n, m)
{
return m == 0
    ? true
    : (n == 0
       ? false
       : bgr(dec(n), dec(m)));
};

var sml = function (n, m)
{
  return m == 0
    ? false
    : (n == 0
       ? true
       : sml(dec(n), dec(m)));
};

var mul = function (n, m)
{
  return m == 0
    ? m
    : add(n, mul(n, dec(m)));
};

var div = function (n, m)
{
  return sml(n, m)
    ? 0
    : inc(div(sub(n, m), m));
};

function fact (n)
{
  return n == 0
    ? 1
    : n * fact(--n);
}

function tailCallFact(n)
{
  var recur = function (m, ret)
  {
    return m == 0
      ? ret
      : recur(m - 1, m * ret);
  };
  return recur(n, 1);
}

function tailCallFact(n)
{
  var recur = function (m, ret)
  {
    return [ret, m == 0
      ? ret
            : recur(m - 1, m * ret)];
  };
  return recur(n, 1);
}

var i = 10;
do { print ("hello"); i--; }while(0 < i)

(function () { var i = 10; do{ print("hello"); i--;}while(0 < i);})();

Array.prototype.each = function (f)
{
  for(var i = 0; i<this.length; i++)
  {
    f(this[i]);
  }
};

(function (f){
   f(f, 10);
})(function (f, n){
      if (0 < n)
      {
        print ("hello");
        f(f, n - 1);
      }
    });

(function (f){
   f(f, 10);
})(function (f, n){
      if (0 < n)
      {
        print("hello");
        f(f, n - 1);
      }
    });

(function (f){
   f(f, f, 10);
})(function (f, n){
      if (0 < n)
      {
        print("hello");
        f(f, n - 1);
      }
    });

function map (f, array)
{
  for (var i = 0; i < array.length; i++)
  {
    array[i] = f(array[i]);
  }
  return array;
}

map(function (v)
    {
      print(v);
      return v;
    },
    [1, 2, 3, 4, 5]);

Array.prototype.map = function (f)
{
  for (var i = 0; i < this.length; i++)
  {
    this[i] = f(this[i]);
  }
  return this;
};

[1,2,3,4,5].map(function(v){print(v);return v;});

 

(define inc
  (lambda (n)
    (+ n 1)))

(define dec
  (lambda (n)
    (- n 1)))

(define add
  (lambda (n m)
    (if (zero? m)
        n
        (inc (add n (dec m))))))

(define sub
  (lambda (n m)
    (if (zero? m)
        n
        (dec (sub n (dec m))))))

(define bgr
  (lambda (n m)
    (cond
     ((zero? n) #f)
     ((zero? m) #t)
     (else (bgr (dec n)(dec m))))))

(define sml
  (lambda (n m)
    (cond
     ((zero? m) #f)
     ((zero? n) #t)
     (else (sml (dec n)(dec m))))))

(define mul
  (lambda (n m)
    (if (zero? m)
        0
        (add n (mul n (dec m))))))

(define div
  (lambda (n m)
    (if (sml n m)
        0
        (inc (div (sub n m) m)))))

(define fact
  (lambda (n)
    (if (zero? n)
        1
        (* n (fact (- n 1))))))

(define tail-call-fact
  (lambda (n)
    (letrec ((iter (lambda (n ret)
                     (cons
                      ret
                      (if (zero? n)
                          '()
                          (iter (- n 1)
                                (* n ret)))))))
      (iter n 1))))

(do ((i 0 (+ i 1)))
    ((= i 10))
  (print "hello"))

(let loop ((s 0))
  (if (< s 10)
      (begin
        (print "hello")
        (loop (+ s 1)))))

(for-each
(lambda (i)
   (print "hello"))
'(1 2 3 4 5))

((lambda (f)
   (f f 10))
(lambda (f n)
   (if (< 0 n)
       (begin
         (print "hello")
         (f f (- n 1))))))

((lambda (f)
   (f f 10))
(lambda (f n)
   (if (< 0 n)
       (begin
         (print "hello")
         (f f (- n 1))))))

(define fact
  (lambda (n)
    (if (zero? n)
        1
        (* n (fact (- n 1))))))

(define tail-call-fact
  (lambda (n)
    (letrec ((iter (lambda (x ret)
                     (if (zero? x)
                         ret
                         (iter (- x 1)(* x ret))))))
      (iter n 1))))

(define e '(define (fact n)
             (if (zero? n)
                 1
                 (* n (fact (- n 1))))))

(car e)
(caadr e)
(cdadr e)
(caddr e)

(use srfi-1)
(map
(lambda (n)
   (cond
    ((zero? (modulo n 3)) "fizz")
    ((zero? (modulo n 5)) "buzz")
    ((zero? (modulo n 15)) "fizzbuzz")
    (else n)))
(iota 100 1))

0 件のコメント:

コメントを投稿