[racket] syntax, differently

From: Jos Koot (jos.koot at telefonica.net)
Date: Mon Aug 2 10:29:06 EDT 2010

Within 80 columns (not counting the startiong comment.
Hope your mailer does not insert or remove line-breaks.

#lang racket
;; <NOT TELLING OTHERS> : state -> (listof state) ;; given a state, compute
all possible successor states that can be reached in one boat crossing

(define (compute-possible-states state)
  (local
    [;; how-many : symbol bl -> number
     ;; to find out how many s are in a bl
     (define (how-many s bl)
       (cond
         [(empty? bl) 0]
         [else
          (cond
            [(symbol=? s (first bl))
             (+ 1 (how-many s (rest bl)))]
            [else (how-many s (rest bl))])]))
     ;; possible-states accumulate all the possible (an many incorrect)
     ;; future boat loads, given the current state.
     (define (compute-a boat-loads possible-states)
       (cond
         [(empty? boat-loads) possible-states]
         [else
          (local
            [(define num-missionaries (how-many 'M (first boat-loads)))
             (define num-cannibals (how-many 'C (first boat-loads)))
             (define (new-state op1 op2 boat-posn)
               (make-state
                (op1 (state-ml state)
                     num-missionaries)
                (op1 (state-cl state)
                     num-cannibals)
                (op2 (state-mr state)
                     num-missionaries)
                (op2 (state-cr state)
                     num-cannibals)
                boat-posn))
             (define logical-boat-move
               (cond
                 [(symbol=? (state-boat-posn state) 'left)
                  (new-state - + 'right)]
                 [else (new-state + - 'left)]))]
            (compute-a (rest boat-loads)
                       (cons logical-boat-move possible-states)))]))]
    (compute-a BOAT-LOADS empty)))

Jos 

> -----Original Message-----
> From: users-bounces at racket-lang.org 
> [mailto:users-bounces at racket-lang.org] On Behalf Of Horace Dynamite
> Sent: 02 August 2010 16:14
> To: Marco Morazan; users at racket-lang.org
> Subject: Re: [racket] syntax, differently
> 
> On Mon, Aug 2, 2010 at 2:02 PM, Marco Morazan 
> <morazanm at gmail.com> wrote:
> > Perhaps, you should consider making separate functions for 
> each nested 
> > cond and nested local. There is nothing "easy to read" 
> about code with 
> > the shape above. One cond and one local per function is a 
> good rule of 
> > thumb.
> 
> Point taken, perhaps that was a poor example. Let me make 
> clear that I wasn't trying to say that I ran into this 
> 80-column "limit" often working HtDP, but there were 
> occasional times when I felt it was necessary to break this. 
> I've copied one such instance from a HtDP exercise below.
> 
> > This the is the negative side of this, but the positive is 
> that there 
> > are lots of things that computers can help people do when 
> it comes to 
> > learning (eg, executing your program at all!). We should do 
> our best 
> > to exploit the computer's power to deal with mundane things 
> (instead 
> > of putting that burden on instructors/students).
> 
> I do bow down to the people here having a lot more experience 
> with these issues than I have, but I just find this statement 
> a little weird with respect to the whole line-length limit 
> debacle. As well as program execution, DrRacket's Stepper in 
> the early teaching languages was so helpful to me. Of course 
> I wouldn't want to do a hand-evaluation in favor of freeing 
> the burden on the computer.
> 
> But if something pisses you of when you're using the Stepper 
> for example, thats because you don't know why you're program 
> is wrong (e.g.), and now you have to go develop a headache 
> figuring out whats the matter. Whereas if the "auto-magic 
> line-breaker v 1.0" snaps your line when you really don't 
> want it to, that pisses you off for the wrong reasons. It's 
> an arbitrary decision made on your behalf because the 
> computer thinks its smarter than it really is.
> 
> I suppose what I'm butchering here in trying to say is that 
> readers/compilers/debuggers for e.g., while they may be very 
> hairy and complicated to implement, with more than one way to 
> do them, they seem very rigid and omnipotent when you're 
> using them as a novice. Whereas something like line-breaking 
> seems very loose, personal and condition-dependent. Perhaps 
> as a comprise for students who do listen, (and in my class in 
> the UK, most students do! I'm by no means the best at all re. 
> Matthias' comment) have the line breaker default if you must 
> have it all, but on the first couple of runs, there should be 
> a very obvious way to tell it where to go.
> 
> Here is a part of my solution to an exercise in HtDP that 
> breaks the line limit, and I don't think I'm being overly 
> stupid in how I've done it,
> 
> ;; <NOT TELLING OTHERS> : state -> (listof state) ;; given a 
> state, compute all possible successor states that can be 
> reached in ;; one boat crossing (define 
> (compute-possible-states state)
>   (local [;; how-many : symbol bl -> number
>           ;; to find out how many s are in a bl
>           (define (how-many s bl)
>             (cond [(empty? bl) 0]
>                   [else (cond [(symbol=? s (first bl))
>                                (+ 1 (how-many s (rest bl)))]
>                               [else (how-many s (rest bl))])]))
>           ;; possible-states accumulate all the possible (an many
> incorrect) future
>           ;; boat loads, given the current state.
>           (define (compute-a boat-loads possible-states)
>             (cond [(empty? boat-loads) possible-states]
>                   [else (local [(define num-missionaries 
> (how-many 'M (first boat-loads)))
>                                 (define num-cannibals 
> (how-many 'C (first boat-loads)))
>                                 (define (new-state op1 op2 boat-posn)
>                                   (make-state (op1 (state-ml state)
> num-missionaries)
>                                               (op1 (state-cl state)
> num-cannibals)
>                                               (op2 (state-mr state)
> num-missionaries)
>                                               (op2 (state-cr state)
> num-cannibals)
>                                               boat-posn))
>                                 (define logical-boat-move
>                                   (cond [(symbol=? (state-boat-posn
> state) 'left)
>                                          (new-state - + 'right)]
>                                         [else (new-state + - 
> 'left)]))]
>                           (compute-a (rest boat-loads)
>                                      (cons logical-boat-move 
> possible-states)))]))]
>     (compute-a BOAT-LOADS empty)))
> 
> The longest line here is 90 characters, and breaking it would 
> be very annoying. Further, this pattern isn't wacky, 
> considering I'd just read the accumulation section in HtDP.
> 
> Horace.
> _________________________________________________
>   For list-related administrative tasks:
>   http://lists.racket-lang.org/listinfo/users



Posted on the users mailing list.