Racket Webserver 显示结构实例

Racket Webserver display struct instance

我正在努力检索/显示提供的实例名称的电子邮件地址。

#lang racket

(struct empInfo(FNAME LNAME IDNO PHONE EMAIL)#:mutable)

(define PersonOne(empInfo "S" "R" 13 "+44" "A@email.com"))
(define PersonTwo(empInfo "H" "I" 31 "+44" "H@email.com"))
(define PersonThree(empInfo "A" "Q" 1 "+44" "S@email.com"))

(require web-server/servlet
 web-server/servlet-env)


(define (response request)
 (define bindings
   (request-bindings request))

(cond
  
  ((exists-binding? `fn  bindings)
 (define AN 
   (extract-binding/single `fn bindings ))
 
; page is generate from here 
 (response/xexpr
  `(html (head (title "title"))
    (body
 
(h1 "title")
     ;(p nbsp, "The Person email address is ", (empInfo-FNAME AN) nbsp) not working expected to 
     (p nbsp, "The Person email address is ", AN nbsp)
     ))
  )
 )
  (else
   (response/xexpr
'(html (head (title "title"))

    (body

(h1 "title")
  (form
   (strong (label "First Name"))
  (input ((name "fn")))
  (input ((type "submit"))),(render-posts)
  ))))
)))


(serve/servlet response
 #:listen-ip #f
 #:port 8080
 #:servlet-path "/rsvr.rkt"
 #:launch-browser? #t)

[![> 使用此行我希望显示提供的字段值

instance but it gives error ;;(p nbsp, "The Person email address is ", (empInfo-FNAME AN) nbsp) not working expected]1]1

此问题与网络服务器无关。

AN 将是名字,而不是结构实例。所以你需要做的是将 AN(empInfo-FNAME PersonOne)(empInfo-FNAME PersonTwo)(empInfo-FNAME PersonThree) 进行比较并显示 (empInfo-EMAIL <that-struct-instance-that-you-are-looking-for>)(假设这是你想要做的) .

但更好的方法是将 PersonOnePersonTwoPersonThree 存储在某种数据库中。在这种情况下,一个列表就足够了。然后,您可以遍历列表以找到结构实例,从而找到电子邮件。

您还需要考虑其他各种问题。如果名字在数据库中不存在怎么办?如果有多个匹配的名字会怎样?

我认为完全删除网络服务器内容并首先解决核心问题会更容易。所以,给定:

(struct empInfo (FNAME LNAME IDNO PHONE EMAIL) #:mutable)

(define database
  (list (empInfo "S" "R" 13 "+44" "A@email.com")
        (empInfo "H" "I" 31 "+44" "H@email.com")
        (empInfo "A" "Q" 1 "+44" "S@email.com")
        (empInfo "S" "Z" 1 "+44" "O@email.com")))

您可能需要编写以下函数:

;; lookup :: string, ListOf[empInfo] -> string or #f
;; lookup consumes a name and a database, 
;; and produces a corresponding email of the first matching person 
;; in the database, or #f if the name doesn't exist in the database.
(define (lookup name database)
  ;; IMPLEMENT THIS
  ...)

(lookup "S" database) ;=> should produce "A@email.com"
(lookup "H" database) ;=> should produce "H@email.com"
(lookup "A" database) ;=> should produce "S@email.com"
(lookup "Q" database) ;=> should produce #f

或者这个函数:

;; lookup-all :: string, ListOf[empInfo] -> ListOf[string]
;; lookup-all consumes a name and a database, 
;; and produces a list of corresponding email of 
;; the matching people in the database
(define (lookup-all name database)
  ;; IMPLEMENT THIS
  ...)

(lookup-all "S" database) ;=> should produce (list "A@email.com" "O@email.com")
(lookup-all "H" database) ;=> should produce (list "H@email.com")
(lookup-all "A" database) ;=> should produce (list "S@email.com")
(lookup-all "Q" database) ;=> should produce (list)

一旦您实现了这些功能之一,您就可以在您的网络服务器应用程序中使用它。

我强烈推荐http://htdp.org/2021-5-4/Book/index.html学习如何设计程序。