简体   繁体   中英

creating a scheme struct list and functions to use it

 (define-struct groceryItem( id description price))

(define grocery-master-list(list (make-groceryItem 1 "bread" 1.99)
                                 (make-groceryItem 2 "milk" 2.99)
                                 (make-groceryItem 3 "carrots" 3.45)
                                 (make-groceryItem 4 "cookies" 4.55)
                                 (make-groceryItem 5 "sauce" 3.80)
                                 (make-groceryItem 6 "steaks" 8.25)
                                 (make-groceryItem 7 "apples" 5.65)))

(define StructObj (make-groceryItem 1 "bread" 1.99))
(groceryItem-id StructObj)
(groceryItem-description StructObj)
(groceryItem-price StructObj)

I need to create a function called "lookup" that takes an id and a list (which initially should be the list defined above) as parameters and returns the object from the list that matches the id. need to use car and cdr functions. This is what created as the lookup function but its totally wrong...need help

  (define lookup 
     (lambda( id grocery-master-list)
       (if(null? grocery-master-list) '()
          (if (= id ( groceryItem-id (car grocery-master-list))) (groceryItem-description(car grocery-master-list))
             (lookup id (cdr grocery-master-list))
           )
       )
     )
  )

Below, is an example of what the call to the lookup function should look like.

(lookup 3 grocery-master-list)

This is a sort of transcript of systematically constructing the lookup function (intermediate versions are commented out with #; - in practice one would just edit them to the next version)

The design method is HtDF (How to Design Functions): one writes down stub with signature and purpose , examples , template (copied from a library of standard templates). Takes a few minutes to construct a correct solution.


(define-struct groceryItem (id description price))

(define grocery-master-list (list (make-groceryItem 1 "bread" 1.99)
                                  (make-groceryItem 2 "milk" 2.99)
                                  (make-groceryItem 3 "carrots" 3.45)
                                  (make-groceryItem 4 "cookies" 4.55)
                                  (make-groceryItem 5 "sauce" 3.80)
                                  (make-groceryItem 6 "steaks" 8.25)
                                  (make-groceryItem 7 "apples" 5.65)))

#| I need to create a function called "lookup" that takes an id and a list
   (which initially should be the list defined above) as parameters
   and returns the object from the list that matches the id.
   need to use car and cdr functions. |#

(define notFoundGroceryItem (make-groceryItem 0 "" 0.0))

#;                                             ; *stub* ;; *signature*
(define (lookup id logi) ;; GroceryItemId ListOfGroceryItem -> GroceryItem
  ;; produce the item from logi with given id  ; *purpose statement*
  notFoundGroceryItem)                         ; *stub body* (valid result)
                                               ; *minimal example*
(check-expect (lookup 1 empty) notFoundGroceryItem )  ; (passes with above stub)

(define (fn lox) ;; ListOfX -> Y                  ; *template*
  ;; produce a Y from lox using natural recursion ;
  (cond                                           ;
    [(empty? lox) ... ]                           ; ...  = "base case value" ;; Y
    [else (....                                   ; .... = "inventory fn(s)" ;; X Y -> Y
           (first lox) (fn (rest lox))) ]))       ;

; groceryItem-id  ;; GroceryItem -> GroceryItemId           ; *inventory*
; =               ;; GroceryItemId GroceryItemId -> Boolean ;    
; car             ;; ListOfGroceryItem -> GroceryItem       ;
; cdr             ;; ListOfGroceryItem -> ListOfGroceryItem ;

                                  ; (substituting in template)
#;(define (lookup id logi) ;; GroceryItemId ListOfGroceryItem -> GroceryItem
  ;; produce the item from logi with given id
  (cond
    [(empty? logi) ... id ]       ;; GroceryItem
    [else (....
           id
           (car logi)             ;; GroceryItem
           (lookup id (cdr logi)) ;; GroceryItem
           ) ]))
                                  ; (use first check-expect to replace ...)
#;(define (lookup id logi) ;; GroceryItemId ListOfGroceryItem -> GroceryItem
  ;; produce the item from logi with given id
  (cond                           ; (first example still passes with this version)
    [(empty? logi) notFoundGroceryItem ] ;; GroceryItem
    [else (....
           id
           (car logi)
           (lookup id (cdr logi))
           ) ]))

(define grocery-test-list (list (make-groceryItem 1 "bread" 1.99)))

(check-expect (lookup  1 grocery-test-list) (make-groceryItem 1 "bread" 1.99) )
(check-expect (lookup 99 grocery-test-list) notFoundGroceryItem )

                                  ; (use above to deduce ....)
(define (lookup id logi) ;; GroceryItemId ListOfGroceryItem -> GroceryItem
  ;; produce the item from logi with given id
  (cond
    [(empty? logi) notFoundGroceryItem ]
    [else (if (= id (groceryItem-id (car logi)))
              (car logi)
              (lookup id (cdr logi))) ]))

(check-expect (lookup 3 grocery-master-list) (make-groceryItem 3 "carrots" 3.45))


Welcome to DrRacket, version 8.4 [cs].
Language: Beginning Student.
All 4 tests passed!
> 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM