racket

Turn string into number in Racket

二次信任 提交于 2019-12-02 00:54:54
I used read to get a line from a file. The documentation said read returns any , so is it turning the line to a string? I have problems turning the string "1" to the number 1 , or "500.8232" into 500.8232 . I am also wondering if Racket can directly read numbers in from a file. Check out their documentation search , it's complete and accurate. Conversion functions usually have the form of foo->bar (which you can assume takes a foo and returns a bar constructed from it). You sound like you're looking for a function that takes a string and returns a number , and as it happens, string->number

Check for a prime number using recursive helper function

牧云@^-^@ 提交于 2019-12-02 00:25:22
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) #f ) What would be a good approach using a recursive helper function?? Thanks! Using a helper simply

Modification of the basic if expression in Scheme. Why does it go into an infinite loop?

拥有回忆 提交于 2019-12-01 22:09:25
In Scheme, I modified the basic 'if' command as: (define (modified-if predicate then-clause else-clause) (if predicate then-clause else-clause)) And then I defined a simple factorial generating program using the modified version of if: (define (factorial n) (modified-if (= n 0) (* n (factorial (- n 1))))) Now, when I call the above function, it goes into an infinite loop. Why does that happen? Scheme has eager evaluation. This means that, unless you're using a special form (like if ) or a macro (like cond or case ) that delegates to such a special form, all subexpressions are evaluated first.

'(quote quote) in scheme

走远了吗. 提交于 2019-12-01 21:50: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! 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 (quote quote))) , notice that the symbol quote is being quoted three times, and this expression is evaluated

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

那年仲夏 提交于 2019-12-01 21:50:52
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) 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 , so it's just to cons 'wo to something and return to the REPL. In the second case, you call procedure test ,

Remove multiple characters from a list if they are next to each other in Scheme

前提是你 提交于 2019-12-01 20:52:34
问题 I have to make a Dr. Racket program that removes letters from a list if they are following the same letter as itself. For example: (z z f a b b d d) would become (z f a b d). I have written code for this but all it does is remove the first letter from the list. Can anyone help? #lang racket (define (remove-duplicates x) (cond ((null? x) '()) ((member (car x) (cons(car(cdr x)) '()))) (remove-duplicates (cdr x)) (else (cons (car x) (remove-duplicates (cdr x)))))) (define x '( b c c d d a a))

How do I handle a variable number of arguments passed to a function in Racket?

♀尐吖头ヾ 提交于 2019-12-01 20:16:36
问题 I like creating functions which take an unlimited number of arguments, and being able to deal with them as a list. It's been useful to me when creating binary trees & I'm using it for a variation on the nearest-neighbour algorithm right now. My method, however, is really horrible: since I can't think of a way to iterate over an improper list (which may well be improper & degenerate), I tried using various list functions to force the improper list into list form. This is my best attempt in a

How do I handle a variable number of arguments passed to a function in Racket?

谁说胖子不能爱 提交于 2019-12-01 20:09:55
I like creating functions which take an unlimited number of arguments, and being able to deal with them as a list. It's been useful to me when creating binary trees & I'm using it for a variation on the nearest-neighbour algorithm right now. My method, however, is really horrible: since I can't think of a way to iterate over an improper list (which may well be improper & degenerate), I tried using various list functions to force the improper list into list form. This is my best attempt in a simple function to determine difference between map-nodes (works, just not sure why it works): (define

Remove multiple characters from a list if they are next to each other in Scheme

て烟熏妆下的殇ゞ 提交于 2019-12-01 20:01:42
I have to make a Dr. Racket program that removes letters from a list if they are following the same letter as itself. For example: (z z f a b b d d) would become (z f a b d). I have written code for this but all it does is remove the first letter from the list. Can anyone help? #lang racket (define (remove-duplicates x) (cond ((null? x) '()) ((member (car x) (cons(car(cdr x)) '()))) (remove-duplicates (cdr x)) (else (cons (car x) (remove-duplicates (cdr x)))))) (define x '( b c c d d a a)) (remove-duplicates x) (define (remove-dups x) (cond [(empty? x) '()] [(empty? (cdr x)) (list (car x))] [

Overloading a struct constructor?

二次信任 提交于 2019-12-01 18:40: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 optional arguments for structs, but the answers have all suggested defining a custom constructor and using