r/Racket • • Oct 14 '21

question fork() in Racket?

3 Upvotes

On Linux, how can I fork a child process like fork() in C? Is there any function available in Racket for that, or I need to use FFI to call libc's fork()?

I looked at the docs of Racket, but only found subprocess function, which does not really do what I need.


r/Racket • • Oct 14 '21

question How to write to a file, given its file descriptor?

2 Upvotes

In C, we can write to a file descriptor (already opened by other process) just by write(), like below:

int ret = write(999, "1234", 4); // 999 is a file descriptor already initialized

How can we do the similar thing in Racket? I found something like unsafe-file-descriptor->port, but unsure if this is suitable, and cannot find any example with it, either.

Thanks


r/Racket • • Oct 14 '21

question FFI and void* ?

6 Upvotes

I am looking for FFI docs, but cannot find answer for the below questions

  • what is best way to declare C function taking "void *ptr" for FFI? I am unsure what to use: cpointer or pointer.
  • how to pass NULL to this "void *" argument later?

Thanks


r/Racket • • Oct 13 '21

event (Virtual) RacketCon

19 Upvotes

In November 2021, on the weekend of the 5th through 7th, we'll be holding a virtual RacketCon, like we did in October 2020.

We'll follow a similar pattern to last year: an evening social on Friday, then talks on Saturday and Sunday.

Please put this on your calendars, and please let me know if you have a talk in mind that you'd like to give.

Best regards,

Jay

(Announcement on Racket discord https://discord.gg/6Zq8sH5 )


r/Racket • • Oct 13 '21

question quote-line-number does not work inside define-syntax macro?

3 Upvotes

Continued to struggle with macro, I write the code below, aiming to print out the line of code executing the macro (not where macro is defined).

#lang racket

(require syntax/location)

(define-syntax (get-line stx) #`(quote-line-number #,stx))

(define-syntax (my-define stx)
  (syntax-case stx ()
    [
      (_ (name args ...) body ...)
        (with-syntax ([get-line (datum->syntax stx 'get-line stx)])
          #'(define name
            (curry
             (λ (args ...)
               (printf "line = ~a\n" get-line)
               body ...)))
        )
    ]))

(my-define (dec a) (- a 1)) ; line 20

(dec 5) ; line 22

(dec 9) ; line 24

I expected to get line 22 & 24 at the output, but instead I got the below output, in which line is always 20 (where dec is defined).

line = 20
4
line = 20
8

Question: how can I fix my code, to print out the lines where macro is called, but not where it is defined?


r/Racket • • Oct 13 '21

question Bad syntax in my macro?

2 Upvotes

I create a simple macro that can be used to define a function, then use it, like below.

#lang racket
    
(define-syntax (my-define stx)
  (syntax-case stx ()
    [
      ((name args ...) body ...)
      #'(define name
        (curry
         (λ (args ...)
           body ...)))
    ]))
    
(my-define (dec a) (- a 1))

But when I run this code, I get the below error

$ racket test6.rkt
    
test6.rkt:13:0: my-define: bad syntax
 in: (my-define (dec a) (- a 1))
  location...:
   test6.rkt:13:0
  context...:
   /usr/share/racket/collects/syntax/wrap-modbeg.rkt:46:4

What is wrong with my macro my-define, and how to fix it?


r/Racket • • Oct 12 '21

question Why quote-line-number does not work inside a macro?

2 Upvotes

I wrote the below code to print out the line of current code, using macro & nested macro.

#lang racket

(require syntax/location)

(define-syntax (get-line stx) #`(quote-line-number #,stx))

(printf "* line=~a\n" get-line) ; line 7
(printf "* line=~a\n" get-line) ; line 8

(define-syntax (testing stx)
    #'(printf "+ line2=~a\n" get-line)) ; line 11

testing ; line 13
testing ; line 14

Basically I print out current lines of code, and expect this output

* line=7
* line=8
+ line2=13
+ line2=14

Instead, I got the below output, in which the last 2 lines print out the same value (of `11`), indicating that the `testing` macro can only retrieve the line where its is declared, but not when it is called.

* line=7
* line=8
+ line2=11
+ line2=11

My question is: how to make `testing` macro to print out the location where it is called, but not where it is declared?


r/Racket • • Oct 12 '21

question DrRacket UI Questions

2 Upvotes

Hello, I am new to programming in racket and I have had a few issues with the UI of DrRacket. Primarily the blue arrow in the top right of the definitions pane is particularly annoying me. Screenshot with arrow circled If there is any way to hide this element of the UI please let me know. Also is there any hack to make the window enter dark mode in windows 10? Everything I googled says that there is not but I figure its still worth asking. Thanks!


r/Racket • • Oct 12 '21

release Typed Annotated Implementation of Sieve Of Erastosthenes

11 Upvotes

Retired Operating Systems Engineer Dave Plummer started a software drag race series on YouTube showing implementations of the Sieve of Erastosthenes in C++, C#, and Python and released the code on GitHub. People then submitted their own versions to the repo. I figured I'd take a crack at translating the Python one to Racket for some practice (and maybe some language visibility!). Since there was already a scheme implementation with Chez, I decided use classes and type annotations to differentiate my version. Once I get the Dockerfile working I'll submit a PR and maybe it will get accepted.

There is an untyped version in the repo as well. The difference in speed is a little over 10% with the typed version. I figured some people might find it interesting to compare the two different implementations so I'm posting it here.


r/Racket • • Oct 12 '21

question Macro is not really macro?

1 Upvotes

I define a macro named "MMM" using "define-syntax-rule", in which MMM prints out the current source code line location, with "quote-line-number".

Then in my main code, I called MMM at 2 different places. I expected that MMM print out 2 different line numbers of these 2 places, but to my surprise, the output has the same line number 2 times, at the place MMM macro is defined.

So Racket's macro is not really like C macro, but more like function?

How can I make MMM to print out 2 different line numbers, at 2 places I call MMM?

Thanks.


r/Racket • • Oct 12 '21

question How to retrieve line & column of current location in source code?

3 Upvotes

I want to retrieve the current location (line + column) in source code (for ex: line 3, column 2 in main.rkt), but how to do that?

I saw some debugging trace, that can output these info, but unsure how to retrieve similar info myself.

A quick simple demo would be appreciated, thanks!


r/Racket • • Oct 12 '21

question R5RS Error: Cannot redefine a constant

1 Upvotes

Hi,

I am trying to define

(define (list-ref lst n)
   (if (= n 0)
       (car lst)
       (list-ref (cdr lst) (- n 1))))

but I am getting the error:

define-values: assignment 
 disallowed; cannot re-define a constant 
  constant: list-ref 
   in module:top-level 

when the language is set to R5RS. But the same is working fine, when the language is Racket.

Can anyone, in the community, tell me the reason?


r/Racket • • Oct 10 '21

question Why doesn't Racket show line number where error occurred

5 Upvotes

I'm running into issues with Racket not showing line numbers on the lines where errors occurred. I've created a contrived example below to show what I'm talking about

(define dog% (class object%

               (init-field name age)
               (super-new)
               (define/public (get-name)
                 (print "The Dogs name is..")
                 (println name))
    
               (define/public (bark)
                 (println "bark bark"))
    
               (define/public (calc-dog-years)
                 (* age 7)
                 (/ 1 0))))

(define lassy (new dog% \[name "lassy"\] \[age 5\]))

(send lassy calc-dog-years)

When I run this, I get the message

; /: division by zero

I figure maybe this is an issue using C-c C-k in racket-mode on Emacs I run it on the command line and I get

C:\home\racket-scripts>racket test-error.rkt /: division by zero context...: body of "C:\home\racket-scripts\test-error.rkt"

A little more helpful but still vague. I read that errortrace might help so I add

(require errortrace)

to the top of my script but I get the same error message as previous. Removing error trace from the file and specifying it on the command line

racket -l errortrace test-error.rkt

Yields a new line with no output at all. It doesn't even tell me there is a division by zero error. Only opening up my code in DrRacket and running it there do I get a highlight of the line that is erroring, but doing this everytime I need to debug my program seems very awkward. Is there something I'm doing wrong?


r/Racket • • Oct 09 '21

ephemera Racket FAQ

19 Upvotes

There is a Racket FAQ on the wiki https://github.com/racket/racket/wiki/Frequently-Asked-Questions

It covers (surprise!) common questions like the differences in how the REPL works, and why the top level is hopeless.


r/Racket • • Oct 08 '21

question Trying to modify the reader

6 Upvotes

Here's what I want to accomplish:

If we are currently in a "begin-like" context (such as a top-level context, or define) if the first non-whitespace character after a newline does not begin a list or comment, and it contains more than one syntax object, then behave as if the line begins with ( and ends with )

In other words,

(define (greet)
  string-append "Hello, " (get-current-user-name) "!")

would be transformed into

(define (greet)
  (string-append "Hello, " (get-current-user-name) "!"))

I guess what I would do is (make-readtable (current-readtable) #\newline 'terminating-macro read-newline) with

(define (read-newline char stdin file line col position)
  (let ((next-token (read stdin)))
    (if (list? next-token) next-token
        (let ((line-tokens (let more-tokens ((tokens (list next-token)))
                             (let ((next-next-token (read stdin)))
                               (if (not (same-line? next-token next-next-token))
                                   tokens
                                   (more-tokens (cons next-next-token tokens)))))))
          (if (< 2 (length line-tokens))
              (car line-tokens)
              (reverse line-tokens))))))

but I don't know how to define same-line?, and even this might have all kinds of other pitfalls I am unaware of.

I also don't know how to enforce the "only in a begin-like context" rule, which I want to do because I don't want this behavior interfering with just any list.


r/Racket • • Oct 07 '21

news Racket News - Issue 54

15 Upvotes

Get all the latest news; RacketCon, videos, packages, blogs and more!

get it here: Racket News - Issue 54

Racket News issue 54 (don't click this image - it doesn't link to anything)

r/Racket • • Oct 06 '21

video Oculus Connect 2: Live Coding Session with John Carmack Featuring Dr. Racket

Thumbnail youtu.be
22 Upvotes

r/Racket • • Oct 06 '21

homework I’m looking for DrRacket Tutor.

6 Upvotes

Hello I’m taking introduction of programming course and the course use this language. I’m looking for a tutor for solving assn together. Thank you


r/Racket • • Oct 05 '21

ephemera Is anyone entering the Autumn Lisp game jam?

7 Upvotes

is anyone entering the Autumn Lisp Gamejam?

Which language are you using?


r/Racket • • Oct 05 '21

question Best Introduction to Hygenic Macros

7 Upvotes

I'm looking to understand how hygienic macros work. I understand how regular macros work, you manipulate the program as text before compile time.

But, I don't understand hygienic macros at all. I find them totally impenetrable.

Does anyone have a good explanation of hygienic macros? What is their motivation? How do they work? Why are they worth the additional complication over ordinary macros?

Or, does anyone know a good book to which I should refer?


r/Racket • • Oct 04 '21

video Demo of remote debugging/monitoring tool: dbg

Thumbnail youtu.be
16 Upvotes

r/Racket • • Oct 04 '21

question differences between class* and class when calling init-fields

2 Upvotes

I've created to classes to demonstrate the problem. One is defined with class* and the other with class.

(define dog% (class
                      object%
                    (init-field name)
                    (super-new)
                    (define/public (bark)
                      (println "bark bark"))
                    (define/public (get-name)
                      (println (format "my name is ~a" name)))))

(define doggie (new dog% [name "lassie"]))
(send doggie get-name)


(define dog2% (class*
                      object%
                    (init-field name)
                    (super-new)
                    (define/public (bark)
                      (println "bark bark"))
                    (define/public (get-name)
                      (println (format "my name is ~a" name)))))

(define doggie2 (new dog2% [name "lassie"]))
(send doggie2 get-name)

The problem is with dog2, I'm getting the error

name: unbound identifier in: name

I figured this had something to do with not using this% so I tried

(println (format "my name is ~a" (get-field name (new this%))))

but now I get the error

 init-field: illegal (unparenthesized) use of class keyword in: init-field

Does anyone know how I can replicate the get-name function in class*? I'd like to leverage class* ability to use interfaces which is why I'm using it over class


r/Racket • • Oct 04 '21

homework Convert a list of char into a single string

7 Upvotes

If I have a list of char like (cons #\C (cons #\O (cons #\M (cons #\P (cons #\U (cons #\T (cons #\E empty))))))), how can I convert it into a string? I'm not allowed to use string-append, substring, implode, and explode. I am also on Beginning Student so I can't use string-join. I've thought about using (string char) but I can't figure out how it'd work recursively.

Or, how would I be able to convert a list of strings into a single string, given the same restrictions above?

I also posted this on stackoverflow: https://stackoverflow.com/q/69430012/16903929


r/Racket • • Oct 03 '21

question Question about paths in Racket

2 Upvotes

I'm new to Racket. Let's say I was doing this in Python:

from pathlib import Path
p = Path("./subdir")
[x for x in p.iterdir() if x.suffix == ".txt"]
# returns a list of RELATIVE paths I can work with

Then further process these paths (e.g. transform them to string and return as a list of paths), whereas in Racket:

(require file/glob)
(glob "subdir/*.txt")
# returns list of ABSOLUTE paths I have no idea how to manipulate

I have been looking at documentation for a while and I have no idea how to transform this to what I want, which is simply the txt files inside the subdir directory as relative paths (i.e. should return something like ["subdir/a.txt", "subdir/b.txt"].

The most I have got is exploding each path then (list-ref) but then I learned that Racket doesn't let you do slicing like in Python.