-1

I am wondering how to convert the explicit recursive functions below into a higher-ordered/abstract functions using foldr, map, filter, etc in Scheme/Racket.

(define (insertNoDups f element lst)
  (cond[(empty? lst) (cons element lst)]
       [(f element (first lst)) lst]
       [else (cons (first lst) 
                   (insertNoDups f element (rest lst)))]))

(define (remove-dups f lst)
  (cond[(empty? lst) empty]
       [else (insertNoDups  f 
                            (first lst) 
                            (remove-dups f (rest lst)))]))
Telean
  • 1
  • 1

1 Answers1

0

insertNoDups (rename it to insert-no-dups) : use member-procedure predicate (more info -> http://www.gnu.org/software/mit-scheme/documentation/mit-scheme-ref/Searching-Lists.html) and cons element to the front if not found.

remove-dups: look at Óscar López's answer here -> How to remove all the duplicates in a list using scheme (only abstract list functions allowed) and use member-procedure predicate instead of member?

Community
  • 1
  • 1
Mathieu Borderé
  • 4,357
  • 2
  • 16
  • 24
  • Thanks for your reply. But what I want is to combine the two functions above into one functions that using abstract function. Also "f" in the functions refers to a function that consumes two variables like (lambda (x y)...). Can you help me to figure out this? thx. – Telean Nov 20 '15 at 18:58
  • isn't that hard to figure out, might be worthwhile to investigate the concepts you want to apply. Because at the moment it seems that you don't have an idea what they do. – Mathieu Borderé Nov 21 '15 at 11:49
  • I really have no idea about this... I've trying a whole day to translate these functions into a abstract function, but nothing come up... Any help please.. – Telean Nov 22 '15 at 22:25