[racket] problem in working out (contents card)

From: Todd O'Bryan (toddobryan at gmail.com)
Date: Tue Aug 3 19:32:32 EDT 2010

Two questions:

1. Is this homework? People occasionally get help with homework here,
but if you try to get help without making clear that that's the
situation, people have also been known to track down the teacher who
gave the assignment and send him/her a copy of your message.

2. What are chips, cards, contents, and discharges? Without knowing
the context of what you're trying to do, we can't be sure if you're
playing poker, shopping with a credit card, or powering up an
automatic defibrillator, much less actually help you.

Todd

On Tue, Aug 3, 2010 at 7: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
>


Posted on the users mailing list.