-1

Ok so I define 3 lists:

(define mylist '((pause 5 5 5)(note 3 4 5)(pause 3 4 4)))
(define myseqlist '(sequential-music-element (note 5 5 5) (note 4 3 4) (note 5 3 4)))
(define myparlist '(parallel-music-element (note 5 2 5) (note 4 2 4) (note 5 3 1)))

I have the predicates note? and pause? , essentially it checks if a list starts with 'note or 'pause then returns true or false. However I can have list containing notes and pauses, which is callled a sequential-music-element (see myseqlist above) or parallel-music-element (see myparlist above)

How can I define a function that returns the degree of polyphony, that takes one of the 4 elements as a parameter? (see below for my attempt)

pause degree of polyphony = 0

note degree of polyphony = 1

sequence-music-element? return the degree of the child with the largest degree

parallel-music-element? return the sum of the degrees of the children

How do I complete this function:

(define (degree-of-polyphony elem)
  (cond [(note? elem) 1]
        [(pause? elem) 0]
        [(sequential-music-element? elem) (argmax ??? )]))

I am not sure how to make use of argmax in this case. It should check for the highest polyphony for the entire list of elements that degree-of-polyphony takes as a parameter.

Example: (degree-of-polyphony myseqlist) should return 1 since it has at least one note, if its all pauses, it should return 0.

Byte Commander
  • 6,506
  • 6
  • 44
  • 71
crystyxn
  • 1,411
  • 4
  • 27
  • 59
  • 2
    Please do not vandalize your posts. Once you've posted a question, it belongs to the Stack Overflow community at large (under the CC-by-SA license). If you would like to disassociate this post from your account, see [What is the proper route for a disassociation request](http://meta.stackoverflow.com/q/323395/584192)? – Samuel Liew Oct 15 '17 at 21:37

1 Answers1

1

If you just have to check if there's at least one note in the list, I'd go with this:

(define (note? ele)
  (and (pair? ele)
       (eq? (car ele) 'note)))

(define (degree-of-polyphony elem)
  (cond [(note? elem) 1]
        [(pause? elem) 0]
        [(sequential-music-element? elem)
         (if (ormap note? elem) 1 0)]))

ormap checks if there's at least one element that meets the given condition, argmax would be useful if we needed to find a maximum value.

Óscar López
  • 232,561
  • 37
  • 312
  • 386
  • it works if I give it myseqlist, but If it give it a list of only pauses, it does not return 0, it doesnt do anyhitng – crystyxn Oct 15 '17 at 21:18
  • Please show me the list. Maybe you passed a `parallel-music-element`? notice that you're not handling that case. – Óscar López Oct 15 '17 at 21:19
  • I passed `'((pause 5 5 5)(pause 3 4 5)(pause 3 4 4))` – crystyxn Oct 15 '17 at 21:19
  • Exactly, you didn't tag the list - it's not a note, a pause or a sequential or a parallel list, so it doesn't fall under any of the conditions of your procedure, hence it returns void. You should pass it like this: `'(sequential-music-element (pause 5 5 5) (pause 3 4 5) (pause 3 4 4))` – Óscar López Oct 15 '17 at 21:21
  • but in this case I do not pass a sequence of elements. I pass a group of elements. So the degree of polyphony should return the maximum polyphony found (meaning if there is one note, it will return 1) – crystyxn Oct 15 '17 at 21:26
  • You have to define the input more precisely, then. Look your conditions: you check to see if it's a note, or a pause, or a sequence - none of those match a list like this: `'((pause 5 5 5) (pause 3 4 5) (pause 3 4 4))`. Either tag the input, or add an extra case in your procedure, to handle the new input type: a "group of elements" – Óscar López Oct 15 '17 at 21:28
  • Always a pleasure :) – Óscar López Oct 15 '17 at 21:29