[Cs254f11] Basic vector question

Lee Spector lspector at hampshire.edu
Sun Oct 9 20:53:43 EDT 2011


Omri: This is good but on your second set of examples I think you meant to be calling get-in on example-vec2, not on example-vec.

 -Lee



On Oct 9, 2011, at 7:56 PM, Omri Bernstein wrote:

> I think I get your meaning, you have a vector with a vector nested in it--like [1 2 [1 5] 3 4]--right? If you want to get a value within a vector within a vector you can use the function "get-in". For example:
> 
> (def example-vec
>   [1 2 [1 5] 3 4])
> 
> (get-in example-vec [2 0])
> >> 1
> 
> (get-in example-vec [2 1])
> >> 5
> 
> The "get-in" function takes a collection and then a vector of what it should use on (what is equivalent to) recursive calls to "get". So the first example above is the same thing as doing (get 0 (get 2 example-vec)). If you had a nested vector within your nested vector, you could access those values directly by doing something like this:
> 
> (def example-vec2
>   [1 2 [1 [10 12] 5] 3 4])
> 
> (get-in example-vec [2 1 0])
> >> 10
> 
> (get-in example-vec [2 1 1])
> >> 12
> 
> Hope it helps.
> 
> -Omri
> 
> 
> On Sun, Oct 9, 2011 at 6:41 PM, James Matheson <jmatheson12 at gmail.com> 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
> 
> _______________________________________________
> 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