racket

Convert number to list of digits

删除回忆录丶 提交于 2019-12-18 09:01:38
问题 How do I convert a number to a list of digits? I am currently doing: ;; (num->list 12345) -> '(1 2 3 4 5) (define (num->list n) (local ((define (num->list n) (map (lambda (c) (char->num c)) (string->list (number->string n)))) (define (char->num c) (- (char->integer c) 48))) (num->list n))) but would like to know if there's a better way. 回答1: This is the digits function of my Standard Prelude: (define (digits n . args) (let ((b (if (null? args) 10 (car args)))) (let loop ((n n) (d '())) (if

Racket/Scheme Flatten Explanations

混江龙づ霸主 提交于 2019-12-18 08:48:19
问题 Can someone help me to break down exactly the order of execution for the following versions of flatten? I'm using Racket. version 1, is from racket itself, while version two is a more common? implementation. (define (flatten1 list) (let loop ([l list] [acc null]) (printf "l = ~a acc = ~a\n" l acc) (cond [(null? l) acc] [(pair? l) (loop (car l) (loop (cdr l) acc))] [else (cons l acc)]))) (define (flatten2 l) (printf "l = ~a\n" l) (cond [(null? l) null] [(atom? l) (list l)] [else (append

How do I define functions using Racket macros?

扶醉桌前 提交于 2019-12-18 02:54:39
问题 I am trying to write a macro that defines a special class of data structure with associated functions. I know this is possible; it is done multiple times in the core language itself. As a specific example, how would I define the define-struct macro in Scheme itself. It needs to create make-struct , struct-<<field>> , etc functions. I tried doing this using define , however, this only defines the function in the macro's lexical scope. How can I actually define a function in a macro? 回答1: The

Check if an argument is a list or an atom

狂风中的少年 提交于 2019-12-17 23:18:50
问题 How do I check if something is an atom? I'm looking for something like number? or list? . 回答1: Usually, you'll want to exclude the empty list too: (define (atom? x) (not (or (pair? x) (null? x)))) or, if you want to be more pedantic, then forbid vectors too: (define (atom? x) (not (or (pair? x) (null? x) (vector? x)))) And of course you can add much more here -- since it's marked as a racket question, you might want to add hash tables, structs, etc etc. So it can just as well be easier to

Fibonacci in Scheme

删除回忆录丶 提交于 2019-12-17 21:30:08
问题 I am trying to understand recursion in Scheme and I have a hard time doing the dry run for it, for example a simple Fibonacci number problem. Could someone break down the steps in which the additions take place, for me? (define (fib n) (if (<= n 2) 1 (+ (fib (- n 1)) (fib (- n 2))))) 回答1: If you're using Racket, as your tags indicate, then you have a built-in stepper. Enter the program into DrRacket, and click Step in the top-right menu: Then a stepper window will open up. Click Step over and

Including an external file in racket

喜你入骨 提交于 2019-12-17 19:57:58
问题 I would like to include all the functions defined in a given racket file so that I get the same effect as if they were copied. Is it possible to do that? 回答1: You can use include as follows: Create a file called "foo.rkt" that looks like this: (define x 1) (define y 2) Then in another file: #lang racket (require racket/include) (include "foo.rkt") (+ x y) You should see the result 3 . You can see the documentation for include as well. 回答2: To export the functions out of a module, you use

Collection of Great Applications and Programs using Macros

泪湿孤枕 提交于 2019-12-17 17:28:19
问题 I am very very interested in Macros and just beginning to understand its true power. Please help me collect some great usage of macro systems. So far I have these constructs: Pattern Matching: Andrew Wright and Bruce Duba. Pattern matching for Scheme, 1995 Relations in the spirit of Prolog: Dorai Sitaram. Programming in schelog. http://www.ccs.neu.edu/home/dorai/schelog/schelog.html Daniel P. Friedman, William E. Byrd, and Oleg Kiselyov. The Reasoned Schemer. The MIT Press, July 2005 Matthias

Buffered I/O in Chicken Scheme?

喜欢而已 提交于 2019-12-14 03:46:20
问题 Racket has the nice read-bytes-async! function, which I believe exists in every other programming language in the world. It reads what it can from an input stream, without blocking, into a buffer, returning the number of bytes written. Said function seems like an absolutely essential function for efficiently implementing, say, the Unix cat tool, yet Chicken Scheme seems to lack any such function. Of course, I can use (read-byte) and (write-byte) , but that is slow and eats up all my CPU. Even

How to have ball collide with bricks in Breakout (racket)

◇◆丶佛笑我妖孽 提交于 2019-12-14 02:59:55
问题 I've been trying to get Breakout to work in Racket, so far the ball bounces from the paddle (paddle is controlled by mouse) and the bricks are present Here is the full on code: (require 2htdp/image) (require 2htdp/universe) (define WIDTH 400) (define HEIGHT 400) (define BALL-RADIUS 10) (define BALL-IMG (circle BALL-RADIUS "solid" "red")) (define REC-WIDTH 50) (define REC-HEIGHT 10) (define REC-IMG (rectangle REC-WIDTH REC-HEIGHT "solid" "grey")) (define BRICK-IMG0 (rectangle 60 30 "solid"

Programmatically close a window made with `racket/gui` (to stop a `timer%`)

删除回忆录丶 提交于 2019-12-14 01:42:22
问题 Racket programs that use racket/gui run until all of the windows are closed. This makes it easy to write a program like: #lang racket/gui (define window (new frame% [label "Hello"] [width 100] [height 100])) (send window show #t) And now the program will continue to run until the window is closed. However, it sometimes makes sense to close a window programmatically, for example, if I want a countdown that will close the window and finish after the countdown is done. As far as I can tell