'How can i use set-union to check all sets (racket)
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"))))
Solution 1:[1]
#lang racket
(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 locations (set-union line1 line2 line3))
(define (read-location prompt) ;; String -> String | #f
;; produce valid location, or #f on eof
(let read-and-validate ()
(display prompt)
(let ([input (read-line)])
(cond
[(eof-object? input) #f]
[(not (set-member? locations input))
(for-each display
`("location must be one of "
,(map string->symbol
(sort (set->list locations) string<?))
"\n"))
(read-and-validate)]
[else input]))))
(define (route)
;; (example of use of read-location)
(let* ([start (read-location "starting location: ")]
[destination (or (not start) (read-location " destination: "))])
(if (and start destination)
(for-each display `("route is " ,start " to " ,destination))
(display "\nno start or destination"))))
(route)
Solution 2:[2]
When I ran it, the code given does return the string "enter you final destination", which may or may not be what you intended.
I have tested the following code, and I believe it has the effect you want:
(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 unified-transit-system (set-union line1 line2 line3))
(define get-location!
(? (set query-message err-message)
"Read in a location value and validate it,
repeating the input request until it gets a valid value"
(display query-message)
(let ((location (read-string 1)))
(read-string 1) ; read again to discard the dangling newline
(if (set-member? set location)
location
(begin
(display err-message)
(newline)
(get-location! set query-message err-message))))))
(define starting-point (get-location! unified-transit-system
"Enter the starting location: "
"Please enter a valid starting location"))
(define destination (get-location! unified-transit-system
"Enter the destination: "
"Please enter a valid destination"))
starting-point
destination
Where (get-location!) takes a set, a prompt string and an error message, and returns a string for a valid location.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | mnemenaut |
| Solution 2 |
