[Cs254f11] Several questions

Wm. Josiah Erikson wjerikson at hampshire.edu
Mon Oct 3 18:20:09 EDT 2011


I'm sure there is a quick and easy answer to both of these questions. 
I'm still trying to get my chops. So I've got a function:

(defn modified_truth
  "If the first argument is literally true, as in that exact string,
   then this function will return the second argument.
   Otherwise, this function will return 0."
  [arg1 modification]
  (if (= arg1 true) (* 1 modification) (* 0 1)))

So the (* 0 1) is silly and is really just a way to return 0. Is there a 
better way to just have something that evaluates to 0? I hate wasting 
CPU cycles :)

Now the harder (or more mysterious to me) question:

I have a map of musical keys, like so:

(def musical-keys { :C 0
           :C# 1
           :D 2
           :D# 3
           })
etc...

Then I am writing a function definition that gets passed a musical key, 
like "C" for instance, and I want to map it to the corresponding number, 
but I don't know how to use a string that contains the value of a key in 
a key-value pair and use it to get the value out of a map. I know how to 
do this:

(:C musical-keys)

But I want to be able to do something like this, assuming that my_song 
is a list of integers:
(def song_key D)

(modified_truth (if (= (:song_key musical-keys) (first my_song)) 0.8)

Does this make any sense?

Thanks in advance,


-----
Wm. Josiah Erikson
Network Engineer
Hampshire College
Amherst, MA 01002



More information about the Cs254f11 mailing list