Mar 31, 2008

ERB

I'm making a CGI program with Ruby.
I use ERB.
It's possible to write PHP-like markup.
If you use Ruby on Rails, ERB is used in views.

Mar 30, 2008

First programming in a long time.

I'm making a CGI program for posting messages.
I use Ruby.
But I don't use Ruby on Rails because of its bad performance.

Mar 29, 2008

What is the first program?

What is the first program for beginners?

"Hello, world!"?
Is it fun?

If they use C# or VB.NET, Windows application is better.
If they use Ruby (Ruby on Rails), Web application is better.

Mar 28, 2008

No excuse! No excuse!

No excuse! No excuse!

Everything is brought about by me.

No excuse! No excuse!

Mar 22, 2008

SICP Exercise 1.22

A procedure that checks a prime number.

(define (prime? n)
(= n (smallest-divisor n)))
(define (smallest-divisor n)
(find-divisor n 2))
(define (find-divisor n test-divisor)
(cond ((> (square test-divisor) n) n)
((divides? test-divisor n) test-divisor)
(else (find-divisor n (+ test-divisor 1)))))
(define (divides? a b)
(= (remainder b a) 0))
(define (square n)
(* n n))


A procedure that measures the time for "prime?".
I rewrite a bit.

(define (timed-prime-test n)
(newline)
(display n)
(start-prime-test n (runtime)))
(define (start-prime-test n start-time)
(if (prime? n)
(report-prime (- (runtime) start-time))
#f))
(define (report-prime elapsed-time)
(display " *** ")
(display elapsed-time)
#t)


DrScheme doesn't have "runtime".

(define (runtime)
(current-milliseconds))


My answer.

(define (search-for-primes min max cnt)
(cond ((> min max))
((= cnt 3))
((even? min) (search-for-primes (+ min 1) max cnt))
(else
(if (timed-prime-test min)
(search-for-primes (+ min 2) max (+ cnt 1))
(search-for-primes (+ min 2) max cnt)))))



(search-for-primes 1000 9999 0)
(search-for-primes 10000 99999 0)
(search-for-primes 100000 999999 0)
(search-for-primes 1000000 9999999 0)


It's impossible to measure the time.

Mar 20, 2008

SICP Exercise 1.21


(define (smallest-divisor n)
(find-divisor n 2))
(define (find-divisor n test-divisor)
(cond ((> (square test-divisor) n) n)
((divides? test-divisor n) test-divisor)
(else (find-divisor n (+ test-divisor 1)))))
(define (divides? a b)
(= (remainder b a) 0))
(define (square n)
(* n n))



(smallest-divisor 199)
(smallest-divisor 1999)
(smallest-divisor 19999)



199
1999
7

Mar 19, 2008

SICP Exercise 1.17 - 1.18

A multiplication procedure.

(define (* a b)
(if (= b 0)
0
(+ a (* a (- b 1)))))


Exercise 1.17.
Rewrite to a code that uses a logarithmic number of steps.
(Use "double" and "halve".)

(define (* a b)
(cond ((= b 0) 0)
((even? b) (* (double a) (halve b)))
(else (+ a (* a (- b 1))))))

(define (double n)
(+ n n))

(define (halve n)
(/ n 2))


Exercise 1.18.
Rewrite to a code that generates an iterative process.

(define (* a b)
(*-iter a b 0))

(define (*-iter a b n)
(cond ((= b 0) n)
((even? b) (*-iter a (halve b) (double n)))
(else (*-iter a (- b 1) (+ n a)))))

(define (double n)
(+ n n))

(define (halve n)
(/ n 2))

Mar 18, 2008

There will be an answer.

Japanese Programmer's: From recursion to iteration.

I found an answer.

(define (fast-expt2 b n)
(fast-expt-iter 1 b n))

(define (fast-expt-iter a b n)
(cond ((= n 0) a)
((even? n) (fast-expt-iter a (square b) (/ n 2)))
(else (fast-expt-iter (* a b) b (- n 1)))))

Mar 17, 2008

From recursion to iteration.

Exercise 1.16 of 1.2.4 Exponentation in SICP.

Recursion.

(define (fast-expt b n)
(cond ((= n 0) 1)
((even? n) (square (fast-expt b (/ n 2))))
(else (* b (fast-expt b (- n 1))))))

(define (even? n)
(= (remainder n 2) 0))

(define (square n)
(* n n))


How do I rewrite this code to iterative process?

Mar 16, 2008

How to trace in DrScheme.

1. Choose language
[Choose Language] - [PLT] - [Textual (MzScheme, includes R5RS)]

2. Incrude a library

(require (lib "trace.ss"))


3. Trace

(trace fib)
(fib 10)

Mar 15, 2008

CPS : Continuation Passing Style

I write a code using recursion.

static int Fact(int n)
{
if (n == 0)
{
return 1;
}
else
{
return n * Fact(n - 1);
}
}


I write a code using CPS.

static int FactCPS(int n, Func cont)
{
if (n == 0)
{
return cont(1);
}
else
{
return FactCPS(n - 1, a => cont(n * a));
}
}

static int Fact(int n)
{
return FactCPS(n, a => a);
}

Mar 14, 2008

SICP Exercise 1.10.

Ackermann's function

(define (A x y)
(cond ((= y 0) 0)
((= x 0) (* 2 y))
((= y 1) 2)
(else (A (- x 1)
(A x (- y 1))))))



> (A 1 10)
1024
> (A 2 4)
65536
> (A 3 3)
65536

Mar 13, 2008

SICP Exercise 1.9.

A.

(define (+ a b)
(if (= a 0)
b
(inc (+ (dec a) b))))


B.

(define (+ a b)
(if (= a 0)
b
(+ (dec a) (inc b))))


A.

(+ 4 5)
(inc (+ (dec 4) 5))
(inc (inc (+ (dec 3) 5))))
(inc (inc (inc (+ (dec 2) 5))))
(inc (inc (inc (inc (+ (dec 1) 5)))))
(inc (inc (inc (inc 5))))
(inc (inc (inc 6)))
(inc (inc 7))
(inc 8)
9

This is a recursive process.

B.

(+ 4 5)
(+ (dec 4) (inc 5))
(+ (dec 3) (inc 6))
(+ (dec 2) (inc 7))
(+ (dec 1) (inc 8))
9

This is a iterative process.

Mar 12, 2008

Linear Recursion and Iteration Sample.

A linear recursion code.

(define (factorial n)
(if (= n 1)
1
(* n (factorial (- n 1)))))


A linear iteration code.

(define (factorial n)
(fact-iter 1 1 n))

(define (fact-iter product counter max-count)
(if (> counter max-count)
product
(fact-iter (* counter product)
(+ counter 1)
max-count)))

Mar 4, 2008

Micor thread with yield of C#.

Micro thread is called co-routine or fiber.
It is possible to implement micro thread by using yield.


class Program
{
static void Main(string[] args)
{
var microThread1 = MicroThread1();
var microThread2 = MicroThread2();
var microThread3 = MicroThread3();

Console.WriteLine("Begin");

microThread1.MoveNext();
microThread2.MoveNext();
microThread3.MoveNext();
microThread1.MoveNext();
microThread2.MoveNext();
microThread3.MoveNext();

Console.WriteLine("End");

Console.ReadLine();
}

static IEnumerator<object> MicroThread1()
{
Console.WriteLine("Mocro Thread 1-1");
yield return null;
Console.WriteLine("Mocro Thread 1-2");
yield return null;
Console.WriteLine("Mocro Thread 1-3");
yield return null;
}

static IEnumerator<object> MicroThread2()
{
Console.WriteLine("Mocro Thread 2-1");
yield return null;
Console.WriteLine("Mocro Thread 2-2");
yield return null;
Console.WriteLine("Mocro Thread 2-3");
yield return null;
}

static IEnumerator<object> MicroThread3()
{
Console.WriteLine("Mocro Thread 3-1");
yield return null;
Console.WriteLine("Mocro Thread 3-2");
yield return null;
Console.WriteLine("Mocro Thread 3-3");
yield return null;
}
}

Mar 3, 2008

I retire from my job.

I've finished the last work at office.
I start new life from tomorrow.

Mar 2, 2008

Programming School.

I want to make up a programming school.
Which programming language should I teach?

Mar 1, 2008

FizzBuzz Object (JavaScript)


<body>
<script type="text/javascript; version=1.7">
var fizzbuzz = {
case : [
{
cond : function(n) { return n % 15 == 0; },
proc : function(n) { return "FizzBuzz"; }
},
{
cond : function(n) { return n % 3 == 0; },
proc : function(n) { return "Fizz"; }
},
{
cond : function(n) { return n % 5 == 0; },
proc : function(n) { return "Buzz"; }
},
{
cond : function(n) { return true; },
proc : function(n) { return n + ""; }
}
],
convert : function(n) {
for (var i = 0; i < this.case.length; i++) {
if (this.case[i].cond(n)) {
return this.case[i].proc(n);
}
}
},
get : function(start, count) {
for (var i = start; i < start + count; i++) {
yield this.convert(i);
}
}
}

var ret = "";

for (var x in fizzbuzz.get(1, 100)) {
ret += x + " ";
}

document.body.innerHTML = ret;
</script>
</body>



  • It's possible to access fizzbuzz (this) while initializing of fizzbuzz.

  • It's possible to use yield keyword in anonymous function.