[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: is too an application




Dan, I took a quick look at your code. It now works with the gui.ss
teachpack though I have only done enough to ensure that lookups for
phone numbers work out. My edits are marked with MF. I think I marked
up the whole thing. 

In general, I recommend that you post this kind of message to the teachers
mailing list, and if interested, cross-post to the mzscheme mailing list. 

-- Matthias

; MF: you shouldn't need the following line for HtDP (ever)
;(require-library "functio.ss")

;; Instead: 

;; Author: Dan Anderson, edited by Matthias Felleisen
;; TeachPack: gui.ss from htdp
;; Language Level: Advanced 

;; 35.4.1
;; address book

; (define ab (list))
; MF: changed the above into the more obvious: 
(define ab empty) 
;;
;
;;MODEL:

;; add : symbol number -> void 
;; to add an entry to ab
;; MF: effect? 
(define (add name phone) 
  (set! ab (cons (list name phone) ab)))

(add 'person1 8181234567)
(add 'Charles 8182244002)

;; lookup : symbol (listof (list symbol number)) -> number
;; to lookup the phone number for name in ab
(define (lookup name addb)
  ; MF: added check and second because assf produces a list or false
  (local ((define result (assf (lambda(entry)
                                 (symbol=? name (first entry)))
                               addb)))
    (if (boolean? result)
        0 ; MF : questionable
        (second result))))
        

;; MF: the following test fails the HtDP standards: 
;; where is the expected result? It would have revealed the above bug. 
(lookup 'person1 ab)

;; remove : symbol -> void  
;; to remove an entry from ab
(define (remove name)
  (set! ab (filter (lambda (x) (not(symbol=? name (first x))))ab)))
;(remove 'person1)

;;
;
;; CONTROL: 
;; MF: turned lookup-cb into a function of one argument
(define (lookup-cb x)
  (draw-message response-msg ;; MF: I added the response-msg argument
   (number->string 
    ; MF: added string->symbol because text-contents produces a string
    ; and lookup consumes a symbol
    (lookup (string->symbol (text-contents entry-text)) ab))))

;;
;
;; VIEW:

(define title-msg (make-message "ADDRESS BOOK"))
(define entry-text (make-text "Type name here:"))
(define lookup-button (make-button "Look up name" lookup-cb))
(define response-msg (make-message "##########"))

;; MF: I turned the list of list of symbols into a list of list of GUI elements
(create-window
 (list (list title-msg)
       (list entry-text response-msg)
       (list lookup-button)))