[Cs254f11] Basic vector question
Lee Spector
lspector at hampshire.edu
Sun Oct 9 21:01:29 EDT 2011
Jamie,
As Omri noted the "get-in" function is handy for doing nested access, but I find that it's often simpler just to nest calls to "get". So, as you say:
>
> (def data [[0 0], [1 1], [2 4], [3 3]])
>
> then (get data 2) returns [2 4] but if I want just 4, how do I get it?
Well since (get data 2) returns [2 4], and since you want the second guy of THAT (which means the guy with index 1), you can just do:
(get (get data 2) 1)
; 4
Another way, which might be handy if you also wanted to use the pair [2 4] for something else, would be to do:
(let [pair (get data 2)]
(get pair 1))
; 4
The "get-in" function provides a nice shortcut for nested calls to "get", but I personally often like to keep the number of functions I have to remember small, so I'd be likely to just use "get" twice.
-Lee
On Oct 9, 2011, at 7:34 PM, James Matheson wrote:
> So if I:
> (def data [[0 0], [1 1], [2 4], [3 3]])
>
> then (get data 2) returns [2 4] but if I want just 4, how do I get it?
>
> On Sun, Oct 9, 2011 at 6:48 PM, Lee Spector <lspector at hampshire.edu> wrote:
>>
>> I think I must not be understanding your question, but:
>>
>> (def foo [1, 5])
>>
>> (get foo 0)
>>
>> ; 1
>>
>> (get foo 1)
>>
>> ; 5
>>
>> What are you looking for that isn't this?
>>
>> -Lee
>>
>>
>> On Oct 9, 2011, at 6:41 PM, James Matheson wrote:
>>
>>> This is a little basic but how do you access an element within a
>>> vector? I know (nth x) or (get x) will return the value at that index,
>>> but if I'm trying to get something where the value is [1, 5] how do I
>>> access 1 or 5 directly?
>>>
>>> Thanks in advance,
>>> Jamie
>>> _______________________________________________
>>> 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