racket

convert a list of data structure in string, scheme

喜欢而已 提交于 2019-12-25 07:39:20
问题 for example I have this structure: (define-struct example (n1 n2)) and I have this list: (list (make-example 1 3) (make-example 7 9) empty) As I can convert it into string? 回答1: Since you have tagged both racket and scheme (two incompatible languages) I've ignored Scheme completely in my answer. I assume you would not tag racket if you were programming in #!r5rs or #!r6rs. #!racket (define (any->string any) (with-output-to-string (lambda () (write any)))) (any->string '(Hello world)) ; ==> "

Scheme error “except: misuse of unit import keyword”

拜拜、爱过 提交于 2019-12-25 06:51:09
问题 I'm writing a function that returns elements which appear in one list and not in another. For example, (except '(a b c) '(a d b e f)) will return '(c) . The first argument can be an atom, and both are assumed to be flat. Here's my code: (define (except lm ln) (cond ((null? ln) lm) ((not (list? lm)) (cond ((in? lm ln) '()) (#t lm))) ((null? lm) '()) ((in? (car lm) ln) (except (cdr lm) ln)) (#t (cons (car lm) (except (cdr lm) ln))))) Then an error returns saying "except: misuse of unit import

Racket string to literal?

六月ゝ 毕业季﹏ 提交于 2019-12-25 02:48:13
问题 I'm doing a project where I'm parsing JSON from the Riot Games API, and I'm having trouble. I'm pretty new to this, so bear with me: The API returns JSON, for example: {"forcinit":{"id":35979437,"name":"F O R C I N it","profileIconId":576,"summonerLevel":30,"revisionDate":1427753158000}} in my code, I have the following: #lang racket (require racket/gui/base net/url json racket/format ) ; --- Query API (define api-request "https://na.api.pvp.net/api/lol/na/v1.4/summoner/by-name/SUMMONER_NAME

Injecting syntax at compile time using Racket's syntax parameters?

浪尽此生 提交于 2019-12-24 22:03:43
问题 I'm trying to use syntax parameters in order to inject new syntax where I need it to be injected. The result of this is then used in other syntax. However, it's not working as I expect it to. Here's a minimal working example: #lang racket (require (for-syntax racket/contract)) (require racket/stxparam) ;; A list for holding the instructions (define instructions-db '()) ;=================================== ; MACRO FOR DEFINING AN INSTRUCTION ;=================================== (provide define

How do I combine two foldr/ foldl functions for this output? (Racket/Scheme)

99封情书 提交于 2019-12-24 19:24:25
问题 I am quite new to racket, and I wondered if there is a way to combine two foldr` functions: '(foldr + 0 (list 1 2 3 4)) ;; output = 10 -> (4+(3+(2+(1+0)))) (foldr * 1 (list 1 2 3 4)) ;; output = 24 -> (4*(3*(2*(1*0))))' I want to receive this output : output = 64 -> (4+4∗(3+3∗(2+2∗(1+1∗0)))) 回答1: #| (4*(3*(2*(1*0)))) -> 0 (not 24) (4+(3+(2+(1+0)))) -> 10 (foldr + 0 (list 1 2 3 4)) -> (+ 1 (+ 2 (+ 3 (+ 4 0)))) -> 10 (foldr * 1 (list 1 2 3 4)) -> (* 1 (* 2 (* 3 (* 4 1)))) -> 24 if you want this

How can I import a function from a file in racket?

对着背影说爱祢 提交于 2019-12-24 16:39:50
问题 I'm sure this is am extremely basic question, but I have two files and I want to import one function from that file into the other one. Is there a way to do so? 回答1: You can use provide within a module to export definitions to other modules that import it with require : ;; a.rkt (provide f) (define (f x) (displayln (add1 x))) ;; b.rkt (require "a.rkt") (f 3) ; => 4 For more information, see the docs. 来源: https://stackoverflow.com/questions/34756477/how-can-i-import-a-function-from-a-file-in

Scheme/Racket: most idiomatic way to append single element to end of list

浪尽此生 提交于 2019-12-24 16:22:23
问题 I want to append the element b to the list a (let's say (a1, a2, ... an) ), e.g. appending the number 3 to (1 2) gives (1 2 3) So far I've been doing (append a (list b)) , which is kind of long and inelegant, so I wonder if there's a "better" way... 回答1: Are you building a list piecemeal, an item at a time? If so, the idiomatic way to do this is to build the list backward, using cons , and then reversing the final result: (define (map-using-cons-and-reverse f lst) (let loop ((result '())

Scheme Function (DrRacket)

喜夏-厌秋 提交于 2019-12-24 15:53:31
问题 So, i'm trying to write the following function in scheme, and to be able to run it on DrRacket. The problem is as follows, make5 - takes two integers, and returns a 5-digit integer constructed of the rightmost 3 digits of the first input, and the leftmost 2 digits of the second input. For example, (make5 561432 254) would return 43225. Negative signs on either input number should be ignored - that is, (make5 561432 -254) would also return 43225. If the first number has less than three digits

Insert node key and value in binary search tree using scheme

こ雲淡風輕ζ 提交于 2019-12-24 13:33:46
问题 I am inserting a key key and a value val into the tree-map t returning a new tree with a node containing the key and the value in the appropriate location. I have this defined: (define (tree-node key value left right)(list key value left right)) (define (get-key tn) (node_key tn)) (define (get-val tn) (node_val tn)) (define (get-left tn) (node_left tn)) (define (get-right tn) (node_right tn)) (define (node_key tn) (list-ref tn 0)) (define (node_val tn) (list-ref tn 1)) (define (node_left tn)

Determin if a racket program is in a sandbox

别等时光非礼了梦想. 提交于 2019-12-24 13:02:53
问题 Is it possible to determine if a Racket program is being run in a sandbox? The reason I ask is because I have a Racket macro that creates a file. And the DrRacket background expander prevents a file from being created (as it should). However, in doing so, it causes an error to appear at the bottom of the window saying the file could not be created. So, I would like to determine if I am in a sandbox, and if I am, don't create the file, and kindly finish up the macro. 回答1: In general, you