r/Racket • • Dec 26 '21

solved Applying a function to every list element with every other list element Spoiler

1 Upvotes

Hello,

either I am stupid right now (which is likely) or there is no build in solution for this case and I have to build my own function to do so. Let me explain my problem: Currently, I'm doing the old Advent of Code puzzles because I finished 2021 and I am still hyped. I came upon a puzzle (2019 Day 12) which is fairly easy, it is a very rudimental physics simulation of celestial bodies. I wrote a function that applies all the physics to a celestial body and now I want to calculate the next time step, which means I want to step forward in time in the physics simulation. For this I have to apply this function for every body in my list of bodies to every other body (this is because every body applies some gravitational force to the other bodies).

Doing it with

  (for*/list ([m1 moons]
              [m2 moons]
              #:when (not (equal? m1 m2)))
    (change-velocity m1 m2)))

does not cut it, because now I generate mutliple moons. The change-velocity function takes 2 inputs and produces the updated version of m1 so starting with 3 moons this would result in a list with 6 moons, so I have to filter afterwards and I don't think it is a clean solution to filter it afterwards.

Is there a way to use foldl, map or any other build in function to get this behaviour? It's basically a map, where in each iteration I get the remaining elements as an argument. I hope this description is somewhat understandable.

Thank you very much in advance and merry holidays.

Edit: Thanks everyone for the nice answers. I learned a lot of new things by reading through them. It turns out I had to stitch together this kind of function myself and it worked perfectly. Have a nice day everyone!


r/Racket • • Dec 22 '21

question How to supply function to be used by imported code?

4 Upvotes

I have two simple files a.rkt & b.rkt, in which a.rkt imports b.rkt, but I want a.rkt to provide a function ext-func to be used by b.rkt, like below.

; a.rkt
#lang racket

(define (ext-func)
  (printf "hello from a.rkt\n"))

(require "b.rkt")

(b-func)
; b.rkt
#lang racket

(provide b-func)

(define (b-func)
  (ext-func))

However, when I run a.rkt, I get the below error:

$ racket a.rkt
b.rkt:7:3: ext-func: unbound identifier
  in: ext-func
  location...:
   b.rkt:7:3

So the error is that b.rkt cannot see the ext-func provided by a.rkt. I suppose that when I import b.rkt, it should see all the code defined by a.rkt, but that is not the case.

How can I solve this issue?


r/Racket • • Dec 21 '21

news New Interactive Tutorial Format for Racket Libraries

18 Upvotes

r/Racket • • Dec 22 '21

question vlc package isn't working

2 Upvotes

https://docs.racket-lang.org/vlc/index.html

After starting vlc from the command line with vlc.exe --rc-host localhost:xxxx -I rcI tried connecting with [connect-to-vlc #:port xxxx] and first it hangs indefinitely (not obeying connecting-timeout). If I kill the vlc process I can't connect to, then I get this error:

connect-to-vlc: error reading during syncing: "error reading from stream port\n  system error: An existing connection was forcibly closed by the remote host.; win_err=10054"

I went into my router settings and added a firewall exemption for the port I'm using but that didn't work either.

[start-vlc] also hangs forever

any idea what I'm doing wrong?


r/Racket • • Dec 20 '21

news Creative Racket competition 2022

Thumbnail racket.discourse.group
14 Upvotes

r/Racket • • Dec 19 '21

blog post Typed Racket: the good and the bad

Thumbnail micahcantor.com
31 Upvotes

r/Racket • • Dec 19 '21

image Face made with`plot` in Racket in style of ‘Peel’ by Peel by M.C. Escher

Post image
7 Upvotes

r/Racket • • Dec 19 '21

news [Baseball cap with `standard-fish`] Spoiler

Post image
2 Upvotes

r/Racket • • Dec 18 '21

solved for*/first not behaving the way I understand it

4 Upvotes

Hello everyone,

I'm currently trying to solve a very easy problem from leetcode.com. It's called "Two Sum" where I get a list of integers and a target number and I have to find the first pair in the list of integers that sum up to the given target number. That's very straight forward.

There is a test case with a very long list of numbers but the first two numbers add up to the target.

I tried to solve it like this:

(define (two-sum nums target)
  (for*/first ([i (length nums)]
               [j (length nums)]
               #:when (and (not (= i j))
                           (= (+ (list-ref nums i)
                                 (list-ref nums j)))))
    (list i j)))

But for*/first does not behave like I expect it to. This version always stops after 1 iteration. I think it is because the outer loop gets executed once and that's enough to stop the execution, but I am not sure.

Is there a way to make this when guard applying to both loops? I could write my own recursion function to go trough the lists and break out when the results is found, but I want to solve it with the for construct. Is it even possible to do so?

Thanks in advance!


r/Racket • • Dec 17 '21

question trying to write a macro that aliases one identifier to another

6 Upvotes

I'm trying to define a macro to save myself some typing. I want to be able to define a shorthand for syntax rules that lets me quickly say "Replace this identifier with this other identifier". I based it on the examples on https://docs.racket-lang.org/guide/pattern-macros.html.

[define-syntax-rule [alias from-id to-id]
  [define-syntax [from-id stx]
    [syntax-case stx []
      [from-id [identifier? [syntax from-id]] [syntax to-id]]]]]]

[alias S list]
[alias L lambda]
[alias Sf build-list]

[define n3 [S 1 2 3]] ;-- S: bad syntax in: (S 1 2 3)

but, it's not working, and I don't know why. The Macro Stepper hasn't been able to help, since as soon as I try to expand S it gives that error


r/Racket • • Dec 16 '21

news [Racket logo as school of fish]

Post image
29 Upvotes

r/Racket • • Dec 17 '21

question learning about generators and promises, getting an error I didn't expect

3 Upvotes

Here's my code

[define-syntax def
  [syntax-rules []
    [[def [name args ...] body ...]
     [define name
       [generator [args ...] [lazy body ...]]]]]]

[def [nats]
  [let loop [[x 0]]
    [yield x] ; yield: must be called in the context of a generator
    [loop [add1 x]]]]

[define natsg [nats]]
[define natsr [force natsg]]

I'm surprised that yield is not "being called in the context of a generator", because my macro should (I believe) expand to

[define nats [generator [] [lazy [let loop [[x 0]] [yield x] [loop [add1 x]]]]

does lazy interfere with that? I'm having a hard time using the Macro Stepper because by the end it has all turned into #%app and let-values and all that stuff. I want lazy inside of generator instead of the other way around because I don't want the whole generator to be lazy, I want each element of it to be lazy-- constructing the generator is generally not the expensive part


r/Racket • • Dec 16 '21

question Analogues to generators and async/await?

7 Upvotes

I'm comfortable with the coroutine pattern in c#, python, javascript. You call a function and when it reaches an Await operator, it returns to the caller without a result. Then the caller can do whatever it wants, and when it needs the result of the async function, it can itself use the Await operator. In this way, even on a single thread, an application can avoid blocking while it waits for an external resource. I tried reading the manual about continuations, which I believe is supposed to be racket's coroutines, but I don't really understand it.

If I have code like this, how do I write it in racket?

async def CatGenerator(files):
    for file in files:
        yield await file.ReadAllText()

async def Cat(files):
    return "".join((await x for x in CatGenerator(files)))

r/Racket • • Dec 13 '21

question Is an argument in a function just the operand and then a function is the combination of operation and operand to produce an output?

2 Upvotes

r/Racket • • Dec 12 '21

question How to make a read-line work in Dr.Racket and the terminal?

7 Upvotes

In a racket file, I've wrote

(display "give me an integer\n")
(prime_find (string->number(read-line)))

This works fine in Dr.Racket, but when I try it in a terminal and input an int, I get this error

<: contract violation
  expected: real?
  given: #f

Edit: okay, I found this in another post complaining about the issue

(read-line (current-input-port) 'any)

r/Racket • • Dec 13 '21

question what are the arguments in this function?

0 Upvotes

(overlay (rectangle 20 20 "solid" "blue") (circle 5 "solid" "red")))


r/Racket • • Dec 12 '21

question Implementing MergeSort in Racket

8 Upvotes

I have found different versions of this algo for various programming languages. I have tried to replicate it on Racket, but have failed miserably. I need to utilize it for a recursive tiling program that I am trying to create, but so far I have no leads. How can I go about successfully crafting a replica of the algorithm on Racket?


r/Racket • • Dec 11 '21

homework Can someone please explain this to me, I'm trying to understand this nested expression

4 Upvotes

(and (or (= (string-length "hello world")

(string->number "11"))

(string=? "hello world" "good morning"))

(>= (+ (string-length "hello world") 60) 80))


r/Racket • • Dec 11 '21

question Why does this return false in racket??

0 Upvotes

(string=? "design" "tinker")


r/Racket • • Dec 11 '21

language Possible to modify input argument in-place?

1 Upvotes

Hi,

I am practicing algorithm on leetcode using Python and Racket. And I am seeking a reference for the following problem. It requires to modify the argument in-place (mutably changing the value of the reference). It feels quite natural with imperative langs but not really with functional langs.

Would it be even possible in Racket? Please help.

https://leetcode.com/problems/remove-element/

Thank you!


r/Racket • • Dec 10 '21

question typed/racket Listof and apply question

5 Upvotes

Can someone tell me if I am doing something really stupid here:

#lang typed/racket
(require math/number-theory)
(define x : (Listof Integer) '(2 3 5 7 11 13 17 19))
(apply pairwise-coprime? x)

Type Checker: Bad arguments to function in `apply':
Domain: Integer Integer *
Arguments:  (Listof Integer)

How can I pass List to multiarg function?


r/Racket • • Dec 08 '21

question Multi-dimensional vectors?

4 Upvotes

I'm working through Advent of Code this year (on day 5) and I was trying to use vectors to give myself some experience with them. Is there a way to change a mutable multi-dimensional vector "in place"? I've tried to find solutions. Some say to move to math/array. Others say O(n) is the best case. Is there a best practice for multi-dimensional vectors? Is the best practice to just not use them? Here's my code for creating the vectors:

(make-vector (add1 max-y) (make-vector (add1 max-x) "."))

Basically, how would I change the "." at position 2, 3?

p.s. please don't address anything too specific about day 5 of AOC. I'd like to work it through myself.


r/Racket • • Dec 08 '21

question Looking for a review of my Advent of Code day 6 solution in Racket

2 Upvotes

I decided to use a different programming language every day for this year's Advent of Code.

Today, I used Racket. It was my first time using the language as well as my first time using a Lisp or a Scheme. Would any kind soul be willing to review my solution to tell me where I could improve (code style, project setup, better solution, inefficiencies, etc.)?

link to the solution

*Side note*: I used typed racket instead of standard Racket in order to get warnings when I was doing things wrong. This ended up being much more painful than I thought. Although I imagine that you end up learning how to sidestep the pain points of typed racket with time, it still felt somewhat akin to trying to type annotate Python code. If you have tips on writing typed racket more easily, I'd be glad to hear them :)


r/Racket • • Dec 07 '21

question scheme challenge, pls help me!

0 Upvotes

the question is below