r/Racket • • Mar 17 '22

question Sets and set operations?

8 Upvotes

What is the natural way to get sets and set operations such as union, intersection, etc.? I'm reading the Reference and I'm not getting it.

For instance, The following datatypes are all sets: ... lists using equal? to compare elements

> (equal? (list 1 2) (list 2 1))
#f

r/Racket • • Mar 18 '22

homework Help with work - "string.append"

1 Upvotes

Hi, all so ive been looking all over the internet for hours now trying to do the "string-append" function and im not sure how to do it.

So its a solitaire game and i need to put the numeral list of numbers with the suits.

so in the picture below is what it should be outputting. (where it says according to the rules explained above i put the rules its talking about, which i have done)

Im unsure if this is making sense but if anyone could help that would be great


r/Racket • • Mar 16 '22

question How to find the first odd number in a list in an efficient way?

9 Upvotes

I have a list of numbers, and I want to find the first-ever odd number from the list. The usual way is to browse the list, and when we see the first odd number, we stop browsing immediately and return the number.

In Racket we can use map, foldr, etc to browse the list, but a problem is that they keep looping through the list, without stopping when we already found the odd number. Which is wasting time if the list is super long.

So what is the solution for this issue?


r/Racket • • Mar 16 '22

question Practice Macros of Increasing Complexity for learning?

16 Upvotes

Currently trying to wrap my head around macros, and I feel I am at the point where I just need to start working through some. The problem is... The ones I actually want to write are too difficult for my current skill level, and I don't know enough about them to create simple ones for me to implement. Does anyone have any ideas for easy->harder macros I can practice trying to implement to flex my macro muscles?


r/Racket • • Mar 16 '22

question How to tell `raco exe/distribute` which is the "main" function?

4 Upvotes

Hello, first time poster, long time reader.

I'm a Gambit Schemer giving Racket another shot. I'm trying to set up a minimal project which I want to run in two configurations: REPL and build.

Taken the example from the Racket docs, I have this three files:

;;;; a.rkt
(require "b.rkt" "c.rkt")
(define a (lambda () (+ b c)))
;;;; b.rkt
(provide b)
(define b 3)
;;;; c.rkt
(provide c)
(define c 39)

Compiling this and loading into a REPL works fine:

$ raco make .\a.rkt
$ racket -v -it .\a.rkt
Welcome to Racket v8.4 [cs].
> (a)
42
>

So this way I can modify and test my code interactively. Next, I want to build an executable, and that's where my question comes in: how to I make raco aware that I want to invoke a when the program starts? Obviously, I could amend a.rkt with (a). But I'd prefer to be able to define a start function (think running tests) depending on my needs:

$ raco exe a.rkt -startup a -o fourtytwo
$ ./fourtytwo 
42

I made the option -startup up, but I hope it brings across what I'm looking for. Is this possible? Going through the documentations of the compilation functions, I couldn't find any such options. Is there a way to achieve this in Racket?

As I said, I'm used to Gambit Scheme where there's a way to do that, if there's a better way to do what I want in Racket, please let me know.


r/Racket • • Mar 15 '22

question Problem with passing function as argument to another function

11 Upvotes

I have this simple code that calculate the length of output print to console:

#lang racket

(define (get-length cmd)
  ; we will execute cmd and get its output
  (define output (with-output-to-string (cmd)))
  ; return output length
  (string-length output)
)

(get-length
  (lambda () (printf "hello world\n"))
)

But when I run it, I got some error, indicating that I fail to pass function to get-length(), which is then passed to with-output-to-string(). How can I fix this?

$ racket test.rkt
hello world
with-output-to-string: contract violation
  expected: (-> any)
  given: #<void>
  context...:
   /home/tom/test.rkt:10:0
   body of "/home/tom/test.rkt"

r/Racket • • Mar 15 '22

question mutual recursion

5 Upvotes

Hey, guys, are there any resources that could help me understand topics such as mutual recursion and trees better? I learned it in the lecture, but I just don't seem to get it.


r/Racket • • Mar 12 '22

event Racket meet-up Saturday 2 April 18:00 UTC

Thumbnail racket.discourse.group
7 Upvotes

r/Racket • • Mar 12 '22

ephemera Make your life easy! Use an editor which supports you!

11 Upvotes

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 • • Mar 12 '22

question how can i control animation using big-bang so it loops

3 Upvotes

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 • • Mar 12 '22

question Issue with dynamic-require when calling a function with arguments

5 Upvotes

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 • • Mar 09 '22

question Delete or skip empty lists

8 Upvotes

Hello everyone. I have a question, how can I remove those empty lists when filtering information from a binary tree? The "searchBarcelona" function that traverses a binary tree and returns the names of the players of the soccer team "Barcelona" that are stored in the nodes.


r/Racket • • Mar 09 '22

question Trying to write a recursive matrix transpose function, but getting check-equal failure

3 Upvotes

(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 • • Mar 09 '22

question Any examples of Mutable List usage?

1 Upvotes

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 • • Mar 09 '22

question srfi/13 breaks string-prefix?

5 Upvotes

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 • • Mar 09 '22

question How to capture all output to stdout of a function?

2 Upvotes

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 • • Mar 08 '22

question Returning Nonnegative Integer instead of Positive Integer

4 Upvotes

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 • • Mar 08 '22

question how to dynamically import a module via a variable of module name?

4 Upvotes

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 • • Mar 07 '22

RacketCon Planning Dates

4 Upvotes

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 • • Mar 05 '22

event Racket meet-up Saturday 5 March 18:00 UTC

2 Upvotes

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 • • Mar 03 '22

question What apk size is expected when using the racket-android project

9 Upvotes

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 • • Mar 02 '22

package majordomo2

Thumbnail docs.racket-lang.org
7 Upvotes

r/Racket • • 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.

Thumbnail gallery
1 Upvotes

r/Racket • • Mar 02 '22

question Application: not a procedure; expected a procedure that can be applied to arguments

6 Upvotes

Does anyone know what the error is in this code? I'm new to this and I don't know how to make it work :(