Search code examples
schemeguile

Using Scheme to assign the value returned by a function to a variable in another function


I have a function remove_duplicates that removes the duplicates in a list and returns the new list.

(define (remove_duplicate list)
(cond
    ((null? list) '() )

    ;member? returns #t if the element is in the list and #f otherwise
    ((member? (car list) (cdr list)) (remove_duplicate(cdr list)))

    (else (cons (car list) (remove_duplicate (cdr list))))

))

I want to assign the return value from this function call to a variable in another function f.

(define (f list)
    ;I have tried this syntax and various other things without getting the results I would like
    (let* (list) (remove_duplicates list))
)

Any help is appreciated, thanks!


Solution

  • This is the correct syntax for using let:

    (define (f lst)
      (let ((list-without-dups (remove_duplicates lst)))
        ; here you can use the variable called list-without-dups
        ))
    

    Also notice that it's a bad idea to name list a parameter and/or a variable, that clashes with a built-in procedure with the same name.