我编写了这段代码,它基本上检查用户是否提交并空了输入(返回和错误),或者输入了不属于三组的字母,如果它成功,它将返回“输入您的最终目的地”。代码中我遇到问题的部分就是这个部分。
((not (and (set-member? line1 a) (set-member? line2 a) (set-member? line3 a)) (error "enter a location which exisits")))我试过用来处理它,但它仍然会给我带来错误。
我试图让球拍检查所有行的联合,如果用户输入,但如果我键入"a“,它会说”进入一个存在的位置“,即使它存在。
这是我的完整代码
(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"))))发布于 2022-02-24 18:02:25
#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)发布于 2022-02-23 18:49:39
当我运行它时,给出的代码确实返回字符串"enter you 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"))
(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其中,(get-location!)接受一个集合、一个提示字符串和一个错误消息,并返回一个有效位置的字符串。
https://stackoverflow.com/questions/71146028
复制相似问题