[Cs254f11] Wow. Basic question

Wm. Josiah Erikson wjerikson at hampshire.edu
Wed Oct 26 14:57:50 EDT 2011


Heh, yeah. I was just able to turn this:

(let [real_notes (vec (get_real_notes (get_note_lines song)))
         unique_real_notes (vec(distinct real_notes))
         frequencies_of_note_occurrance (loop [counter (dec (count 
unique_real_notes)) frequencies ()]
                                          (if (< counter 0)
                                            frequencies
                                            (recur (dec 
counter)(cons(count (filter #(= (get unique_real_notes counter) %) 
real_notes))frequencies))))]
     (peek(last(sort(map vector frequencies_of_note_occurrance 
unique_real_notes))))))

into this:

(first(last(sort #(< (second %) (second %2) )(frequencies (get_notes 
(get_note_lines song)))))))

That's my favorite part of this language.

     -Josiah


On 10/26/11 11:48 AM, Lee Spector wrote:
> Yeah, "distinct" is really useful. As is "frequencies". These are particularly nice because writing them from scratch is a pain, and it turns out that they can be used in all sorts of unexpected situations to make it easy to do things that seemed hard at first. Sort of like "sort" can be used in lots of situations.
>
>   -Lee
>
> On Oct 26, 2011, at 10:01 AM, Wm. Josiah Erikson wrote:
>
>> Ha! See, I knew it would be that simple. I didn't realize that "vector" was a function. But of course it is :)
>> Now another question - there must be a neat function that takes a collection and returns a collection of the unique values, yes? Like if I had
>>
>> ( a b c d a b e d g h d r q)
>>
>> it would return
>>
>> (a b c d e g h r q)
>>
>> Oh wait, just found it. It's called "distinct". I knew it would exist :)
>>
>> Excellent. Now my "count the frequencies of all the notes in the song" function should run much faster.
>>
>> Thanks,
>>    -Josiah
>>
>>
>> Lee Spector wrote:
>>> (def a '(1 2 3 4 5))
>>>
>>> (def b '(i j l k m))
>>>
>>> (map vector a b)
>>>
>>> ; ([1 i] [2 j] [3 l] [4 k] [5 m])
>>>
>>> That what you wanted?
>>>
>>> -Lee
>>>
>>>
>>> On Oct 25, 2011, at 11:00 PM, Wm. Josiah Erikson wrote:
>>>
>>>
>>>> I must be up too late.
>>>>
>>>> I have a list, a, and another list, b.
>>>>
>>>> I want to make a new list of two-item vectors that is both lists put together, so that it will look like ([(nth a 1)(nth b 1)][(nth a 2)(nth b 2)][(nth a 3)(nth b 3)])
>>>>
>>>> ...and so on. interleave doesn't quite do what I want. Maybe in the morning it will occur to me how to do this without a loop. If it's obvious to somebody else, will you help me out?
>>>>
>>>> Thanks in advance,
>>>>
>>>> -- 
>>>> -----
>>>> Wm. Josiah Erikson
>>>> Network Engineer
>>>> Hampshire College
>>>> Amherst, MA 01002
>>>>
>>>> _______________________________________________
>>>> 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
>>>
>>>
>> -- 
>> -----
>> Wm. Josiah Erikson
>> Network Engineer
>> Hampshire College
>> Amherst, MA 01002
>>
> --
> 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
>

-- 
Wm. Josiah Erikson
Network Engineer
Hampshire College
Amherst, MA 01002
(413) 559-6091



More information about the Cs254f11 mailing list