[Cs254f11] 2D vectors
Wm. Josiah Erikson
wjerikson at hampshire.edu
Tue Oct 11 15:33:48 EDT 2011
Um. Never mind. Clearly "notes" and "tunes" are not the same thing.
I should get more sleep or something.
-Josiah
On 10/11/11 3:32 PM, Wm. Josiah Erikson wrote:
> So what if I have a list of lists, like this:
>
>
> song_creation.core=>
> (repeatedly 20 #(create-tune length bottom top))
> ((48 58 71 70 68 66 72 66 53 66 69 69 49 51 62 59 62 69 55 62)
> (67 48 69 49 69 66 54 72 71 69 65 56 51 52 58 66 51 57 70 52)
> (62 69 70 61 57 59 69 63 60 49 58 61 67 56 72 57 60 70 72 66)
> (61 64 69 58 60 67 68 69 58 59 58 63 72 64 59 55 57 55 61 64)
> (66 56 65 66 54 65 65 70 64 72 48 69 64 61 57 71 65 57 69 72)
> (51 68 72 62 54 61 68 50 65 68 52 59 65 49 55 70 52 68 57 56)
> (67 50 59 49 65 60 64 68 67 65 65 54 63 52 69 71 55 56 60 50)
> (64 53 60 60 62 56 48 57 62 54 53 71 62 65 48 71 59 49 62 54)
> (65 50 59 58 67 61 58 70 71 51 51 63 49 62 54 71 50 62 57 69)
> (49 51 51 70 69 53 57 54 48 51 63 49 57 68 56 50 54 70 65 53)
> (55 55 68 50 72 61 67 61 66 57 59 56 59 68 72 56 54 63 55 70)
> (65 48 56 60 70 57 63 59 69 52 51 62 54 70 58 67 50 50 58 48)
> (66 69 55 56 71 48 70 57 55 59 60 66 59 48 56 50 70 64 49 48)
> (50 55 67 64 54 52 58 51 71 57 65 61 51 65 56 56 48 50 49 63)
> (48 71 52 50 55 68 48 71 58 61 62 63 48 69 59 68 54 61 67 71)
> (67 51 72 64 51 53 54 62 56 64 63 63 59 62 58 53 52 60 71 48)
> (65 61 52 51 57 59 58 62 48 61 53 55 53 49 57 55 61 69 70 72)
> (50 52 71 69 60 67 53 48 72 62 55 48 57 72 52 64 48 71 52 72)
> (65 51 66 51 66 61 69 50 67 63 68 57 56 56 69 71 51 69 57 49)
> (68 56 65 53 50 49 58 62 54 57 58 59 59 58 51 70 67 66 63 72))
>
> song_creation.core=>
> (def tunes(repeatedly 20 #(create-tune length bottom top)))
> #'song_creation.core/tunes
>
> tunes
> ((67 56 57 72 69 72 60 68 68 50 49 50 64 62 50 62 48 65 48 64)
> (50 64 59 66 61 59 53 57 66 66 66 71 60 56 71 52 71 49 54 62)
> (71 58 52 55 48 56 50 58 70 68 56 55 49 70 66 51 65 52 68 70)
> (59 50 72 52 55 57 64 58 71 49 69 53 48 48 58 52 48 50 58 70)
> (54 72 50 56 63 65 64 50 56 70 56 66 48 61 71 56 70 64 69 58)
> (57 57 69 66 61 54 51 66 66 50 63 49 67 48 64 61 68 49 61 69)
> (66 68 62 67 57 48 59 51 59 58 57 65 50 54 55 65 63 69 70 62)
> (66 72 54 48 49 52 70 50 51 71 52 63 63 71 60 59 67 68 53 52)
> (66 58 68 55 66 67 65 64 56 61 70 58 67 64 69 55 63 53 70 66)
> (62 71 51 62 67 49 60 71 50 52 63 72 71 71 69 68 59 54 51 51)
> (70 61 72 49 72 56 64 66 71 51 53 54 49 50 64 64 55 48 72 56)
> (61 72 67 54 56 68 54 58 49 62 69 69 63 62 60 56 48 49 52 53)
> (48 68 55 66 71 54 66 49 61 62 48 64 58 66 53 63 56 72 68 62)
> (48 55 65 68 56 53 69 55 67 50 57 71 51 50 49 51 72 56 60 58)
> (48 71 53 60 60 67 50 55 71 71 61 61 49 59 56 54 50 52 49 71)
> (48 49 67 69 64 72 69 49 57 49 62 72 67 48 49 57 56 71 58 52)
> (54 69 58 62 71 71 52 57 48 51 58 68 59 68 72 60 55 52 63 51)
> (49 71 49 51 67 65 57 72 48 48 70 54 54 60 70 71 55 50 48 58)
> (56 62 69 52 53 70 59 63 63 71 52 72 61 56 62 52 65 59 71 69)
> (71 71 60 48 72 68 52 61 49 60 62 52 72 51 50 53 49 52 63 65))
>
> song_creation.core=>
> (nth notes 10)
> 58
>
> song_creation.core=>
> (nth notes 0)
> 48
>
> I expected nth to return me the entire collection, (which I could then
> use a nested call to nth on) but it doesn't - instead it returns
> something I actually don't understand. Should I just use vectors instead?
>
> -Josiah
>
>
> On 10/4/11 5:01 PM, Lee Spector wrote:
>> In response to some off-list questions I put together a little code
>> to show how one might represent a 2D grid as a vector of vectors,
>> with the internal vectors being rows of the grid. You could put
>> anything in the actual grid locations, and unlike 2D arrays in many
>> languages there could be different types of things in each location.
>>
>> I'm sharing this with the class list in case anyone else finds it
>> handy. I don't show the return values here, but you could just
>> evaluate it expression by expression in clooj to see what it does:
>>
>> (ns vec2D)
>>
>> (defn getXY
>> "Returns the contents of location (x, y) in a 2D vector v2D."
>> [v2D x y]
>> (get (get v2D x) y))
>>
>> (defn setXY
>> "Returns a 2D vector like v2D but with newval at location (x, y)."
>> [v2D x y newval]
>> (assoc v2D
>> x
>> (assoc (get v2D x)
>> y
>> newval)))
>>
>> (defn procXY
>> "Returns a 2D vector like v2D but with location (x, y) filled
>> with the result of calling procfn on the old contents of
>> location (x, y)."
>> [v2D x y procfn]
>> (setXY v2D x y (procfn (getXY v2D x y))))
>>
>> (def myvec2D
>> [[0 0 0 0 0]
>> [0 0 0 0 0]
>> [0 0 0 0 0]
>> [0 0 0 0 0]
>> [0 0 0 0 0]])
>>
>> myvec2D
>>
>> (setXY myvec2D 2 2 8)
>>
>> (procXY myvec2D 2 2 inc)
>>
>> (def altered-vec2D
>> (-> myvec2D
>> (setXY 2 2 8)
>> (procXY 2 2 inc)
>> (procXY 0 0 dec)
>> (procXY 3 1 #(+ % 23))))
>>
>> altered-vec2D
>>
>> (for [i (range 5)]
>> (for [j (range 5)]
>> (if (zero? (getXY altered-vec2D i j))
>> 100
>> (getXY altered-vec2D i j))))
>>
>> (def mysetvec2D
>> [[#{} #{} #{} #{} #{}]
>> [#{} #{} #{} #{} #{}]
>> [#{} #{} #{} #{} #{}]
>> [#{} #{} #{} #{} #{}]
>> [#{} #{} #{} #{} #{}]])
>>
>> (-> mysetvec2D
>> (procXY 2 2 #(conj % :apple))
>> (procXY 2 3 #(conj % :banana)))
>>
>> --
>> 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
>
--
Wm. Josiah Erikson
Network Engineer
Hampshire College
Amherst, MA 01002
(413) 559-6091
More information about the Cs254f11
mailing list