Skip to content

Instantly share code, notes, and snippets.

@dawranliou
Last active April 27, 2018 17:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dawranliou/b08e6d3059253471050e98c80875ae8b to your computer and use it in GitHub Desktop.
Save dawranliou/b08e6d3059253471050e98c80875ae8b to your computer and use it in GitHub Desktop.
Functional Programming in Scala 6.5 in Clojure
(def words
(clojure.string/split-lines (slurp "https://lamp.epfl.ch/files/content/sites/lamp/files/teaching/progfun/linuxwords.txt")))
(def mnem
{\2 "ABC" \3 "DEF" \4 "GHI" \5 "JKL" \6 "MNO" \7 "PQRS" \8 "TUV" \9 "WXYZ"})
(def char-code
(into {} (for [[k vs] mnem
v vs]
[v k])))
(defn word-code
[word]
(apply str (map char-code (.toUpperCase word))))
(def words-for-num
(group-by word-code words))
(defn decode
[number]
(if (empty? number)
#{()}
(into #{} (for [split (range 1 (inc (.length number)))
word (get words-for-num (subs number 0 split))
rest (decode (subs number split))]
(cons word rest)))))
(defn translate
[number]
(map (partial clojure.string/join " ") (decode number)))
(translate "5299626")
;; ("lazy Nan" "jazz man" "lazy Mao" "jazz Nan" "lazy man" "jazz Mao")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment