racket

Operations using list elements

最后都变了- 提交于 2019-12-11 14:03:30
问题 I have a list in this format, (+ 2 3). Where the first character is a math symbol that can be applied to the other two elements. I cannot seem to get it to do the operations. I want to return 5 for the previous example. I've tried this: ((car '(+ 2 3)) (cadr '(+ 2 3)) (caddr '(+ 2 3))) But I get the following error: application: not a procedure. 回答1: You can try eval , should do it straight away: > (eval '(+ 1 2)) 3 If you'd like to have more control over the input, write a funcion: (define

“not a proper list” error in DrRacket writing Scheme

孤者浪人 提交于 2019-12-11 13:59:04
问题 I just follow the instructions at 3.3.3 of SICP to create the table. The code I wrote just works well. here is code_0.scm: #lang scheme (require rnrs/base-6) (require rnrs/mutable-pairs-6) (define (make-table) (list '*table*)) (define (assoc key records) (cond ((null? records) false) ((equal? key (caar records)) (car records)) (else (assoc key (cdr records))))) (define (insert! key value table) (let ((record (assoc key (cdr table)))) (if record (set-cdr! record value) (set-cdr! table (cons

Dynamically create FFI methods in Racket

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-11 13:59:00
问题 I'm playing with idea of loading С/Rust/etc functions via FFI in Racket. I'd like to specify list of function names as strings and then just load them by some helper function. Main problem is creating identifier/word from a string. For example, it is very simple in Rebol: foo: "test1" set to-word (rejoin [foo "_result_data"]) some print test1_result_data but in Racket I have to use syntax stuff. So I've found examples like How do I define functions using Racket macros? and Racket Macro to

using set! to change the value of a variable in drscheme

对着背影说爱祢 提交于 2019-12-11 13:57:46
问题 I'm trying to delete an occurrence of some value in a binary search tree. This is what I have so far: (define removeBin (lambda (x t) (cond ((< x (car t)) (removeBin x (cadr t))) ((> x (car t)) (removeBin x (caddr t))) ((equal? x (car t)) (if(and (null? (cadr t)) (null? (caddr t))) '() (let ((r (replacement t))) ((set! (car t) r) (removeBin r t)))))))) It's giving me the following error: set!: not an identifier in: (car t) What does that mean? and how can I fix it so that set! would work?

checking and comparing two Racket contracts?

荒凉一梦 提交于 2019-12-11 13:29:42
问题 Is there a way to compare them? This doesn't work for instance: (equal? (flat-contract integer?) (flat-contract integer?)) 回答1: First of all, note that the function flat-contract is for backward-compatibility, so you probably should not use it. From the documentation: This function is a holdover from before predicates could be used directly as flat contracts. It exists today for backwards compatibility. So your question is really to ask if two predicates are the same or not. In general this

Transpose list of tuples filling with empty lists

蓝咒 提交于 2019-12-11 12:34:00
问题 I'm new to Scheme and I'm trying to write a procedure which combines n list into a list of n -tuples. If the lists are of different size, the tuples should contain the empty list () when the corresponding list ran out of elements. My current implementation is the following: (define (comb list1 list2) (cond [(empty? list1) empty] [(empty? list2) empty] [else (cons (list (first list1) (first list2)) (comb (rest list1) (rest list2)))])) However, this program doesn't produce another tuple when

Naming variables using variables in Racket?

橙三吉。 提交于 2019-12-11 12:07:45
问题 If I have two variables, for example (define x 10) (define y 20) And I want to create a new variable, using the values of x and y to create the name, how would I go about doing so? For example let's say I want to make a new variable called variable-x-y (define variable-x-y "some-value") In this case, x would be 10 and y would be 20. Basically to summarize everything, I want to be able to enter variable-10-20 and have it return "some-value" I'm sorry if this sounds like a novice question. I'm

Creating an append function in Racket

五迷三道 提交于 2019-12-11 11:07:36
问题 In ISL, how would you create a recursive append function that takes two lists and returns a list of all highest position elements of the first list with the highest position elements of the second list (without using lambda or append )? Basically a function that would hold for these check expects: (check-expect (append-test '(a b c) '(d e f g h)) (list 'a 'b 'c 'd 'e 'f 'g 'h)) (check-expect (append-test '() '(7 2 0 1 8 3 4)) (list 7 2 0 1 8 3 4)) I feel like it would definitely use map ,

Remove subsequence function (deep recursion) in scheme

大憨熊 提交于 2019-12-11 10:45:01
问题 Im trying to write a function called removesub* which accepts two arguments (l1 and l2) . The function needs to return the second list with the first occurence of the subsequence removed. So, if the first list is '(a b c) , the first a if the second list is removed, the first b that appears after the removed a is removed, and the first c that appears after the removed b is removed - no matter how deep the atoms are nested. Working Example Input: (removesub* '(a b) '(w (x b) ((a) ((y z))) b a)

Scheme: Towers of Hanoi (recursion)

我只是一个虾纸丫 提交于 2019-12-11 10:43:12
问题 I'd like to start off by saying this is homework; so I'm not asking for a solution, just some tips. I've been ruminating on this for about a week now. Every solution I come up with doesn't do it recursively, seeing as I can't manage to wrap my head around doing it recursively with the list as the only parameter. My professor said they were able to do it with about 6 helper functions. As mentioned, I have to solve the problem taking taking the list as the only parameter. Here's an example of