[racket] choice% - replace all choices

From: Matthias Felleisen (matthias at ccs.neu.edu)
Date: Sun Jan 5 12:06:20 EST 2014

Hi Frank, and everyone else, 

The snippet I posted is not C++ with parentheses: 
 -- the function main encapsulates the essence (and I prefer to do so to test at the REPL not while drracket evaluates the def area)
 -- the auxiliary functions are mostly functional 
 -- even though they instantiate classes (which could be hidden under a functional veneer) 
Furthermore, the program has only one kind of  side-effect, namely, changing f's children so that just the desired menu is visible (and everything else you might wish can be pulled back in). 

While The Little Schemer introduces the language (Lisp, Scheme, Racket) via functional programming, keep in mind that there is also The Seasoned Schemer, which introduces side-effects as ways to elegantly express change of state across component boundaries, when and if needed. 

In general, the point of Racket is that you can choose your programming style. For the most part, you can stay as functional as you want, as untyped or as (conventionally) typed), as safe or as unsafe, as lazy or as strict, as (class-based) object-oriented as you want, and as logical as you want. Best of all, you can integrate these programming styles via relatively smooth transition, best illustrated with the typed-unttyped boundary. [I intend to explore other boundaries in a similar fashion.] The best way to describe this way of programming is 'full spectrum' and we are proud that we can offer this ideas. 

Best of all -- as you re-discovered -- you can also create new languages that fit your style/needs/domain even better than any of the ones that exist. 

That's what Racket is all about. -- Matthias







On Jan 5, 2014, at 8:08 AM, Frank Weytjens wrote:

> Hello again,
> 
> I dived into the racket reference to find an answer myself. Chapter 5: Classes and Objects. I knew about it. Just forgot about it. After reading chapter 5 you are supposed to think like an object-oriented programmer when it comes to windowing. Okay, since I hear no remarks on this. I think I stand alone with my blame of an identity problem in Scheme. It is THE language to write other languages. And that is what they did. They mimic C++, or better, incorporate all advantages of object oriented programing in this functional programing language. Hard for a turtle to accept that his favourite program language, that is so proud of being easy to learn, just absorbs all goodies from an other language and so become less clean. I guess I will have to learn to live with it. Better kept quiet from the start. I knew my question was leading to nowhere from the begin. Sorry, for disturbing you.
> 
> Frank
> 
> Op 5 jan. 2014 13:14 schreef "Frank Weytjens" <fweytjens.android at gmail.com> het volgende:
> Hello Mathias,
> 
> I'm a turtle walking trough SchemeLand. Still chewing The Little Schemer on a regular basis. Advising every one to read it. Never found one person that believed this book could be so interesting. I like to see your programs. Always clean. I can almost understand this program on the fly. Only thunk was new for me. Why do I react on this post. Just for a silly remark. This program looks like it was made by a C programmer. This (main) ;) But not only a C programmer. Also one with knowledge of C++ This new function does not look very Scheme to me.  Why  they chose for new and not straight away frame with some parameters? It kind of disappoint me, my beautiful functional language has been sleeping with procedural and object orientated languages. Looks like it has some identity problems. Maybe i'm wrong. Would like to hear your opinion on it.
> 
> Thank you,
> 
> Frank
> 
> Op 4 jan. 2014 23:49 schreef "Matthias Felleisen" <matthias at ccs.neu.edu> het volgende:
> 
> On Jan 4, 2014, at 1:30 PM, lothar atheling wrote:
> 
> > hello,
> >
> > is there a way to replace all the choices at one whack?
> 
> Here is how I would do it. It's basically a small library. -- Matthias
> 
> 
> 
> #lang racket/gui
> 
> (define (main)
>   (define f (new frame% [label "testing choice replacement"][width 100][height 200]))
>   ;; String [List-of String] String [-> (instance-of Choice%)] -> (instance-of Choice%)
>   ;; add a choice% object to f with selections los and special switch entry s at bottom
>   ;; if this special choice is made, switch to (next)
>   (define (choice lbl los s next)
>     (new choice%
>          [label lbl]
>          [choices (append los (list "" s))]
>          [parent f]
>          [callback (lambda (c e)
>                      (define chosen (send c get-string-selection))
>                      (cond
>                        [(string=? s chosen)
>                         (send f begin-container-sequence)
>                         (send f change-children (all-but (next)))
>                         (send f end-container-sequence)]
>                        [else
>                         (displayln chosen)]))]))
> 
>   ;; --- some instances of choice%
>   (define c
>     (choice "choose character"
>             `("darth vadar" "luke skywalker" "princess leia" "han solo" "chuwaka")
>             "chose tea instead"
>             (thunk d)))
> 
>   (define d
>     (choice "chose teas"
>             `("green" "black" "vanilla")
>             "chose character instead"
>             (thunk c)))
> 
>   ;; --- set up f now and show
> 
>   (send f change-children (all-but c))
>   (send f show #t))
> 
> ;; X -> [List-of X] -> (cons X [List-of X])
> ;; create a function that adds c to a given list and removes all others
> ;; NOTE: this is more complex than needed here to show how this could be generalized.
> (define (all-but c)
>   (lambda (children)
>     (define x (filter (lambda (d) (eq? c d)) children))
>     (if (member c x) x (cons c x))))
> 
> (main)
> 
> 
> ____________________
>   Racket Users list:
>   http://lists.racket-lang.org/users

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

Posted on the users mailing list.