Tuesday, January 03, 2012

Vocabulary trainer in Clojure

~/src/gh/voc/train.clj.html
(use '[clojure.string :only (split split-lines)])

; file is tab-seperated pairs of foreign and native words

(let [contents (slurp "100a.txt" :encoding "utf-8")
      lines (split-lines contents)
      pairs (map #(split % #"\t") lines)]

  (loop [my-pairs (shuffle pairs)]
    (let [pair (first my-pairs)]
      (do
        (println)
        (println (first (first my-pairs)))
        (if (not= (read-line) (second pair))
          (do
            (println "wrong, correct is:" (second pair))
            (recur (shuffle my-pairs)))
          (do
            (println "correct")
            (if (seq (rest my-pairs)) ; (seq idiomatic for (not (empty?
              (recur (rest my-pairs)))))))))

Monday, January 02, 2012

Monty Hall problem in Clojure

~/src/gh/goat.clj.html
; Monty Hall problem in Clojure

(defn pick-first-guess [doors]
  (first doors)
)

(defn change-guess [doors]
  (if (= :goat (second doors)) (last doors)
    (second doors))
)

(def samples (map shuffle (repeat 1000 [:goat :goat :car])))

(count (filter #(= % :car) (map pick-first-guess samples)))
(count (filter #(= % :car) (map change-guess samples)))

Tuesday, August 31, 2010

Thursday, March 25, 2010

Youtube down

I've seen that for the first time in years.

Thursday, March 18, 2010

Experimenting with Scala

This is my first useful Scala program. It was ported from a Python program (see below). In 1993 I wrote an equivalent in Modula-2 ;-)

import scala.io.Source;
import java.io.File;
import scala.util.Random;

object voc {
  def train(lst : List[Tuple2[String, String]]) : Unit = {
    println();
    if (lst.length == 0) {
      println(":-D");
      return;
    }

    val shuffled = Random.shuffle(lst);
    val current = shuffled.head;
    println(">>> " + current._1);
    var line = Console.readLine();
    if (line == current._2) {
      println("correct");
      train(shuffled.tail);
    } else {
      print("\007");
      println("wrong - correct is: " + current._2);
      train(shuffled);
    }
  }

  def main(args: Array[String]) {
    val lst = Source.fromFile(new File(args(0))).getLines()
          .map { s => s.split("\t") }
          .filter( l => l.length == 2 )
          .map (l => Tuple2(l.head, l.last))
          .toList;

    train(lst);
  }
}


Python version:

from __future__ import with_statement
import random
import sys

def train(lst):
    while len(lst) > 0:
        random.shuffle(lst)

        lang1, lang2 = lst[0]

        print ">>>", lang1
        try:
            answer = raw_input("answer: ").strip()
        except EOFError:
            # ^Z pressed, ignore
            continue

        if answer == lang2:
            print "correct"
            lst = lst[1:]
        else:
            print chr(7)
            print "wrong - correct is =>", lang2

        print
        print

    print ":-D"

def main():
    filename = sys.argv[1]

    lst = []
    for line in open(filename):
        line = line.strip()
        if not line:
            continue
        parts = line.split("\t")
        assert len(parts) == 2
        lst.append(parts)

    train(lst)


if __name__ == "__main__":
    main()

Sunday, March 14, 2010

PHP, the forgotten programming language

I just took a quiz and had a complete blackout of PHP. Can you name the 25 most popular programming languages? Quiz here.

Wednesday, February 17, 2010

Olympic Winter Games

How little do you care?

 
Vote at SPON.

Monday, January 18, 2010

Thursday, November 05, 2009

Interesting read about unit tests

It's OK Not to Write Unit Tests is an interesting blog post. Recommended to make you think again why you write them, if what you're writing are really unit tests, etc.

Monday, October 05, 2009

Unverständnis bei Bosbach von CDU

"Ich verstehe die Kritik der Liberalen an den auf Kinderpornografie beschränkten Netzsperren nicht." Es gebe kein Recht auf ungehinderten Zugriff auf Kinderpornografie im Internet, betonte Bosbach.

Netzwelt-Ticker: Google Wave strandet an der Hypeküste - SPIEGEL ONLINE - Nachrichten - Netzwelt


Liberale wollen im Internet weiter freien Zugriff auf KiPo haben, während die CDU das Netz für uns sicher machen will. So einfach ist das-