Dr Scheme example

mister-fett -1 Tallied Votes 320 Views Share

Since no one has posted in this forum as of yet, I decided to tap out a quick scheme. This program, when run through the Mr Ed part of dr scheme available from www.drscheme.org , will cause a window to pop up with text and a button. By clicking the button, the text changes, so does the window, and so does the location of the window. Does anyone know what Dr Scheme is besides me on this forum? (there have to be some out of 16,000 users!)

(define start (instantiate frame% ("Welcome to the land")(x 50)(y 50)(height 300)(width 600)))
(define line1 (instantiate message% ("This is a game, and you find yourself in a room" start)))
(define line2 (instantiate message% ("in this room are two things: A sword, and a wand." start)))
(define line3 (instantiate message% ("Obvious exits are north and east." start)))
(instantiate button% () (label "Grab Sword")(parent start)
  (callback (lambda (button event)(send start show #f)(send sword1 show #t))))
(instantiate button% () (label "Grab Wand")(parent start)
  (callback (lambda (button event)(send start show #f)(send sword1 show #t))))
(send start show #t)
(define sword1 (instantiate frame% ("This is a test")(x 100)(y 100)(height 500)(width 800)))
(define msg (instantiate message% ("This whole thing was just a test, so I am not going to complete the game! (sorry!)" sword1)))

Dani AI

Generated

This thread began with ’s small DrScheme GUI sketch and later includes two practical requests: asked for decimal→binary conversion and posted an attempted routine for list nesting depth. The two compact, idiomatic Scheme snippets below (portable across PLT/Racket and most R5RS-compatible interpreters with only minor name differences) provide correct behavior and clarify common pitfalls found in the earlier posts.

Decimal → binary (returns a list of bits, MSB first; zero yields '(0); negative inputs signal an error):

(define (decimal->binary n)
  (cond ((< n 0) (error "decimal->binary: negative input"))
        ((= n 0) '(0))
        (else
         (let loop ((q n) (acc '()))
           (if (= q 0)
               acc
               (loop (quotient q 2) (cons (remainder q 2) acc)))))))

(define (binary->string bits)
  (apply string-append (map number->string bits)))

Notes: decimal->binary is tail-recursive and runs in O(log n) time and space (number of bits). The helper binary->string is convenience for display in implementations that provide string-append and number->string.

Corrected list-depth (definition used here: atoms have depth 0; the empty list counts as depth 1; a non-empty list has depth 1 + max depth of its elements):

(define (list-depth x)
  (letrec ((max2 (lambda (a b) (if (> a b) a b)))
           (elem-depth
            (lambda (y)
              (cond ((null? y) 1)
                    ((pair? y) (+ 1 (max-elems y)))
                    (else 0))))
           (max-elems
            (lambda (s)
              (if (null? s) 0
                  (max2 (elem-depth (car s)) (max-elems (cdr s)))))))
    (elem-depth x)))

Clarification: ’s original attempt treated the empty list specially but only recursed on one branch; the corrected version inspects both car and cdr and computes the maximum depth across elements. These routines are small, explicit, and suitable to paste into an old DrScheme/PLT environment (modern Racket may accept them unchanged).

dstern 0 Unverified User

I just joined, and am a scheme programmer. So don't think you are all alone here!

Dani 5,664 The Queen of DaniWeb Administrator Featured Poster

I have some scheme experience as well, from a computer science programming languages course I took. No experience with Dr. Scheme though. Lambda! Lambda! Lambda!

vegaseat 1,735 DaniWeb's Hypocrite Team Colleague

Looks a little like the old goofy LISP!

bexta87 0 Newbie Poster

i'm doing scheme at university... and i need some scheme code to convert a decimal number to binary for an exercise we're doing. does anyone have any ideas?

mister-fett 0 Light Poster

I am now in contact with bexta87.
Does anyone else on this forum know scheme?

Junkbone 0 Newbie Poster

1) It looks like LISP because it is LISP. Scheme is a very minimalist version of Common Lisp, and DrScheme is an implementation with some goodies added, such as the GUI library that mr. Fett is using in that example. So you could say PLT scheme is LISP's cousin.
2) As for bexta87's question (although it's a little late for him to do anything iwht the answer), it would depend on your implementation. I know PLT Scheme has a procedure that would do that for you. For more restrictive implementations, you may have to resort to logarithm rules (log base 2 of x equals ln of x over ln of 2) to figure out what order of magnitude you're talking about, then compare it to the next-lowest power of two, if our number is lower, the bit is a zero, if it's higher, the bit is a one and subtract the power of two from our number. Then repeat the comparision process until you get down to 2^0.

nwest 0 Newbie Poster

i want to take a list's depth i write a code but it is not true can you help about this
(define (E11 lst)
(if (null? lst)
1
(if (list? (car lst))
(max (+ 1 (E11 (car lst))))
(E11 (cdr lst)))))

Rashakil Fol 978 Super Senior Demiposter Team Colleague

Welcome to the internet, nwest.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.