Scheme using modulo operator on a list -
i found max value of list , want return numbers except numbers have modulo equal 0 max value , im not quite sure how use modulo operator on whole list numbers need can lead me in right direction have far
(define (findlargest a_list) (if (null? a_list) ;if empty list #f ;its false (let loop ((a_list (cdr a_list)) ;binds loop variable a_list value of cdr a_list(second , subsequent items in list) (maxval (car a_list))) ;maxval set car of a_list (first item of list) (cond ((null? a_list) maxval) ;if list empty return max ((> (car a_list) maxval) ;checks see if current element > max (loop (cdr a_list) (car a_list))) ;find new max (else (loop (cdr a_list) maxval)));keeps same max
you're reinventing wheel findlargest
, there's built-in procedure that:
(define (findlargest lst) (apply max lst))
now, regarding question - looks perfect job filter
:
(define (filter-max-modulo lst) (let ((max-val (findlargest lst))) (filter (lambda (val) (not (zero? (modulo val max-val)))) lst)))
for example:
(filter-max-modulo '(0 -2 -4 -3 -7 -1 2)) => '(-3 -7 -1)
Comments
Post a Comment