racket

Racket-y way on multidimensional vectors operation?

旧城冷巷雨未停 提交于 2019-12-20 03:20:02
问题 I've read this question before, and followed Eli Barzilay's answer on srfi-25. Besides reading the source code of srfi-25, I found writing some auxiliary function would be much more easier, for example #lang racket (define (set2v! vec x y value) (vector-set! (vector-ref vec x) y value)) (define (get2v vec x y) (vector-ref (vector-ref vec x) y)) (define v2 (vector (vector 1 2 3) (vector 4 5 6) (vector 7 8 9))) (get2v v2 1 1) (set2v! v2 1 1 99) (get2v v2 1 1) I was wondering if there maybe some

List order after duplicate filtering

我怕爱的太早我们不能终老 提交于 2019-12-20 03:15:04
问题 I'm trying to teach myself functional language thinking and have written a procedure that takes a list and returns a list with duplicates filtered out. This works, but the output list is sorted in the order in which the last instance of each duplicate item is found in the input list. (define (inlist L n) (cond ((null? L) #f) ((= (car L) n) #t) (else (inlist (cdr L) n)) )) (define (uniquelist L) (cond ((null? L) '()) ((= 1 (length L)) L) ((inlist (cdr L) (car L)) (uniquelist (cdr L))) (else

Filtering through a list of numbers

余生长醉 提交于 2019-12-20 03:11:35
问题 This is my function for curry: (define (curry g) (lambda(x) (lambda(y) (g x y)))) I'm trying to produce a list of numbers not equal to 1 using the curry function. What I have so far is: (define filter-numbers ((curry filter) ((curry equal?) 1))) But it only produces the list of numbers equal to 1. ex. (filter-numbers (list 1 2 3)) -> (list 1) I want to get (list 2 3) but have no idea how. Can anyone help? 回答1: Try this - is the right approach in Racket, using the built-in curry and filter-not

Check for a prime number using recursive helper function

好久不见. 提交于 2019-12-20 02:56:14
问题 I am trying to check if a number is prime using recursion. I was required to use a recursive helper function, but I am not sure how I should implement it. I think I know the algorithm, but I've never tried to use a recursive helper function in Racket. This is my current thoughts: See if n is divisible by i = 2 Set i = i + 1 If i^2 <= n continue. If no values of i evenly divided n , then it must be prime. This is what I have so far... (define (is_prime n) (if (<= n 1) #f (if (= (modulo n 2) 0)

Scheme Continuation: What's the difference between call 'call/cc' in top level and non-top level?

Deadly 提交于 2019-12-20 02:26:24
问题 This code works as expected: (define saved #f) (cons 'wo (call/cc (lambda (k) (set! saved k) '()))) (saved 'ca!) output (Racket console): '(wo) '(wo . ca!) But when I wrap it in a function and call it, the program never stops. Why? (define (test) (define saved #f) (cons 'wo (call/cc (lambda (k) (set! saved k) '()))) (saved 'ca!)) (test) 回答1: A continuation is all that's left to be done in the execution context where it's saved. In the first case, the continuation is saved when calling cons ,

'(quote quote) in scheme

孤人 提交于 2019-12-20 02:12:58
问题 I'm trying to learn scheme by myself. Could anyone tell me why '(quote quote) will output 'quote , and '(quote 'quote) will output ''quote ? Thank you very much! 回答1: This expression: '(quote quote) ... after expanding '<something> to (quote <something>) is equivalent to (quote (quote quote)) , notice that the symbol quote is being quoted two times, and this expression is evaluated and printed as ''quote . On the other hand, this expression: '(quote 'quote) ... is equivalent to (quote (quote

Overloading a struct constructor?

不羁岁月 提交于 2019-12-20 01:39:33
问题 My question is the same as the one asked here, but the answer given there doesn't actually work. I have a bunch of structs inheriting from a parent struct A , which has two fields that I want to make optional for all its descendants. Previously I was using #:auto , but that turns out to really not be what I want because it breaks methods like struct-copy , and also I do want to be able to optionally supply values for those fields on struct creation. I've found a few other questions about

Overloading a struct constructor?

本小妞迷上赌 提交于 2019-12-20 01:39:05
问题 My question is the same as the one asked here, but the answer given there doesn't actually work. I have a bunch of structs inheriting from a parent struct A , which has two fields that I want to make optional for all its descendants. Previously I was using #:auto , but that turns out to really not be what I want because it breaks methods like struct-copy , and also I do want to be able to optionally supply values for those fields on struct creation. I've found a few other questions about

Overlapping module imports in Racket

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-19 16:55:26
问题 I want to load an image and animate it in Racket. I can do it easily in the Dr. Racket, but I am using Emacs with Geiser. To load the image I need to: (require racket/draw) Next, to draw this image onto the screen, I'm planning to use the big-bang module. To load this module I have to: (require 2thdp/image) But I get this error: module: identifier already imported from: 2htdp/image at: make-pen in: racket/draw errortrace...: This basically means that I can't import the same module twice. But

Is struct a macro in Racket?

瘦欲@ 提交于 2019-12-19 10:46:14
问题 I remember I read somewhere it is not a macro and is built into the core language. Something like that, I am not sure, because I can no longer remember from where I read it. So is struct a macro in Racket or not? If not, why is it built into the core language? 回答1: A macro; struct.rkt has (define-syntax (struct stx) (define (config-has-name? config) (cond [(syntax? config) (config-has-name? (syntax-e config))] [(pair? config) (or (eq? (syntax-e (car config)) '#:constructor-name) (eq? (syntax