racket

How can I create an association list from 2 lists?

若如初见. 提交于 2019-12-19 09:05:42
问题 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)) 回答1: 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 回答2: In C# 4.0 you can do it this way; var numbers = Enumerable.Range(1, 10).ToList<int>(); var abcs = new List<string>()

reverse list - scheme

徘徊边缘 提交于 2019-12-19 07:55:35
问题 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? 回答1: 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

Capturing Macros in Scheme

我的梦境 提交于 2019-12-19 07:52:19
问题 What's the simplest way to define a capturing macro using define-syntax or define-syntax-rule in Racket? As a concrete example, here's the trivial aif in a CL-style macro system. (defmacro aif (test if-true &optional if-false) `(let ((it ,test)) (if it ,if-true ,if-false))) The idea is that it will be bound to the result of test in the if-true and if-false clauses. The naive transliteration (minus optional alternative) is (define-syntax-rule (aif test if-true if-false) (let ((it test)) (if it

Racket Interactive vs Compiled Performance

梦想与她 提交于 2019-12-19 05:04:47
问题 Whether or not I compile a Racket program seems to make no difference to the runtime performance. Is it just the loading of the file initially that is improved by compilation? In other words, does running racket src.rkt do a jit compilation on the fly, which is why I see no difference in compiling vs interactive? Even for tight loops of integer arithmetic, where I thought some difference would occur, the profile times are equivalent whether or not I previously did a raco make . Am I missing

File I/O operations - scheme

不想你离开。 提交于 2019-12-19 03:20:49
问题 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. 回答1: It's mostly implementation-specific. Given that you're using racket, see the guide section and the reference manual. 回答2: 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

Mysterious Racket error: define: unbound identifier; also, no #%app syntax transformer is bound in: define

落爺英雄遲暮 提交于 2019-12-18 15:54:12
问题 This program produces an error: define: unbound identifier; also, no #%app syntax transformer is bound in: define When pasted into the REPL (to be exact, the last line: (displayln (eval-clause clause state ))), it works. When run in definition window, it fails. I don't know why. #lang racket (define *state* '((a false) (b true) (c true) (d false))) (define *clause* '(a (not b) c)) (define (eval-clause clause state) (for ([x state]) (eval `(define ,(first x) ,(second x)))) (eval (cons 'or (map

How to make a GUI using Lisp: DrScheme or Common Lisp

≯℡__Kan透↙ 提交于 2019-12-18 12:23:55
问题 Or the basic work need to do to create a GUI. I know the basic Components of GUI, but where to begin. I'm just a self-study person and I'm reading "How to Design Program" (HtDP) at the end of the book the author suggest that knowledge of GUI and CGI computer network are needed to be a programmer. The information of the last two is easy to find. But it seems there are little book talking about how to create a GUI. I guess maybe it's too "low" in the process designing the computer program that

Running SICP Pattern Matching Rule Based Substitution Code

蓝咒 提交于 2019-12-18 11:53:24
问题 I have found the code from this lesson online (http://groups.csail.mit.edu/mac/ftpdir/6.001-fall91/ps4/matcher-from-lecture.scm), and I am having a heck of a time trying to debug it. The code looks pretty comparable to what Sussman has written: ;;; Scheme code from the Pattern Matcher lecture ;; Pattern Matching and Simplification (define (match pattern expression dictionary) (cond ((eq? dictionary 'failed) 'failed) ((atom? pattern) (if (atom? expression) (if (eq? pattern expression)

Which language in DrScheme for SICP?

江枫思渺然 提交于 2019-12-18 11:51:52
问题 I have been using the Module for SICP in DrScheme 4.2 but which language has the best support for SICP in DrScheme? Has anyone here tried this? Thanks. 回答1: I don't think you need anything but R5RS which is available in DrScheme via Language > Choose Language... . You might want to allow redefinition of bindings. After you have selected R5RS, click on " Show Details " and uncheck " Disallow redefinition of initial bindings ". Some places in the text uses an error function, which is not

Counting elements of a list and sublists

旧巷老猫 提交于 2019-12-18 09:29:16
问题 I'm trying to create a function to count all the elements in a list, including the elements of its sublists. initially, to get started, i came up with a basic function myList : (define myLength (lambda (L) (cond ((null? L) 0) (else (+ 1 (myLength (cdr L))))))) However, it doesn't help me account for function calls like: (numAtoms '()) "...should be 0" (numAtoms '(())) "...should be 0" (numAtoms '(1 1)) "...should be 2" (numAtoms '(1 (1 1) 1)) "...should be 4" (numAtoms '(1 (1 (1 1)) 1)) "..