Need help using set-union

i have written this code which basically checks if the user has submitted and empty input (which returns and error) or inputs a letter which does not belong in neither of three sets,and if its successful it will return "enter you final destination". The part of the code that i am having issues is this part.

((not (and (set-member? line1 a) (set-member? line2 a) (set-member? line3 a)) (error "enter a location which exisits")))

i have tried using set-union with it but it still gives me errors.

i am trying to get racket to check the union of all the lines aganist the user input but for this code if i type in "a" it will say "enter a location which exisits" even though it does.

here is my full code
(define line1 (set "a" "b" "c" "d" "e"))
(define line2 (set "f" "g" "c" "h" "i"))
(define line3 (set "k" "i" "l" "m" "e"))

(define exsists (λ (a)
(cond
((empty? a) (error " you need to enter a starting location"))
((not (set-member? line1 a)) (error "enter a location which exisits"))
(else "enter you final destintation"))))

this is my full code

This line first checks whether a belongs to all three sets at once, because you use an and. This condition is false for "a". By applying not you get #t, which shows the error message.

You should change and to or, or connect multiple negated conditions with an and.

1 Like

i tried replacing and with or it gave me this Module Language: there can only be one expression in the definitions window in: (define line2 (set "f" "g" "c" "h" "i"))

If your previous program worked, changing and to or should not give you this message. Try checking if your program respects the syntax, and in particular whether all parentheses are balanced.

I'm not 100% clear on the intention, but I think this is what you're looking for:

#lang racket
; Accept and verify user input.                                                                
;                                                                                              
; Input is invalid if:                                                                         
;                                                                                              
;   1) it is empty                                                                             
;   2) it does not appear in any of our 3 sets of legal inputs                                 
;                                                                                              
;   In case 1, ask them for a starting location                                                
;   In case 2, ask them for a valid option                                                     
;                                                                                              
; On success, ask for their final destination                                                  
(define line1 (set "a" "b" "c" "d" "e"))
(define line2 (set "f" "g" "c" "h" "i"))
(define line3 (set "k" "i" "l" "m" "e"))  ; 'kill me'?  That's worrisome.
(define valid-items (set-union line1 line2 line3))

(define (exists a)
  (cond
    [(empty? a) (displayln "you need to enter a starting location")]
    [(not (set-member? valid-items a))
     (displayln "enter a location which exists")]
    [else (displayln "enter your final destintation")]))

(display "empty string: ") (exists "")
(display "foobar: ")       (exists "foobar")
(display "a: ")            (exists "a")

I've changed 'error' to 'displayln' only so that it's easier to see the output without having it stop partway through. Change it back as needed.

(define (exists a) ...) is shorthand for (define exists (λ (a) ...)

(offtopic sidebar): There is another form that can be used for defining functions that generate functions:

; This:

(define ((exists a) b) 
 (list a b)

; is shorthand for this:

(define exists
  (λ (a) 
    (λ (b) 
     (list a b))

; You can see this here:
(exists 7)
((exists 7) 8)