racket

Recursive Function Composition in Scheme

泪湿孤枕 提交于 2019-12-10 20:02:06
问题 Below is an attempt I've made to create a procedure that returns the function composition given a list of functions in scheme. I've reached an impasse; What I've written tried makes sense on paper but I don't see where I am going wrong, can anyone give some tips? ; (compose-all-rec fs) -> procedure ; fs: listof procedure ; return the function composition of all functions in fs: ; if fs = (f0 f1 ... fN), the result is f0(f1(...(fN(x))...)) ; implement this procedure recursively (define compose

Saving all random numbers generated by the program then reusing them in racket

妖精的绣舞 提交于 2019-12-10 19:17:19
问题 I have a project making calls to random numbers, for example: I have calls that look like this: (random) to generate numbers between 0 and 1, also calls like this: (random n) , to generate numbers within range. What I want to do is put all the random numbers generated throughout the program in a file. I have this code: (require (rename-in racket [random random0])) (define random-port (open-output-file "random-numbers.rktl" #:exists 'replace)) (define (random x) (define y (random0 x))

decent way of nested definition in scheme

≡放荡痞女 提交于 2019-12-10 18:53:53
问题 I want to define a constant foo using an auxiliary function, say, bar . And I want to hide bar inside the definition of foo , so I come with this code: (define foo (define (bar n) (+ n n)) (bar 1)) However, this definition causes syntax errors in many scheme implementations(mit-scheme, racket, guile, etc.). I have three workarounds but none of them seems satisfactory: (define foo1 ((lambda () (define (bar n) (+ n n)) (bar 1)))) (define foo2 (let ((bar (lambda (n) (+ n n)))) (bar 1))) (define

A splicing syntax class that matches an optional pattern and binds attributes

橙三吉。 提交于 2019-12-10 18:42:34
问题 A splicing syntax class that I have is defined as follows. The syntax class matches a sequence of two statements (first pattern), one of the statements (third and second patterns) and perhaps even none of those statements at all (last pattern). As you can see there is quite a lot of "duplicate" code, because every pattern returns either the attributes of something captured in the pattern, or an empty thing otherwise. The problem I have is that currently the statement is never truly optional,

calling other functions in macros

被刻印的时光 ゝ 提交于 2019-12-10 17:25:53
问题 How can I call other functions/macros within a macro? The following doesn't seem to work (even if I define bar with define-syntax ) (define (bar) #'"hello") (define-syntax (foo stx) (syntax-case stx () [(_ '(a b)) (bar)])) 回答1: Racket’s macro system maintains careful separation between runtime and compile-time code. You defined bar as a runtime function, but you actually want to use it in a macro. Therefore, you need to explicitly define bar at compile-time by wrapping it with begin-for

local in racket

一笑奈何 提交于 2019-12-10 16:59:10
问题 I am reading about local definitions in the book, and I came across this example- (local ((define (f x) (+ x 5)) (define (g alon) (cond [(empty? alon) empty] [else (cons (f (first alon)) (g (rest alon)))]))) (g (list 1 2 3))) what exactly does local do here? 回答1: local is documented either in here as part of one of the HtDP languages or in here as part of the local module. Let's see each one in turn. First the one in HtDP: (local [definition ...] expression) Groups related definitions for use

Racket accumulator list function

早过忘川 提交于 2019-12-10 16:11:36
问题 I'm working on a specific step in creating the 2048 game which you may have played. It's on a bunch of websites online. Basically all this function does is: 1) All blank spaces move to the back and 2) if the first two numbers are equal then it doubles and checks every two numbers These are the instructions for the step I'm stuck on: Design a slide-left function so that it runs a single pass over the given row using an APS (accumulator passing style) helper. You will need to maintain two

plt-redex: capture-avoiding substitution for free?

China☆狼群 提交于 2019-12-10 15:51:33
问题 Every time I define a language in PLT redex, I need to manually define a (capture-avoiding) substitution function. For example, this model isn't finished because subst isn't defined: #lang racket/base (require redex/reduction-semantics) (define-language Λ [V ::= x (λ x M)] [M ::= (M M) V] [C ::= hole (V C) (C M)] [x ::= variable-not-otherwise-mentioned]) (define -->β (reduction-relation Λ [--> (in-hole C ((λ x M) V)) (in-hole C (subst M x V))])) But the definition of subst is obvious. Can PLT

Static analyzer for functional programming languages, e.g.Scheme

我只是一个虾纸丫 提交于 2019-12-10 15:43:12
问题 I seldom see static analyzer for functional programming languages, like Racket/Scheme, I even doubt that whether there are any. I would like to write a static analyzer for functional languages, say Scheme/Racket. How should I go about it? 回答1: First read this paper by Shivers, explaining why there is no static control flow graph available in Scheme. Might implemented k-CFA in Scheme. Matt Might's site and blog is a good starting point for exploring static analysis of higher-order languages. I

Curly brackets {} to replace 'begin' in Racket

拈花ヽ惹草 提交于 2019-12-10 15:23:24
问题 Is it possible to have a macro to use curly brackets {} to indicate a block of statments, so as to replace the 'begin' keyword. Hence, instead of: (if (condition) (begin (statement1) (statement2) (statement3) (statement4)) (else-statement)) we may use: (if (condition) { (statement1) (statement2) (statement3) (statement4) } (else-statement)) How can this be accomplished? Thanks for your answers. 回答1: This is completely possible, and there are several ways to do it. (Quick note before I start,