racket

Cartesian product in Scheme

独自空忆成欢 提交于 2019-12-07 04:50:24
问题 I've been trying to do a function that returns the Cartesian Product of n sets,in Dr Scheme,the sets are given as a list of lists,I've been stuck at this all day,I would like a few guidelines as where to start. ----LATER EDIT ----- Here is the solution I came up with,I'm sure that it's not by far the most efficent or neat but I'm only studing Scheme for 3 weeks so be easy on me. 回答1: Here's a concise implementation that is also designed to minimize the size of the resulting structure in

Different kinds of continuations in Racket

﹥>﹥吖頭↗ 提交于 2019-12-07 04:45:06
问题 Can someone give a relatively simple example of the differences in Racket between call-with-composable-continuation and call-with-current-continuation . I've worked through the examples in the Racket Guide 10.3 of call-with-composable-continuation , and the examples of call-with-current-continuation in The Scheme Programming language section 3.3 but I'm not clear on the difference. Could someone give an example where they would give different results in the same context. 回答1: A very thorough

Multidimensional vectors in Scheme?

旧街凉风 提交于 2019-12-07 04:11:29
问题 I earlier asked a question about arrays in scheme (turns out they're called vectors but are basically otherwise the same as you'd expect). Is there an easy way to do multidimensional arrays vectors in PLT Scheme though? For my purposes I'd like to have a procedure called make-multid-vector or something. By the way if this doesn't already exist, I don't need a full code example of how to implement it. If I have to roll this myself I'd appreciate some general direction though. The way I'd

Some Macro terms in Racket

不羁的心 提交于 2019-12-07 01:26:06
问题 I am confused by the terms for a long time, thinking it is good to ask out what exactly do they mean: A. syntax. B. syntax value. C. syntax object. D.s-expression E.datum (in syntax->datum) What's the difference between s-expression and symbol? What's the difference between s-expression and datum? What's the difference between (syntax, syntax values and syntax object) from s-expression? Code examples for explanation will be appreciated. 回答1: "Syntax" is a type for representing source code in

Arbitrary computation in Scheme macro

守給你的承諾、 提交于 2019-12-06 13:16:46
问题 Scheme macros, at least the syntax-case variety, are said to allow arbitrary computation on the code to be transformed. However (both in the general case and in the specific case I'm currently looking at) this requires the computation to be specified in terms of recursive functions. When I try various variants of this, I get e.g. main.scm:32:71: compile: unbound identifier in module (in the transformer environment, which does not include the run-time definition) in: expand-vars (The

Time Code in PLT-Scheme

空扰寡人 提交于 2019-12-06 10:33:10
I want to see how long a function takes to run. What's the easiest way to do this in PLT-Scheme? Ideally I'd want to be able to do something like this: > (define (loopy times) (if (zero? times) 0 (loopy (sub1 times)))) > (loopy 5000000) 0 ;(after about a second) > (timed (loopy 5000000)) Took: 0.93 seconds 0 > It doesn't matter if I'd have to use some other syntax like (timed loopy 5000000) or (timed '(loopy 5000000)) , or if it returns the time taken in a cons or something. The standard name for timing the execution of expressions in most Scheme implementations is "time". Here is an example

Why is it legal in a function definition to make self-call but illegal for a value?

吃可爱长大的小学妹 提交于 2019-12-06 09:37:05
问题 Structure and Interpretation of Computer Programs (SICP) 3.5.2 introduces infinite streams: (define ones (cons-stream 1 ones)) This code doesn't work in DrRacket, with the error: ones: undefined; cannot reference an identifier before its definition Other code like this: (define (integers-starting-from n) (cons-stream n (integers-starting-from (+ n 1)))) (define integers (integers-starting-from 1)) produce error: Interactions disabled (fall in infinite loop?) So far as I read(SICP), the key

Changing a function into CPS style

南楼画角 提交于 2019-12-06 08:33:19
问题 We were asked to write a procedure that when given a list it will replace the first occurrence of a given element and only the first, but the catch is to write in CPS style. We are unable to turn it to CPS style written procedure that is given a success-cont and fail-cont.. If anyone is willing to give it a try we will really appreciate it :] The procedure we have (graciously provided by answers here): (define (replace-one list old new) (cond ((pair? list) (let ((next (replace-one (car list)

How to create a list with n applications of a procedure

泄露秘密 提交于 2019-12-06 08:13:22
My question is related to this one but in my case I would like to obtain a list with the results of n applications of a function whose output is not computable again with the previous result (picking randomly an element from a list, for example). That is, it is not the composition of a function n times with itself but the n results shown together into a list. Something like this? #!racket/base (require srfi/1) (define (times/list proc n) (unfold-right zero? proc sub1 n)) (times/list (lambda (v) (abs (- v 5))) 10) ; ==> (4 3 2 1 0 1 2 3 4 5) (times/list (lambda _ 5) 10) ; ==> (5 5 5 5 5 5 5 5 5

Iterate through the letters of the alphabet in Racket

纵饮孤独 提交于 2019-12-06 07:09:43
问题 I'd like to write a program that iterates through the letters in the alphabet as symbols and does something with them. I'd like it to be roughly equivalent to this C code: for(char letter = 'a'; letter <= 'z'; letter++) { printf("The letter is %c\n", letter); } I really have no idea how to do this in Racket. Thanks for your help. 回答1: Assuming that you only want to iterate over lowercase English alphabet letters, here's one way to do it: (define alphabet (string->list