Making my own helper function

49 views
Skip to first unread message

JJ C

unread,
Jul 23, 2020, 11:43:48 PM7/23/20
to Racket Users
I am in a course where I have to use beginning student with list abbreviation in Racket to do assignments. 
Below is my code for unique-right, which is supposed to return a list which has only the right most occurrences of the elements of list. 
The restriction in doing the asisngment is that I cannot use any of the built-in functions such as append or member. 
I'd need to re name a function and define it as I was doing below. For example, in the place where I need to use member? I renamed it as mymember.
Now what I am trying to do is define the helper function mymember. I know that that bold part is incorrect but I have no idea at the moment how to make the code correct. 




; unique-right
; List<Any> -> List<Any>
; returns a list which has only the right most occurrences of the elements of lst

(define (unique-right lst)
  (cond
    [(empty? lst) lst]
    [else (if (mymember? (first lst) (rest lst)) (unique-right (rest lst)) (cons (first lst) (unique-right (rest lst))))]))



; mymember
; List<Any> -> List<Any>
; returns a list that has all of the elements of lst plus a non-occurring elt

(define (mymember elt lst)
  (cond
    [(empty? lst) lst]
    [else (if (equal? elt (first lst))
              lst
              (cons (mymember elt (rest lst)))]))
             



Averell Aquino

unread,
Jul 25, 2020, 3:30:56 AM7/25/20
to Racket Users
Creating examples will show you what you need to do. Check the examples below if I understood your intention.

; unique-right
; List<Any> -> List<Any>
; returns a list which has only the right most occurrences of the elements of lst
(check-expect (unique-right '()) '())
(check-expect (unique-right (list "a")) (list "a"))
(check-expect (unique-right (list "a" "b" "a")) (list "b" "a"))
(check-expect (unique-right (list "a" "b" "a" "c" "d" "c")) (list "b" "a" "d" "c"))
(define (unique-right lst)
  (cond
    [(empty? lst) '()]
    [else (if (mymember? (first lst) (rest lst))
              (unique-right (rest lst))
              (cons (first lst) (unique-right (rest lst))))]))

; mymember
; Any List<Any> -> Boolean
; is elt in list
(check-expect (mymember? "a" '()) #false)
(check-expect (mymember? "a" (list "a")) #true)
(check-expect (mymember? "c" (list "a" "b" "a")) #false)
(define (mymember? elt lst)
  (cond
    [(empty? lst) #false]
    [else (or (equal? elt (first lst))
              (mymember? elt (rest lst)))]))
Reply all
Reply to author
Forward
0 new messages