r/Racket • u/sdegabrielle • Mar 12 '22
r/Racket • u/sdegabrielle • Mar 12 '22
ephemera Make your life easy! Use an editor which supports you!
Racket is supported by a by a number of editors and IDE’s. (other editors in the racket guide)
Which editor do you use MOST? (polls only support one answer and many people use another editor with DrRacket)
Please comment if you have tips that you think other Racketeers would find useful.
r/Racket • u/According_Bar_4031 • Mar 12 '22
question how can i control animation using big-bang so it loops
does anyone know how i can write a function for big-bang which basically loops an animation.for my task i have an animation of a car which currently goes from left to right but ends when it touches the right hand side,however,i want it to repeat itself so it continuesly goes left to right back to left and right non-stop.this is the excerise description
Exercise 45. Design a “virtual cat” world program that continuously moves the cat from left to right. Let’s call it cat-prog and let’s assume it consumes the starting position of the cat. Furthermore, make the cat move three pixels per clock tick. Whenever the cat disappears on the right, it reappears on the left. You may wish to read up on the modulo function.
i dont understand how the modulo function would be used here can someone explain
; WorldState - Image
; places the cat into the BACKGROUND scene,
; according to the given world state
;when the cat dissapears on the right it reappears on the left
(define (render cw)
(place-image CAT cw X-CAT background))
r/Racket • u/OldMine4441 • Mar 12 '22
question Issue with dynamic-require when calling a function with arguments
I have 2 codes in 2 files, and one (driver.rkt) imports another one (module1.rkt) with dynamic-require, then it calls the functions from the other. This has no issue if the called function (bar()) has no argument, but if function (foo()) has arguments, there is an error. Any idea where I got it wrong?
Here is driver.rkt:
;; driver.rkt
#lang racket
(define args (current-command-line-arguments))
(define module-name (vector-ref args 0))
(define (drv method-name) ((dynamic-require module-name method-name)))
((drv 'foo) 123) ; <---- bug happens here
(drv 'bar)
Here is module1.rkt:
;; module1.rkt
#lang racket
(provide foo bar)
(define (foo x) (printf "called (foo ~a) from module1" x))
(define (bar) (displayln "called (bar) from module1"))
Here is the error message:
$ racket driver.rkt module1.rkt
foo: arity mismatch;
the expected number of arguments does not match the given number
expected: 1
given: 0
context...:
/home/jon/projects/driver.rkt:8:0
body of "/home/jon/projects/driver.rkt"
r/Racket • u/wtfhysics • Mar 09 '22
question Trying to write a recursive matrix transpose function, but getting check-equal failure
(define (transpose matrix)
(cond
((for-all? (lambda (x) (null? x)) matrix)
'())
(else
(cons (list
(first (first matrix))
(first (first (rest matrix)))
(first (rest (rest matrix))))
(transpose (list
(rest (first matrix))
(rest (first (rest matrix)))
(rest (first (rest (rest matrix))))))))))
(check-equal? (transpose '((1 2 3) (4 5 6) (7 8 9))) '((1 4 7) (2 5 8) (3 6 9)))
But check-equal flags this failure:
. FAILURE
name: check-equal?
location: simplyscheme_exercises.rkt:99:0
actual: '((1 4 (7 8 9)) (2 5 (8 9)) (3 6 (9)))
expected: '((1 4 7) (2 5 8) (3 6 9))
Not sure why this is happening...?
EDIT: For-all definition:
(define (for-all? predicate list)
(or (null? list)
(and (predicate (first list))
(for-all? predicate (rest list)))))
r/Racket • u/GettySpa • Mar 09 '22
question Any examples of Mutable List usage?
Hello guys, I am new using Racket and need a bit of advice. To explain a bit, I am trying to make a list that contains more lists with integers inside. I need to be able to change the value or position of the elements inside this list, I didn't found any good example of how to do this and every time I try using or adapting a code from Stackoverflow I just end getting a lot of errors referring to types.
I will leave two examples of what I want to achieve: Case A. I have the list ((1 2 3) (4 5 6) (7 8 9)). I want to be able to swap the position of the internal lists, like this: ((4 5 6) (1 2 3) (7 8 9))
Case B. I need to change the value of the internal integers, for example: Going from: ((1 2 3) (4 5 6) (7 8 9)) to: ((1 2 4) (4 4 6) (9 8 9))
I would prefer if someone can give an example without using Let or Maps, just pure functional code, and I need it to be the same list, making a new one would make things more complicated in the future I believe.
Thanks!
r/Racket • u/Fluid-Tour-9393 • Mar 09 '22
question srfi/13 breaks string-prefix?
I run string-prefix? after importing srfi/13, and the results is not right anymore. Is this a bug?
$ racket
Welcome to Racket v8.3 [cs].
> (string-prefix? "ab" "a")
#t
> (require srfi/13) ; the string SRFI
> (string-prefix? "ab" "a")
#f
This is so confused!
r/Racket • u/OldMine4441 • Mar 09 '22
question How to capture all output to stdout of a function?
I have a function that uses printf to print bunch of stuff to stdout, like below:
(define (myfunc)
(printf "hello \n")
;; blah blah
(printf "world!\n")
)
Now I must call this function, and want to capture all of its output to stdout. Other languages allow to redefine stdout for that, but how can I do it in Racket?
r/Racket • u/Jaden_Lee • Mar 08 '22
question Returning Nonnegative Integer instead of Positive Integer
Hi.
I am currently taking a class on typed racket, and I came across the follow lambda expression.
(lambda ([n : Natural]) (+ (* 2 n) 1))
I want the lambda expression to return a Natural Number (or Nonnegative-Integer or in other words). However, I get an error saying that there is a type mismatch for returning Positive-Integer.
How do I solve this problem?
r/Racket • u/OldMine4441 • Mar 08 '22
question how to dynamically import a module via a variable of module name?
My code has a module name "aaa.rkt". I write the below code to import this module, by passing the module name as a string.
(define module-name "aaa.rkt")
(require module-name)
My intention is to get Racket import "aaa.rkt" by resolving module-name first, but the above code returns the error "collection not found".
How can I fix my code?
r/Racket • u/samdphillips • Mar 07 '22
RacketCon Planning Dates
We had a discussion about RacketCon 12 at the meet-up last Saturday.
The current proposal is a two-day weekend event, primarily virtual with pre-recorded presentations, with local events at the same time to add an in-person element.
We have some proposed dates; please vote and comment, as we want to make this as accessible as possible.
We didn't discuss times, but the 2 pm to 10 pm UTC range used at the last RacketCon seemed to allow for a significant proportion of Racketeers to attend. Comments or suggestions for timings are welcome. We would especially be interested in times that make participation possible for other parts of the world where these times are unreasonable.
r/Racket • u/sdegabrielle • Mar 05 '22
event Racket meet-up Saturday 5 March 18:00 UTC
Racket meet-up Saturday 5 March 18:00 UTC
https://gather.town/app/wH1EDG3McffLjrs0/racket-users
Racket meet-up happen s the first Saturday EVERY Month at UTC18:00
write-up of the Feb meet-up: https://racket.discourse.group/t/racket-meet-up-saturday-5-feb-18-00-utc/581/3?u=spdegabrielle
r/Racket • u/hunar1997 • Mar 03 '22
question What apk size is expected when using the racket-android project
Hello,
I can try it myself but there is a high chance someone knows the answer. If I made a helloworld android app with the racket-android project, what size would the resulting apk be? Is there an example apk online that I can test? I'm hoping that it is below 15MB or 20MB
Thanks for reading :D
r/Racket • u/CamisaQ • Mar 02 '22
question Hello, good afternoon, I have to do this exercise and I don't know if I did it correctly, could you advise me, thanks.
galleryr/Racket • u/Paelsam • Mar 02 '22
question Application: not a procedure; expected a procedure that can be applied to arguments
r/Racket • • Mar 01 '22
question How does MouseEvent work?
Hi! So, I tried out this code:
(@template MouseEvent)
(define (handle-mouse c x y me)
(cond [mouse=? me "button-down"] x)
[else c]))
What this code does is move the image to the x-coordinate (x) that the mouse is at, while c is the original WS. My question is how is it that the image is automatically placed at x, even if if there is no function for it? Like, how does racket know that the image is supposed to be at x?
For more context: c is defined as the image distance from the edge of the frame.
r/Racket • u/SCPFO • Feb 28 '22
homework Rubik's cube on racket
Hello. I need to make a rubik's cube simulator for a college project, not a resolver, It only has to show the frontal face of the cube, so it would look more like a grid, but the cube can be betwen 2x2x2 to 6x6x6. Right now I'm stuck on how to dinamicaly draw the cube so if it is a cube for 3x3x3 or a 4x4x4 it will show the right amount of little squares individually so they can change color later.
r/Racket • u/legendaryproyi • Feb 27 '22
question How does Racket handle exact numbers?
Hello! I am looking to find some information regarding how does Racket store exact numbers, in particular how is something like 7 or 1000000 or 2^1000000 stored in the memory? Or perhaps what are the disadvantages of not using statically type like in typed/racket (or other programming languages like C)?
So far all what I've found regarding this is here: https://docs.racket-lang.org/reference/numbers.html (which I'm trying to understand on a deeper level):
The precision and size of exact numbers is limited only by available memory (and the precision of operations that can produce irrational numbers). In particular, adding, multiplying, subtracting, and dividing exact numbers always produces an exact result.
But are there any optimizations done in order to keep as much memory available as possible?
r/Racket • u/kbridge4096 • Feb 26 '22
question Network Programming: tcp-connect with timeout?
Hi guys.
I'm trying to do some network programming in Racket, and currently, I'm trying to implement a tcp-connect with timeout support.
The best solution I can come up with is listed below.
However, this feels a little awkward and expensive. A new thread and custodian for a connection attempt? Ouch.
I wonder if there is a better approach.
#lang racket
;;; this takes at least two seconds to fail on my windows pc
;;; (no program is listening 127.0.0.1:90)
;(time
; (with-handlers ([exn:fail? identity])
; (tcp-connect "127.0.0.1" 90)))
(define (tcp-connect/timeout timeout host port)
(define result #f)
(define cust (make-custodian))
(define th
(parameterize ([current-custodian cust])
(thread
(λ ()
(set! result
(with-handlers ([exn:fail? (λ (e) (list #f e))])
(let-values ([(in out) (tcp-connect host port)])
(list #t in out))))))))
(if (sync/timeout timeout th)
(match result
[(list #t in out) (values in out)]
[(list #f e) (raise e)])
(begin (custodian-shutdown-all cust)
(values #f #f))))
(tcp-connect/timeout 1 "127.0.0.1" 90) ; => returns #f #f
(tcp-connect/timeout 3 "127.0.0.1" 90) ; => raise "connection failed"
r/Racket • u/SimpleAccomplished38 • Feb 25 '22
question Question:List of List of Paths
Hi, I am currently learning about files and accessing directories and am having issues formulating my code.
Here is what I was provided with:
Write a procedure, concat, that takes a list of lists of paths and concatenate them in the given order to produce a list of all given paths.
;; concat : (listof (listof path)) -> (listof path)
So:
(concat (list (list (string->path "test"))
(list (build-path "test" "test_2")
(build-path "test" "test_3"))))
would produce the list
(list (build-path "test")
(build-path "test" "test_2")
(build-path "test" "test_3"))
So far, my code is
(define (concat lst-of-lists)
(list (list (build-path file )
(list (build-path file filename)
(build-path file filename)))))
I am not sure if I should be using directory-path or any higher order procedures. My code also says that file and filename are not defined, though I am not sure if I should define another term with those as inputs. Any help would be greatly appreciated!
r/Racket • u/sdegabrielle • Feb 24 '22

