[Cs254f11] Confusion over maps

Lee Spector lspector at hampshire.edu
Tue Nov 15 21:59:09 EST 2011


Ah -- well there may be tradeoffs either way, but you might find this piece of hackery interesting:

(defn foo [x] (+ x 23))

((eval (read-string (name :foo))) 100)

 -Lee


On Nov 15, 2011, at 9:54 PM, Wm. Josiah Erikson wrote:

> Thanks. I think that if I use keywords then I will have trouble calling things the way I need to, because they are dual-purpose, but perhaps I can get clever and figure out a way to do it.
> I knew there had to be a simple explanation.
>    -Josiah
> 
> 
> On 11/15/11 9:48 PM, Lee Spector wrote:
>> You don't have to have : in front of keys for maps to work properly, but the nice thing about keywords (symbols that start with :) is that they don't have to be quoted. And you seem to have a quoting issue here.
>> 
>> The simple fix, if you want to continue using those non-keyword keys, is to change:
>> 
>>> (get test low_note)
>> to
>> 
>> (get test 'low_note)
>> 
>> What you were doing was asking for the value in test for the key which IS THE VALUE of a var called low_note. In other words, it will evaluate test (to get the map), and low_note (to get whatever was bound to that var), and look up the value in low_note in the map that's in test. That *should* give you an error.... and from the fact that it doesn't, and that it's giving you nil instead, I'm guessing that you've actually def'd low_note somewhere, giving it some value... and that value is what you're looking up in the map, and since it doesn't find that you're getting nil.
>> 
>> If I recreate your map, which requires that I quote all of the symbols as follows:
>> 
>> (def test
>> {'average_velocity 37,
>> 'number_of_different_real_notes 57,
>> 'most_common_real_note 64,
>> 'average_real_note 60,
>> 'average_note 57,
>> 'low_note 28,
>> 'high_note 91,
>> 'std_dev_mostcom 11})
>> 
>> then I can do:
>> 
>> (get test 'low_note)
>> 
>> to get
>> 
>> 28
>> 
>> and
>> 
>> (find test 'low_note)
>> 
>> to get
>> 
>> [low_note 28]
>> 
>> Alternatively, if you used keywords then you could use :low_note both in the map and in the get and find forms.
>> 
>>  -Lee
>> 
>> 
>> On Nov 15, 2011, at 9:08 PM, Wm. Josiah Erikson wrote:
>> 
>>> I don't understand why my "get" and "find" statements aren't working here. Probably really basic (I don't HAVE to have the useful ":" macro in front of the keys to use a map, do I?)
>>> 
>>> test
>>> {average_velocity 37,
>>> number_of_different_real_notes 57,
>>> most_common_real_note 64,
>>> average_real_note 60,
>>> average_note 57,
>>> low_note 28,
>>> high_note 91,
>>> std_dev_mostcom 11}
>>> 
>>> critic_evolution.core=>
>>> (get test low_note)
>>> nil
>>> 
>>> critic_evolution.core=>
>>> (keys test)
>>> (average_velocity
>>> number_of_different_real_notes
>>> most_common_real_note
>>> average_real_note
>>> average_note
>>> low_note
>>> high_note
>>> std_dev_mostcom)
>>> 
>>> critic_evolution.core=>
>>> (find test low_note)
>>> nil
>>> 
>>> critic_evolution.core=>
>>> test
>>> {average_velocity 37,
>>> number_of_different_real_notes 57,
>>> most_common_real_note 64,
>>> average_real_note 60,
>>> average_note 57,
>>> low_note 28,
>>> high_note 91,
>>> std_dev_mostcom 11}
>>> 
>>> critic_evolution.core=>
>>> (vals test)
>>> (37 57 64 60 57 28 91 11)
>>> 
>>> critic_evolution.core=>
>>> (map? test)
>>> true
>>> 
>>> critic_evolution.core=>
>>> _______________________________________________
>>> Cs254f11 mailing list
>>> Cs254f11 at lists.hampshire.edu
>>> https://lists.hampshire.edu/mailman/listinfo/cs254f11
>> --
>> Lee Spector, Professor of Computer Science
>> Cognitive Science, Hampshire College
>> 893 West Street, Amherst, MA 01002-3359
>> lspector at hampshire.edu, http://hampshire.edu/lspector/
>> Phone: 413-559-5352, Fax: 413-559-5438
>> 
> _______________________________________________
> Cs254f11 mailing list
> Cs254f11 at lists.hampshire.edu
> https://lists.hampshire.edu/mailman/listinfo/cs254f11

--
Lee Spector, Professor of Computer Science
Cognitive Science, Hampshire College
893 West Street, Amherst, MA 01002-3359
lspector at hampshire.edu, http://hampshire.edu/lspector/
Phone: 413-559-5352, Fax: 413-559-5438



More information about the Cs254f11 mailing list