I dont get the error when running your example.

Notice that your function (allDiff? '() '(1 2 3)) returns '()
I suppose you want #t in this case.

Why the line    ( if (null? (cdr list1))  ?
Simpler is:

(define (allDiff? list1 list2)  
 (if (null? list1) #t
  (if (memq (car list1) list2) #f
   (allDiff? (cdr list1) list2))))

How about using cond in stead of if?

(define (allDiff0? list1 list2)  
 (cond
  ((null? list1))
  ((memq (car list1) list2) #f)
  (else (allDiff0? (cdr list1) list2))))

Other simple versions:

(define (allDiff1? list1 list2)  
 (or (null? list1)
  (and (not (memq (car list1) list2))
   (allDiff1? (cdr list1) list2))))

(define (allDiff2? list1 list2)  
 (not (ormap (curryr memq list2) list1)))

(define (allDiff3? list1 list2)  
 (not (for/or ((a (in-list list1))) (memq a list2))))

(define (allDiff3a? list1 list2)  
 (not
  (for/or ((a (in-list list1)) (b (in-list list2)))
   (or (memq a list2) (memq b list1)))))

Or you can recur on both lists:

(define (allDiff4? list1 list2)
 (or (null? list1) (null? list2)
  (and
   (not (memq (car list1) list2))
   (not (memq (car list2) list1))
   (allDiff4? (cdr list1) (cdr list2)))))

And two very odd versions ;)

(define (allDiff5? list1 list2)
 (define a (remove-duplicates list1 eq?))
 (define b (remove-duplicates list2 eq?))
 (=
  (length (remove-duplicates (append list1 list2) eq?))
  (+ (length a) (length b))))

(define (allDiff6? list1 list2)
 ; This version assumes the lists not to contain #f
 (define a (remove-duplicates list1 eq?))
 (define b (remove-duplicates list2 eq?))
 (not (check-duplicates (append a b) eq?)))

Jos


-----Original Message-----
From: [email protected] [mailto:[email protected]]
On Behalf Of Andreas Petrou
Sent: miƩrcoles, 27 de abril de 2016 2:39
To: Racket Users
Subject: [racket-users] why this error?

My code is here:)
;3.) A function called allDiff? that takes as parameters two lists. The
function returns #t if none of  the elements in the first list can be found
in the second
(
 define (allDiff? list1 list2)  
    ( if (null? list1) '()
           ( if (null? (cdr list1))
             (if (memq (car list1) list2) #f #t)
               (if (memq (car list1) list2) #f
                   (allDiff? (cdr list1) list2))
               )
           )
  )

See this error. Please Help
list1: undefined;
 cannot reference an identifier before its definition

-- 
You received this message because you are subscribed to the Google Groups
"Racket Users" group.
To unsubscribe from this group and stop receiving emails from it, send an
email to [email protected].
For more options, visit https://groups.google.com/d/optout.

-- 
You received this message because you are subscribed to the Google Groups 
"Racket Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
For more options, visit https://groups.google.com/d/optout.

Reply via email to