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)))))))))

0 Kommentare: