[racket] problem in working out (contents card)

From: iam_here at aol.in (iam_here at aol.in)
Date: Tue Aug 3 19:07:16 EDT 2010



Hello I have a program consists of two tasks. My first task works fine. In second task I have to create a new chip as soon as the new card has been created.


I am giving you sample run for the idea



==> (define c1 (a-chip))
c1
==> (contents c1)
0
==> (charge c1 10.00)
done
==> (contents c1)
10.0
==> (discharge c1 9.34)
9.34
==> (contents c1)
0.66
==> (discharge c1 0.67)
no











==> (define alan (a-card 432776 1904))
alan
==> (define bill (a-card 1 95))
bill
==> (inserted bill 96)
no
==> (contents bill)                                        // HAVING PROBLEM IN THIS
ERROR: Not inserted
==> (inserted bill 95)
done
==> (contents bill)                     //PROBLEM IN THIS
0
==> (free bill)
done
==> (new-pin bill 96)
no
==> (inserted bill 95)
done
==> (new-pin bill 96)
done
==> (account-number bill)
1
==> (free bill)
done
==> (account-number bill)
1



I AM NOT ABLE TO WORK OUT CONTENTS IN TASK 2..
I have already created contents in task 1. Do I have to create it again.


let me summarise my problem- 
when I write (contents card) it has to work all the three procedures defined in task one contents, charge and discharge.
I have given some efforts but don't worth it.


Any help will be appreciated
thanks


;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;TASK 1;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;


(define (a-chip)


  (let ( (balance 0) )
    (define (the-chip op)
    (sequence
        (cond ((eq? op 'contents)     (lambda () balance))
              ((eq? op 'charge)         charge)
              ((eq? op 'discharge)      discharge)
              (else (error "chip: unknown operation" op))
        )
    'nononon)
    )


    (define (charge amount)
        (set! balance amount)
        'done
        
        
    )


    (define (discharge amount)
       (if (> (- balance amount) 0)
        ( sequence (set! balance (- balance amount))
                
            amount)
        'no
        )
    )


    the-chip
  )
    "mjuu"      


)






(define (charge chip amount)    ((chip 'charge) amount))
(define (discharge chip amount) ((chip 'discharge) amount))


(define (contents chip) ((chip 'contents) ))





;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;TASK 2;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;




(define (a-card account tpin)
    ( let ( (chip #f))
       (define (the-card request)
        (cond ((eq? request 'inserted)      inserted)
              ((eq? request 'free)        free)
              ((eq? request 'new-pin)       new-pin)
              ((eq? request 'account)   ( lambda () account))
                  (else (error "card: unknown operation" request))
        )
)














(define (inserted pin )
    (if (= pin tpin)
    (sequence (set! chip #t)
        
                )
        
    
            'done)      
        'no))


(define (free)
    (sequence (set! chip #f)
        'done)
        )




(define (new-pin new)
    (if (eq? #t chip )
        (sequence (set! tpin new )
            'done)
        'no)
)
        
    














    the-card
    )
   )




(define (contents card)         ((card 'contents)))
(define (pupu card)         ((card 'pupu)))
(define (inserted card pin)     ((card 'inserted ) pin))
(define (free card)         ((card 'free)))
(define (new-pin card new)      ((card 'new-pin) new))
(define (account-number card)       ((card 'account)))





-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.racket-lang.org/users/archive/attachments/20100803/86f63f4e/attachment.html>

Posted on the users mailing list.