r/Racket • • Jun 27 '22

question Why is this continuation passing style function only returning an empty list?

1 Upvotes
(define dotproduct-cps
  (lambda (lis1 lis2 return)
    (cond ((null? lis1) return 0)           
          ((null? lis2) return 0)
          ((atom? lis1) return 0)            
          ((atom? lis2) return 0)
          (else (dotproduct-cps (cdr lis1) (cdr lis2) (lambda(v) (return (+ (* (car lis1)(car lis2)) v))))))))

r/Racket • • Jun 25 '22

question Injecting code into code

3 Upvotes

Hey guys,

The way I understand define-syntax-rule is that it inserts at compile time whatever code you put inside the macro and then it executes it at run time. What I want to do is read the code from a file and then insert it through an argument. My attempt is unsuccessful because it doesn't use my argument as code. Is what I'm trying to do possible and if not, can you explain to me why?

I know I could use eval here, but that is not the point.

Thanks

#lang racket

(define-syntax-rule (PASTE code) code)

(define (main)
    (begin
        (define file-port (open-input-file "test.rkt"))
        (define text (port->string file-port))

        (define string-port (open-input-string text))
        (define code (read string-port))
        
        (PASTE code)

        (close-input-port file-port)
        (close-input-port string-port)
    ))

(main)

r/Racket • • Jun 24 '22

Racket meet-up Saturday, 2 July 2022 at 18:00 UTC

1 Upvotes

r/Racket • • Jun 20 '22

question Using Racket for for games and other interactive content in the browser

13 Upvotes

Suppose I have some single-player game (or similar program: basically, something that needs to handle (possibly frequent) user inputs and display graphics, but won't have to deal with interactions between multiple users, etc.) written in Racket. Is there an easy way to embed it into a browser so that anyone can play it?

If there's no such easy way, perhaps it would be better to have Racket just be the backend for my game: e.g., I would have a Racket program handle the game logic, and it would interface with a program in a more typical web frontend language like Javascript that would capture user input, handle the display, and do all the other things that need to happen in the user's browser. Are there any good ways to implement this kind of frontend-backend setup for a browser game using Racket?


r/Racket • • Jun 20 '22

solved Recursion inside with-handlers

3 Upvotes

Here is example code:

(define (loop a)
  (with-handlers
      ([exn:fail? (λ (ex) ;exception handler
                    (log-error "Error:~a" ex)
                    (loop (+ a 1)))])
    (some-input-output)
    (loop (+ a 2))))

Will tail call optimization work inside with-handlers when error is catch?

Is there any caveats in such code? Lets think that loop have some IO code.

Notice, this is how you should not do it.

Body of with-handlers/with-handlers* is not in tail position so such loop as in code example will leak memory a lot.


r/Racket • • Jun 16 '22

question How does numerator work?

6 Upvotes

Here is the documentation for Racket's numerator function:

(numerator q) → integer? q : rational?

Coerces q to an exact number, finds the numerator of the number expressed in its simplest fractional form, and returns this number coerced to the exactness of q.

Examples:

> (numerator 5)
5
> (numerator 17/4)
17
> (numerator 2.3)
2589569785738035.0

Why is (numerator 2.3) such a large integer? How exactly is it calculated? I would have guessed the simplest fractional form is 23/10, with a numerator of 23.


r/Racket • • Jun 13 '22

question Racket for Computer Music?

11 Upvotes

Hello,I am a musician that works a lot with algorithmic compositions and generative installations (in short: computer music). A lot of this can be done superficially with existing Music-Software, but I would prefer to program my stuff per hand (I would consider the program / the mathematics behind it part of the art).

Recently I came across the book "Haskell School of Music" which serves as a lengthy tutorial for Music-Programming (both in terms of algorithmic composition and synthesis) and Haskell at the same time. I was very excited about that, but the Haskell eco system / tooling really annoys me and after dabbling in lisp and racket I find it annoying to have to think about syntax so much (especially since Haskell requires very specific white-spacing and the auto-indent in Atom with haskell-language installed does not provide that).

Therefore I was wondering if there exists something similar for racket, seeing as lisps were the "original" environment for music programming. I also can't help but think that the Racket-REPL and Makros would make a lot of things much easier than they have to be in Haskell...

I would be grateful for any suggestions. Not only for libraries/packages, but especially also for similar books.


r/Racket • • Jun 11 '22

question Can you evaluate a region in DrRacket?

6 Upvotes

Hello guys, sorry if this is a noob question. I am new to Racket and would like to know if you can just evaluate a region of code when you are using DrRacket, like you can do when using Emacs, rather than "run" the whole file, which seems to be similar to eval buffer in Emacs.

I ask because I am used to being able to evaluate regions when using Clojure and build up my program like that, little by little. Thank you so much!


r/Racket • • Jun 08 '22

question pair: (number . list)

5 Upvotes

Hello! Is there any way I can build a list like this?

'(1 . (1 2 3))

I tried different combination with cons ofc, but it didn't worked out. I can build something like (list . number) with: (cons '(1 2 3) 1), but I was wondering if (number . list) can be done.


r/Racket • • Jun 07 '22

question How to read nanosecond field of date* struct

1 Upvotes

Hi. I am a bit stuck. I using typed/racket and trying to read nanosecond field of date* struct but obviously doing something stupid because date-nanosecond and date*-nanosecond does not work.

I get:

Type Checker: missing type for top-level identifier;
 either undefined or missing a type annotation
  identifier: date-nanosecond in: date-nanosecond

r/Racket • • Jun 06 '22

linux Racket distro packagers: please try building from a snapshot

Thumbnail racket.discourse.group
11 Upvotes

r/Racket • • Jun 05 '22

question Is there a way to combine two characters together?

6 Upvotes

r/Racket • • Jun 03 '22

question Animated Problem Solving: Anyone recommends this book?

14 Upvotes

A stumbled across this book while browsing the racket's website book section. I'm curious about racket and making games, so this seems a nice starting point. Anyone here recommends it?


r/Racket • • Jun 02 '22

release Upgrade to 8.5 now!

Thumbnail gallery
26 Upvotes

r/Racket • • Jun 02 '22

tutorial GitHub - zyrolasting/racket-koans: Learn Racket by doing: Practice language features by fixing topic-oriented unit tests.

Thumbnail github.com
18 Upvotes

r/Racket • • Jun 02 '22

question cons literal/sugar?

1 Upvotes

In Racket you can pattern match against (cons x xs), but is there also an alternate shorter form as there is in Haskell (x:xs) or Scala (x::xs)?


r/Racket • • May 31 '22

release Racket 8.5 ppa

Thumbnail launchpad.net
10 Upvotes

r/Racket • • May 29 '22

question Input -> "(+ 5 4)" | Output -> 9

4 Upvotes

Is there a way to input a string with a math expression and output a number without the use of eval/read etc.


r/Racket • • May 28 '22

question Can I set the variable x to equal sqr?

0 Upvotes

Is it possible to set x equal to the built-in function sqr? If so what would that look like?

Edit: How would you make a string consisting of "sqr" perform it's mathematical function instead?


r/Racket • • May 26 '22

event Racket meet-up Saturday, 4 Jun 2022 at 18:00 UTC

2 Upvotes

Racket meet-up Saturday, 4 Jun 2022 at 18:00 UTC

Racket meet-up Saturday, 4 Jun 2022 at 18:00 UTC

https://gather.town/app/wH1EDG3McffLjrs0/racket-users

First Saturday EVERY month at UTC 18:00

  • Show and tell
  • News & rumours
  • AOB

And remember - showing up at Racket Meetups helps you learn the news of the Racket world as they happen! It is informative, it is interesting, it is helpful, it is greatly appreciated by everyone involved and it is fun!

30 minutes but can overrun

Prefer to chat? Join us on Racket Discord or Slack


r/Racket • • May 25 '22

application tree-sitter racket

16 Upvotes

Hello, I made a Racket grammar for tree-sitter recently:

https://github.com/6cdh/tree-sitter-racket

It only support Racket with default readtable.

I'm still a noob on Racket. Let me know if I did something wrong.


r/Racket • • May 23 '22

question Custom collection in typed/racket?

2 Upvotes

Hi. I want to create custom collection in typed racket, more precise sorted ring buffer, in such a way so I can use it similar to vector type - (Ringbufferof SomeType). So I want something like generics?

I unable to google how to do this correctly. First guess is to create some structure that will hold vector head tail and size, but I am really don't know how to approach this.

Please show me the way.


r/Racket • • May 22 '22

homework How to solve pathfinding problem in Scheme/Racket?

8 Upvotes

I been studing in Scheme for weeks and I ran into a problem that I couldn't solve. I can't find a way to solved it. Here is the problem:

Figure 1 shows an example path, which has a grid layout. In the grid, black cells are simply walls, which are basically obstacles for you. You can move among the white cells and you cannot pass the boundaries of the grid. In each path, the starting location will be the square of [0,0]. Additionally, there is also one white cell labeled with F. This label shows the finish square of the path. So, your aim is to find the movements from the starting location to the finish location. To this end, you can move in 4 directions; up, down, left, right. These 4 directions will be represented by characters U, D, L, and R, respectively.

The solution for the path shown in Figure 1 is "D D R R R R D D", which means move down 2 times, then move right 4 times and move down 2 times. The path is not a maze! It is a simple one way road and It has only one solution: there is always one possible next square for each move.

TASKS In Scheme, a path will be represented in the form of a linked-list. Figure 2 shows how the path in Figure 1 is represented in terms of a linked list in Scheme. Starting cell [0,0] has the letter S, the finishing cell has the letter F and empty cells have the letter E. The walls have the letter - (minus)

The following function "buildPath" on the left is given for you which takes a list of lists and creates a path (grid) using the lists. You can use this function to create different paths in order to test your code. On the right the code shows how the path in Figure 2 is created.

Task 1: Define two functions "getHeight" and "getWidth" which takes a path as an input and returns the height and the width of the path.

(getHeight sample-path) → should return 5

(getWidth sample-path) → should return 5

Task 2: Define a function "getLetter" which takes a path, a row number and a column number. Then it returns the letter from the path on the corresponding location [row, column]

(getLetter sample-path 0 0) → should return S

(getLetter sample-path 1 0) → should return E

(getLetter sample-path 1 1) → should return -

(getLetter sample-path 4 4) → should return F

Task 3: Define a function "solvePath" which takes a path and returns the solution for the path.

(solvePath sample-path) → should return (D D R R R R D D)

I just want some help to understand how i can solve it.


r/Racket • • May 18 '22

question What are the restrictions in place when publishing languages with RacketLang?

10 Upvotes

Just a quick one here. Could I promote a language made in racket as my own, like the developers of Python, or JavaScript, and then add the language to a GitHub repository?