[Cs254f11] Variable scope issue

Wm. Josiah Erikson wjerikson at hampshire.edu
Thu Nov 17 11:47:01 EST 2011


So I'm rewriting my functions that create my globally-accessible map of 
functions to values per song. I'm rewriting them because I figured it 
was bad form to make them depend on "song" being set globally - rather I 
could pass the value of song to them, and that would be better style. 
That's all fine and dandy, except that the function I've written to 
create my global map doesn't seem to pass the value of song that it gets 
passed into it on to the functions that it calls:

(defn create_function_map
   "Returns a map where the keys are the functions in function-list
    and the values are what you get when you eval said function with 
song set to song"
   [song function-list]
   (zipmap function-list (map eval (map #(list % 'song) function-list))))

(defn create_terminal_map_vector
   "This takes a list of function names and a list of parsed songs, and 
returns a vector of maps.
    The maps have keys that are the function names and the values are 
the values that said functions
    return when song is set to the corresponding song in parsed-song-list"
   [function-list parsed-song-list]
   (vec(map create_function_map parsed-song-list (repeat function-list))))

(def terminal-map (create_terminal_map_vector list-of-analysis-functions 
parsed_songs))

That last def, which is what creates the data that everything else 
refers to, barfs because create_function_map (yes, I know, I could roll 
it into create_terminal_map_vector, and I probably will eventually, but 
it's easier to read this way for now) doesn't pass its value of song on 
to the "eval" call. If I just modify create_function_map so that it does 
a "def song song" before the zipmap, everything is fixed. I also tried a 
local "let", but that doesn't work either. Why? Clearly I'm missing 
something obvious. I shouldn't have to def things inside functions, right?

Thanks in advance,

-- 
Wm. Josiah Erikson
Network Engineer
Hampshire College
Amherst, MA 01002
(413) 559-6091



More information about the Cs254f11 mailing list