racket

How can you re-define a constant identifier in DrScheme?

天涯浪子 提交于 2019-12-01 18:03:40
I am using DrScheme to write a Scheme interpreter. I define a Read Eval Print Loop and I am re-defining the eval procedure. This works fine in other scheme implementations like Chez Scheme, but I don't like the code editing in Chez Scheme, so I would like to use DrScheme for this. When I make a definition such as: (define (eval exp env) (cond ...)) It says: define-values: cannot change constant identifier: eval Is there a way to override that and let me change constant identifiers? I'd prefer not to have to rename all my variables to get around this. It turns out there are options per each

Overlapping module imports in Racket

自古美人都是妖i 提交于 2019-12-01 17:34:46
I want to load an image and animate it in Racket. I can do it easily in the Dr. Racket, but I am using Emacs with Geiser. To load the image I need to: (require racket/draw) Next, to draw this image onto the screen, I'm planning to use the big-bang module. To load this module I have to: (require 2thdp/image) But I get this error: module: identifier already imported from: 2htdp/image at: make-pen in: racket/draw errortrace...: This basically means that I can't import the same module twice. But I need both these libraries. How do I avoid this problem? Greg Hendershott When two modules provide

Racket Macro to auto-define functions given a list

自作多情 提交于 2019-12-01 11:01:39
I want to auto-generate a bunch of test functions from a list. The advantage being I can change the list (e.g. by reading in a CSV data table) and the program will auto-generate different tests on the next program execution. For example, say I am trying to identify oxyanions in a string containing a chemical formula . My list may be something like: (define *oxyanion-tests* ; name cation (list (list "aluminate" "Al") (list "borate" "B") (list "gallate" "Ga") (list "germanate" "Ge") (list "phosphate" "P") (list "sulfate" "S") (list "silicate" "Si") (list "titanate" "Ti") (list "vanadate" "V")

Is struct a macro in Racket?

浪子不回头ぞ 提交于 2019-12-01 09:30:44
I remember I read somewhere it is not a macro and is built into the core language. Something like that, I am not sure, because I can no longer remember from where I read it. So is struct a macro in Racket or not? If not, why is it built into the core language? A macro; struct.rkt has (define-syntax (struct stx) (define (config-has-name? config) (cond [(syntax? config) (config-has-name? (syntax-e config))] [(pair? config) (or (eq? (syntax-e (car config)) '#:constructor-name) (eq? (syntax-e (car config)) '#:extra-constructor-name) (config-has-name? (cdr config)))] [else #f])) (with-syntax ([orig

What is ' (apostrophe) in Racket?

帅比萌擦擦* 提交于 2019-12-01 08:35:16
问题 I am a little confused about the meaning of the ' sign in racket. It appears to me that the same sign has different meanings. Look at 2 simple examples below: list Returns a newly allocated list containing the vs as its elements. > (list 1 2 3 4) '(1 2 3 4) quote Produces a constant value corresponding to datum (i.e., the representation of the program fragment) without its lexical information, source location, etc. Quoted pairs, vectors, and boxes are immutable. > '(1 2 3 4) '(1 2 3 4) So my

Racket Macro to auto-define functions given a list

て烟熏妆下的殇ゞ 提交于 2019-12-01 08:12:44
问题 I want to auto-generate a bunch of test functions from a list. The advantage being I can change the list (e.g. by reading in a CSV data table) and the program will auto-generate different tests on the next program execution. For example, say I am trying to identify oxyanions in a string containing a chemical formula. My list may be something like: (define *oxyanion-tests* ; name cation (list (list "aluminate" "Al") (list "borate" "B") (list "gallate" "Ga") (list "germanate" "Ge") (list

How can I create an association list from 2 lists?

我们两清 提交于 2019-12-01 06:45:28
In DrScheme, how can I create an association list from 2 lists? For example, I have, y = ( 1 2 3 ) x = ( a b c ) and I want z = ((a 1) (b 2) (c 3)) Assuming Scheme (since your last 2 questions are on Scheme): (define x '(1 2 3)) (define y '(4 5 6)) (define (zip p q) (map list p q)) ;; <---- (display (zip x y)) ;; ((1 4) (2 5) (3 6)) Result: http://www.ideone.com/DPjeM In C# 4.0 you can do it this way; var numbers = Enumerable.Range(1, 10).ToList<int>(); var abcs = new List<string>() { "A", "B", "C", "D", "E", "F", "G", "H", "I", "J" }; var newList = abcs.Zip(numbers, (abc, number) => string

reverse list - scheme

爷,独闯天下 提交于 2019-12-01 05:35:33
I'm trying to reverse a list, here's my code: (define (reverse list) (if (null? list) list (list (reverse (cdr list)) (car list)))) so if i enter (reverse '(1 2 3 4)), I want it to come out as (4 3 2 1), but right now it's not giving me that. What am I doing wrong and how can I fix it? The natural way to recur over a list is not the best way to solve this problem. Using append , as suggested in the accepted answer pointed by @lancery, is not a good idea either - and anyway if you're learning your way in Scheme it's best if you try to implement the solution yourself, I'll show you what to do,

File I/O operations - scheme

余生长醉 提交于 2019-11-30 21:50:27
Can someone point me to basic file I/O operations examples in Scheme? I just want to try basic read/write/update operations on a file. Finding it difficult as not having appropriate resources to learn from. It's mostly implementation-specific. Given that you're using racket, see the guide section and the reference manual . The easiest way to read/write files in any R5RS compliant Scheme is: ;; Read a text file (call-with-input-file "a.txt" (lambda (input-port) (let loop ((x (read-char input-port))) (if (not (eof-object? x)) (begin (display x) (loop (read-char input-port))))))) ;; Write to a

Type of Define expression in Scheme

与世无争的帅哥 提交于 2019-11-30 20:49:43
To put it simply: My question is whats is the type of a define expression in Scheme? Take for example: (define x 5) or (define x (lambda (n) (* n n))) It's a bit confusing for me. Can anyone help? In Racket define is a special form and not an expression, so it doesn't have a value per-se, if you try to execute something like this you'll get an error: (display (define x 42)) => define: not allowed in an expression context in: (define x 42) If it were to have a value it'd be something akin to void , but that will be dependent on the particular implementation details of the interpreter (I believe