簡體   English   中英

創建方案結構列表和使用它的函數

[英]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)

我需要創建一個名為“lookup”的 function,它將一個 id 和一個列表(最初應該是上面定義的列表)作為參數,並從與 id 匹配的列表中返回 object。 需要使用car和cdr功能。 這是作為查找 function 創建的,但它完全錯誤......需要幫助

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

下面是調用查找 function 的示例。

(lookup 3 grocery-master-list)

這是一種系統地構建lookup function 的抄本(中間版本用#; - 實際上,人們只需將它們編輯到下一個版本)

設計方法是HtDF (How to Design Functions):寫下帶有簽名用途存根示例模板(從標准模板庫中復制)。 構建正確的解決方案需要幾分鍾時間。


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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM