[Cs254f11] Simple mapping problem

Thomas Helmuth thGST at hampshire.edu
Thu Nov 17 13:12:22 EST 2011


Also, you could do it with only one map:

(map #(+ 5 (first %)) m)

This just maps a function down m that adds 5 to the first thing in the
inner vector.

-Tom

On Thu, Nov 17, 2011 at 1:05 PM, Lee Spector <lspector at hampshire.edu> wrote:

>
> Jesse,
>
> You've got several near misses there, and there are several ways to do
> what you want. My first impulse is:
>
> (map #(+ % 5)
>     (map first m))
>
> => (90 97 71 85 55 90 71 42 108 39 118)
>
> Reading from the inside out, it maps first down m to produce a list of all
> the pitches (the first elements of the notes in your melody). Then it maps
> a function down that list of pitches. That function is specified
> anonymously, using the #() syntax, and it is the function that returns the
> result of calling + on its argument (%) and 5.
>
> In place of #(+ % 5) you could write, if you like, (fn [x] (+ x 5)).
>
> Of course, you could use a variable there in place of the 5.
>
> If you don't like the nesting you could do something like:
>
> (let [pitches (map first m)]
>  (map #(+ % 5) pitches))
>
> And there are many other options. Here's a particularly weird one just for
> the heck of it:
>
> (->> (map first m)
>     (map #(+ % 5)))
>
>  -Lee
>
>
>
> On Nov 17, 2011, at 12:05 PM, Jesse French wrote:
>
> > Hi all,
> >
> > I'm trying to figure out how to map some simple functions along a
> vector, and having trouble due to my lack of understanding of how map works.
> >
> > (def m
> > [[85 1]
> > [92 5/16]
> > [66 15/16]
> > [80 1/8]
> > [50 1/8]
> > [85 1/4]
> > [66 13/16]
> > [37 1/16]
> > [103 1/8]
> > [34 1/8]
> > [113 7/16]]
> >
> > My goal is to map a '+ x' down the first value of this vector.
> >
> > Even
> > (map first (map inc m))
> > doesn't work, when I would like to be doing something like
> > (map first (map (+  m x))
> > or
> > (map (+  (map first m) x))
> >
> >
> > (def p [100 15 29 7 63 59 44 72])
> > (map inc p)
> > This works, so I'm assuming my problem is with the syntax of how I'm
> nesting them.
> >
> >
> > Any thoughts?
> >
> > Jesse
> >
> > _______________________________________________
> > 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
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.hampshire.edu/pipermail/cs254f11/attachments/20111117/3c000623/attachment-0001.htm>


More information about the Cs254f11 mailing list