Created
January 6, 2014 14:28
-
-
Save spdegabrielle/8283520 to your computer and use it in GitHub Desktop.
> is there a way to replace all the choices at one whack?
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
On Jan 4, 2014, at 1:30 PM, LA wrote:
Here is how I would do it. It's basically a small library. -- Matthias
[see gist]
Matthias Felleisen via srs.acm.org
5:06 PM (21 hours ago)
to Frank, Racket
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