racket

Racket macro to define functions given a repeated pattern

早过忘川 提交于 2019-12-12 13:17:50
问题 The problem is quite difficult to explain because I need to collect my thoughts, so bear with me. I've been able to reduce the problem to a minimal example for illustrative purposes. The example will not make any sense as to what this would be useful for, but I digress. Say I want to extend the racket language to write things that look like this: (define-something (['a] 'whatever) (['b 'c] 'whatever2)) Between the square brackets is a sequence of one or more symbols, followed by a sequence of

Add a character to a frequency list

余生长醉 提交于 2019-12-12 13:16:57
问题 I have a project about huffman coding, and I am stuck, I don't understand why my code is not working. This is the exercise: Write a function add1 which, given a character, adds 1 to its frequency in a frequency list. If the character is not yet in the list of frequencies, it is added. (add1 "e" '(("l" 1) ("e" 2) ("x" 1))) → (("l" 1) ("e" 3) ("x" 1)) (add1 "e" '(("a" 4) ("b" 3))) → (("a" 4) ("b" 3) ("e" 1)) What I wrote: (define add1 (lambda (c l) (if (null? l) '() (if (member? c l) (if (equal

Why is (begin) valid in Scheme?

試著忘記壹切 提交于 2019-12-12 12:17:15
问题 I have tested in Racket and Chez Scheme and found (begin) is acceptable while (define a (begin)) is not. For example with Racket I got > (begin) > (define a (begin)) ; stdin:56:10: begin: empty form not allowed And my question is why is (begin) allowed at all? Is there any specific reason/intuition for this? 回答1: The form begin has two purposes. 1. To sequence the evaluation of expressions 2. To "splice" sequences together (used by macros) The first one is what is used most often: (begin e0

Implementation of variadic map function in Scheme

笑着哭i 提交于 2019-12-12 11:33:58
问题 As you can see in the example below, map function in Scheme is variadic function. > (map (lambda (number1 number2) (+ number1 number2)) '(1 2 3 4) '(10 100 1000 10000)) '(11 102 1003 10004) I want to implement this variadic option, but I only succeeded to find the two arguments map implementation: (define (map f lst) (if (null? lst) '() (cons (f (car lst)) (map f (cdr lst))))) Can someone help me implement variadic map function? 回答1: In Scheme, you can write a variadic function as (lambda x .

Multiply without + or *

丶灬走出姿态 提交于 2019-12-12 10:52:54
问题 I'm working my way through How to Design Programs on my own. I haven't quite grasped complex linear recursion, so I need a little help. The problem: Define multiply , which consumes two natural numbers, n and x , and produces n * x without using Scheme's * . Eliminate + from this definition, too. Straightforward with the + sign: (define (multiply n m) (cond [(zero? m) 0] [else (+ n (multiply n (sub1 m)))])) (= (multiply 3 3) 9) I know to use add1 , but I can't it the recursion right. Thanks.

What is “3D syntax”?

回眸只為那壹抹淺笑 提交于 2019-12-12 10:38:13
问题 In the context of writing Racket macros, what does "3D syntax" mean? I've heard the phrase a few times. Including once in reference to a macro I was writing. But that was awhile ago; I fixed it, and now I can't remember exactly what I was doing wrong originally. Also: Is 3D syntax always bad? Or is it like eval (where if you think you need to use it, you're probably wrong, but there are some valid uses in expert hands)? 回答1: Syntax objects are usually supposed to be just serializable data. 3D

How to 'display' multiple parameters in R5RS Scheme

╄→尐↘猪︶ㄣ 提交于 2019-12-12 08:59:23
问题 In R5RS Scheme how do you display multiple parameters, with a single call? my implementation below works, but adds extra parentheses and spaces. #!/usr/bin/env racket #lang r5rs (define (display-all . rest) (display rest)) (display-all "I " "have " "a " "lovely " "bunch " "of " "coconuts\n") results in owner@K53TA:~$ ./Template.ss (I have a lovely bunch of coconuts ) 回答1: Simplest: (define (display-all . vs) (for-each display vs)) Note the use of for-each instead of map - for-each is the same

why am I getting “application not a procedure”?

久未见 提交于 2019-12-12 06:58:49
问题 I am trying to write a procedure that computes f by means of an iteratve process. The function f is defined by the rule that f(n) = n, if n < 4 and f(n) = f(n - 1) + 2f(n - 2) + 3f(n - 3) + 4f(n - 4), if n >= 4. Here is my procedure: (define (f n) (define (newF temp n) (letrec ((first (- n 1)) (second (- n 2)) (third/fourth (- n 3)) (fifth (- n 4))) (define (d) ((if (< first 4) (set! temp (+ temp first)) (newF temp first)) (if (< second 4) (set! temp (+ temp (* second 2))) (newF temp second))

How to make eval work on define?

风格不统一 提交于 2019-12-12 05:49:18
问题 I posted some other easy code, to clarify what's happening When I use eval on the following code #lang racket (define (test ) `( (define num 1) (define l (list)) (define num2 (add1 num)) (displayln num2))) (eval (test) (make-base-namespace)) racket screams at me define-values: not in a definition context in: (define-values (num) 1) My questions are: How to make eval work on definition? If eval is not designed to work on definitions, then is there some workarounds that can make it work? I

Scheme rewrite let* as nested unary lets

China☆狼群 提交于 2019-12-12 05:29:01
问题 I have written a function match-rewriter that is essentially match-lambda except that it returns its argument if no match is found: (define-syntax match-rewriter (syntax-rules () ((_ (patt body) ...) (λ (x) (match x (patt body) ... (_ x)))))) Now I would like to use match-rewriter to take strings representing source code for let* and rewrite it as nested unary lets : (define let*→nested-unary-lets (match-rewriter (`(let*((,<var> ,<val>) ...) ,<expr1> ,<expr2> ...) I am really stumped over how