I am attempting to write a clojure version of lecture 6.7 from Martin Odersky"s functional progamming course. The original was written in Scala.
The concept is to take a phone number (as a string) and produce a list of possible phrases given a dictionary of words.
So, translate("7225247386") would produce, amongst others, the phrase "Scala is fun"
Here's a Gist with the full Scala version.
Here's my attempt using Clojure
The problem occurs with the final 'encode' function The Scala version is
def encode(number: String): Set[List[String]] =
if (number.isEmpty) Set(List())
else {
for {
split <- 1 to number.length
word <- wordsForNum(number take split)
rest <- encode(number drop split)
} yield word :: rest
}.toSet
With my Clojure version
(defn encode
"Takes a number as a string. Returns all the different ways to encode that number as a list of words"
[number]
(if (empty? number)
(vector)
(for [split (range 0 (count number))
word (words-from-number (subs number 0 split))
remainder (encode (subs number split))]
(concat word remainder))))
The problem seems to be that strings keep getting converted to lists of characters; that's why I used (subs number 0 split) rather than (take split number)
A version of the above which just finds the first word seems to be on the right track...
(defn encode
[number]
(for [split (range 0 (count number))
word (words-from-number (subs number 0 split))]
word))
(encode "7225247386") => ("pack" "rack" "sack" "Scala")
...but adding in the recursive calls to 'encode' with the remaining digit-string just returns an empty list.
Any advice would be gratefully appreciated.
Thank you.