racket

Scheme / Racket Vector in Vector transformation

元气小坏坏 提交于 2020-01-05 10:27:21
问题 I'm having a problem transforming a vector like this: #(#(1 2 3)#(1 2 3)#(1 2 3)#(1 2 3)#(1 2 3))) Into one like this: #(#(1 1 1 1 1) #(2 2 2 2 2) #(3 3 3 3 3)) I wrote a piece of test code but the output is wrong. I went into the debugger and I think I know which line of code cause the problem. I can't seems to find a way to make it work. Any help is greatly appreciated. (define (test) (let* ((table #(#(1 2 3)#(1 2 3)#(1 2 3)#(1 2 3)#(1 2 3))) (counter 5) (size 3) (new-table (make-vector

Representing an amount of money with specific bills

笑着哭i 提交于 2020-01-05 07:59:53
问题 I want to write a function in Racket which takes an amount of money and a list of specific bill-values, and then returns a list with the amount of bills used of every type to make the given amount in total. For example (calc 415 (list 100 10 5 2 1)) should return '(4 1 1 0 0) . I tried it this way but this doesn't work :/ I think I haven't fully understood what you can / can't do with set! in Racket, to be honest. (define (calc n xs) (cond ((null? xs) (list)) ((not (pair? xs)) (define y n)

Using Racket to Plot Points

送分小仙女□ 提交于 2020-01-05 05:50:11
问题 After spending some time looking at the documentation, searching the web, and experimenting at the prompt, I haven't succeeded at plotting points using Racket. Could someone post an example of how I'd go about plotting (0 1 2 3 4 5) for my x coordinates and (0 1 4 9 16 25) for the y coordinates. I think 1 good example will clear the problem up. 回答1: Based on the first example of the doc, and given that the function you want to plot already exists in Racket, it's as simple as: (require plot)

How can I unsplice a list of expression into code?

你。 提交于 2020-01-05 05:41:05
问题 I have an experiment for my project, basically, I need to embedded some s-expression into the code and make it run, like this, (define (test lst) (define num 1) (define l (list)) `@lst) ; oh, this is not the right way to go. (define lst `( (define num2 (add1 num)) (displayln num2))) I want the test function be like after test(lst) in racket code: (define (test lst) (define num 1) (define l (list)) (define num2 (add1 num) (displayln num2)) How can I do this in racket? Update The reason I would

Recursive Scheme Function Value Error

女生的网名这么多〃 提交于 2020-01-05 05:00:12
问题 I am writing a small hangman game in scheme and am getting a very weird issue that almost seems like a language specific one. In my game I have a variable that holds the number of mistakes allowed and on each recursive call of my game loop, I "let" the value to a new one if it needs to be changed. Here is some code to help visualize how I run the game loop. guessed_list - a list of string characters containing old guesses and one new guess (ex. '("a" "x" "b") where "a" is the new guess) game

Macro of [S:N] for in-range in Racket

回眸只為那壹抹淺笑 提交于 2020-01-05 03:48:15
问题 How can I create a macro so that S:N or [S:N] returns a range of numbers starting with S and ending with N (step 1). Basically, it should be able to use it in place of 'in-range'. I tried to create something similar to Curly brackets {} to replace 'begin' in Racket but could not. Edit: I tried following as suggested by @soegaard : my-top.rkt: #lang racket (define-syntax-rule (my-top S:N) (range S N) ) (provide (rename-out [my-top #%top])) test.rkt: #lang racket (require "my-top.rkt") (1:42)

What's the return value of `define` in Scheme?

断了今生、忘了曾经 提交于 2020-01-04 13:39:59
问题 I'm curious about the return value of define in Scheme. So I wrote the following lines in Racket #lang r5rs (display (define a 3)) And get the error define: not allowed in an expression context in: (define a 3) I have 2 questions about this: Does it mean that define has no return value? According to R5RS, define is not an expression. It's a program structure. Is it true that only expressions have return values, and other forms don't? 回答1: "If a tree falls in a forest and no one is around to

How can I distinguish scheme dialects in org-babel code blocks?

余生长醉 提交于 2020-01-04 05:24:13
问题 Evaluating this code (C-c C-c): #+begin_src scheme (andmap + '(1 2 3) '(4 5 6)) #+end_src leads to the following babel error: ERROR: Unbound variable: andmap The cause: babel evaluated the code with Guile instead of Racket. How can I tell Babel to execute code using Racket, not Guile? 回答1: http://terohasu.net/blog/2011-09-08-on-racket-support-in-emacs-org-mode.html describes a way: When configuring Emacs to set things up I wasn’t familiar with Babel or any of the solutions for evaluating

Scheme/Racket filter/map multiple arguments

a 夏天 提交于 2020-01-04 05:08:31
问题 Lets say I want to do the following: (define (foo lst x) (filter function lst) but function takes in 2 arguments (and function was given to me), one being the list lst it will use, and the other being x . Syntactically, how would I change that line to pass in the second argument? Sorry I am new to Scheme/DrRacket. 回答1: Try this, using curry: (define (foo lst x) (filter (curry function x) lst)) That is, assuming that function takes as first parameter x and as second parameter each one of the

Difference between append and cons racket

邮差的信 提交于 2020-01-04 04:31:27
问题 I'm trying to understand what the difference is between cons and append in terms of how the lists are being stored underneath. consider the case: (define x '(hi, hello)) (define y '(randomvalue1, randomvalue2)) if I'm assuming that calling (cons x y) on these two lists will generate a dotted pair where the first pointer points to x and the second points to y . all in all neither x or y has been changed. Only thing added memory wise is the dotted pair. However when I call append with the two