OCT-DEVELOPERS
OCT-DEVELOPERS

Reputation: 27

Racket Webserver display struct instance

i am trying hard to retrive / display email address of provided instance name.

#lang racket

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

(define PersonOne(empInfo "S" "R" 13 "+44" "[email protected]"))
(define PersonTwo(empInfo "H" "I" 31 "+44" "[email protected]"))
(define PersonThree(empInfo "A" "Q" 1 "+44" "[email protected]"))

(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)

[![> using this line i am expective to display field value of provided

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

Upvotes: 0

Views: 70

Answers (1)

Sorawee Porncharoenwase
Sorawee Porncharoenwase

Reputation: 6502

This problem is not really related to webserver.

AN will be a first name, not a struct instance. So what you need to do is to compare AN against (empInfo-FNAME PersonOne), (empInfo-FNAME PersonTwo), (empInfo-FNAME PersonThree) and display (empInfo-EMAIL <that-struct-instance-that-you-are-looking-for>) (assuming that this is what you want to do).

But a better way is to store PersonOne, PersonTwo, PersonThree in some sort of database. In this case, a list would suffice. Then, you can iterate through the list to find the struct instance and thus the email.

There are other various problems that you need to consider. What should happen if the first name doesn't exist in the database? What should happen if there are multiple matching first name?

I think it would be easier to remove webserver stuff entirely and tackle the core problem first. So, given:

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

(define database
  (list (empInfo "S" "R" 13 "+44" "[email protected]")
        (empInfo "H" "I" 31 "+44" "[email protected]")
        (empInfo "A" "Q" 1 "+44" "[email protected]")
        (empInfo "S" "Z" 1 "+44" "[email protected]")))

You might want to write the following function:

;; 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 "[email protected]"
(lookup "H" database) ;=> should produce "[email protected]"
(lookup "A" database) ;=> should produce "[email protected]"
(lookup "Q" database) ;=> should produce #f

Or this function:

;; 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 "[email protected]" "[email protected]")
(lookup-all "H" database) ;=> should produce (list "[email protected]")
(lookup-all "A" database) ;=> should produce (list "[email protected]")
(lookup-all "Q" database) ;=> should produce (list)

Once you implement one of these functions, you can then use it in your webserver application.

I highly recommend http://htdp.org/2021-5-4/Book/index.html to learn how to design programs.

Upvotes: 1

Related Questions