Function no returning the value, Scheme programming

Started by
2 comments, last by Zahlman 13 years, 5 months ago
Hi !

I wrote a program, that finds unique item from the list and returns the list, problem - it doesn't return the list !!!!

;; checks if the items is atom(define atom?  (lambda (x)    (and (not (pair? x)) (not (null? x)))));; to store unique items(define uniqueItems '())(define recursive_search      (lambda (myList)        (cond           ((null? myList) #t)          ((atom? (car myList))            (if (eq? (memq (car myList) uniqueItems ) #f)              (set! uniqueItems (cons (car myList) uniqueItems ))              (extractUniqueAtoms_recurse (cdr myList))            )          )          ((list? (car mylist))            (recursive_search (car myList))            (recursive_search (cdr myList))))))

Thanks !

[Edited by - Zahlman on October 28, 2010 7:33:37 AM]
Advertisement
Which list are you trying to return, the uniqueItems list or the argument list? Also, can I assume that you meant "resursive_search" when you wrote "extractUniqueAtoms_recurse". You could have another function that would call recursive search, and then return the uniqueItems variable after recursive_search is finished building it. Like this:

(define get-unique-items  (lambda (myList)    (recursive_search myList)    uniqueItems))


A better way to do it would be to represent uniqueItems as a set. A simple way to do this (not efficient) is to use the lset operations from the srfi-1 library. These operations treat lists as sets. There should be some way to include functions from this library with the scheme implementation you are using.

(define get-unique-items  (lambda (my-list)    (cond      ((null? my-list) '())      ((pair? my-list)       (cond         ((or (null? (car my-list)) (pair? (car my-list)))          (lset-union eq? (get-unique-items (car my-list))                          (get-unique-items (cdr my-list))))         (else (lset-adjoin eq? (get-unique-items (cdr my-list))                                (car my-list)))))      (else ; case where my-list is not a list, just return the            ; argument as the only unique item in the set            ; could also return an error if desired        (list my-list)))))
Quote:
I wrote a program, that finds unique item from the list and returns the list, problem - it doesn't return the list !!!!


1) You've written your function to return either #t, #f or nothing at all (return value of set! is unspecified). Why do you expect it to return a list when you've told it not to return one?

2) If you've correctly pasted in your whole program, you haven't defined extractUniqueAtoms_recurse.

3) Using destructive assignment here (set!) goes against everything that is good and pure.
Don't use !-predicates here. Become comfortable with recursion. Hint: have recursive_search take two parameters, the second of which is uniqueItems. Pass in () the first time you call it, and have the recursive calls build up its contents. When myList is null, return the uniqueItems, so that they bubble up all the way through the recursion.

This topic is closed to new replies.

Advertisement