[racket] problem in working out (contents card)

From: Jon Rafkind (rafkind at cs.utah.edu)
Date: Tue Aug 3 19:41:46 EDT 2010

  It looks like you have defined `contents' twice, maybe thats the core 
of your issue?

On 08/03/2010 05:07 PM, iam_here at aol.in wrote:
>
> 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)))
>
>
> _________________________________________________
>    For list-related administrative tasks:
>    http://lists.racket-lang.org/listinfo/users

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

Posted on the users mailing list.