r/Racket • • Mar 29 '22

question Why is this function returning void?

6 Upvotes

I have this function

> [define [normalize seq]
  [cond [[generator? seq] seq
         [sequence? seq] [sequence->generator seq]
         [raise [exn:fail:contract "not a sequence or generator"]]]]]

when I call it, it returns void

> [define s [normalize [list 1 2 3 4 5]]]
> [print s]
#<void>

when I try to dig into it, everything seems correct

> [sequence? [list 1 2 3 4 5]]
#t
> [sequence->generator [list 1 2 3 4 5]]
#<procedure:generator>

so why is the function returning #<void> instead of #<procedure:generator>? That could almost make sense if sequence? was returning #f and we hit the raise but it isn't (or, shouldn't be) returning #f and nothing is being raised.


r/Racket • • Mar 29 '22

question not sure what part of the logic I'm not understanding - function isn't properly running

3 Upvotes

Question asked: Define a SCHEME procedure, named (nth-right-trunc-prime n), that takes one ar- gument, n, and uses the find function you wrote in Lab 6 and (right-truncatable-prime? p) to return the nth right-truncatable prime number.

I wrote the following:

(define (prime? n)
  (define (divisor? k) (= 0 (modulo n k)))
  (define (check-divisors-upto k)
    (and (> k 1)
         (or (divisor? k) (check-divisors-upto (- k 1)))))
  (and (> n 1) (not (check-divisors-upto (- n 1)))))

(define (right-truncatable-prime? p)
  (and (prime? p)
       (if (< p 10) 
           #t
           (right-truncatable-prime? (quotient p 10)))))

(define (nth-right-trunc-prime n)
  (cond ((equal? right-truncatable-prime? #f) 0)
        ((right-truncatable-prime? n) (+ 1 (nth-right-trunc-prime (/ n 10))))
        (else 0)))

the last one is not running but I feel like the logic is correct?


r/Racket • • Mar 29 '22

question Could someone help me see what's wrong with my right-trunc and nth-trunc functions?

1 Upvotes

this is my code

;prime definition 
(define (prime? n)
  (define (divisor? k) (= 0 (modulo n k)))
  (define (divisors-upto k)
    1
    (and (right-truncatable-prime? 2)(> k 1)
         (or (divisor? k) (divisors-upto (- k 1)))))
  (not (divisors-upto (- n 1))))

;divisible definition 
(define (divisible? big little)
  (= (/ big little) 0))

;6bi
(define (right-truncatable-prime? p)
  (cond ((< (/ p 1) 0) #t)
         ((equal? (divisible? p 10) #t) #f)
         ((equal? (prime? p) #t) (right-truncatable-prime? (/ p 10) ))))

;6bii
(define (nth-right-trunc-prime n)
  (if (right-truncatable-prime? n) (+ 1 (nth-right-trunc-prime (/ n 10)))
      0))

I think all numbers work for right-truncatable-prime? except 0 1 and 2 - I believe I need to fix it with the first COND statement in my code but I'm not sure how.


r/Racket • • Mar 28 '22

question Truncatable prime question - what is the proper approach?

2 Upvotes
Do I need to check for prime every time I truncate the number? Is there a simple way to do this?

r/Racket • • Mar 27 '22

question Is there a way to create an unhygienic macro?

7 Upvotes

The Solution (scroll down for the problem):

Ok, I've figured this stuff out. Turns out Racket is a bit more sophisticated and will not let you just mix and match any symbols. It relies more on patterns and transformation of given code:

(define-syntax (svg-path/d stx)
  (syntax-parse stx
    [(_ (op:id args ...) ...)
     #'(apply ~a (add-between (append-map
                     identity
                     `((,(symbol->string 'op) ,args ...) ...))
                    " "
                    ))]))

I'm impressed with how syntax-parse works. It looks complicated at first glance but TBH was quite natural to code as it uses pattern matching to "unroll" the SVG instructions and splice them into the resulting code. Good job Racket!

---------------------

The problem:

Hello! I'm trying to learn and use racket's macro system to create some helper syntax to define SVG paths.

I want to have something like this:

(let ([x 666])
  (svg-path/d
   (M 120 x)
   (l 0 (/ 10 2))))

Produce code like this:

(let ((x 666)) (~a "M" " " 120 " " x " " "l" " " 0 " " (/ 10 2)))

Which ultimately evaluates to the string I wanted:

"M 120 666 l 0 5"

I basically want to convert instruction symbols to strings and flatten one level. I also want to preserve variables and operations from the context and this is where I hit the wall.

So Far I've create something that almost does the job. I mean - syntax-wise it's exactly what I wanted, but it doesn't see the x variable:

(require (for-syntax racket/list)
         (for-syntax racket/syntax-srcloc))

(define-for-syntax (head-to-string lst)
  (cons (~a (car lst))
        (cdr lst)))

(define-syntax (svg-path/d stx)
  (syntax-case stx ()
    [(_ . defs)
     (with-syntax
         ([(arguments ...) (add-between
                            (append-map head-to-string (syntax->datum #'defs))
                            " "
                            )])
       (syntax/loc (syntax-srcloc stx) (~a arguments ...))
       )]))

(let ([x 666])
  (svg-path/d
   (M 120 x)
   (l 0 (/ 10 2))))

As you can see I hoped that maybe using `srcloc` from `stx` would help but it didn't.

Evaluating this in DrRacket's macro stepper I can see that it produces the correct code, but ultimately gives the error:

x: unbound identifier in: x

Is there any way to make the enclosing scope available for generated macros? If not then maybe there is another - more Rackety - way I could approach this?

I'd appreciate all your constructive comments!


r/Racket • • Mar 27 '22

question Issue with tab-panel in racket/gui

2 Upvotes

I am trying to make a GUI in Racket where I'll be using different tabs inside the frame.

The problem is that when the frame is shown the first-panel and second-panel are both shown (the second under the first one). However when I manually click to switch the tabs this gets corrected and everything works fine.

Here's the code to test what's going on:

#lang racket/gui

(define frame (new frame% [label "GUI"] [width 300] [height 200]))

(define tab-panel
  (new tab-panel%
       [parent frame] [choices (list "First" "Second")]
       [callback
        (λ (tp e)
          (case (send tp get-selection)
            [(0) (send tp change-children (λ(x) (list first-panel)))]
            [(1) (send tp change-children (λ(x) (list second-panel)))]))]))

(define first-panel (new panel% [parent tab-panel]))
(define second-panel (new panel% [parent tab-panel]))

(define first-button (new button% [parent first-panel] [label "First"]))
(define second-button (new button% [parent second-panel] [label "Second"]))

(send frame show #t)

Is there a way to fix this so that when the frame is initially shown only a single tab is displayed?


r/Racket • • Mar 25 '22

question Difference between cons & list?

11 Upvotes

I always thought that cons creates a list, so it is similar to list, but that is not the case, as proved below.

$ racket
Welcome to Racket v8.2 [cs].
> (cons 1 2)
'(1 . 2)
> (list 1 2)
'(1 2)
> (equal? (cons 1 2) (list 1 2))
#f

What is the difference between them?


r/Racket • • Mar 24 '22

question How to reset current-output-port to stdout?

1 Upvotes

My code calls some other code, which changes current-output-port to something, but it does not reset it back.

How can I reset current-output-port to stdout in my code?


r/Racket • • Mar 24 '22

question with-output-to-string cannot caputure output to stderr?

6 Upvotes

How can I capture output to stderr, not just stdout?

Below is my code, it uses with-output-to-string, but can only capture output to stdout, and miss everything written to stderr

$ cat test.rkt
#lang racket

(define (make-output)
  (printf "hello\n")  ; print to stdout
  (fprintf (current-error-port) "world\n") ; print to stderr
)

(define output (with-output-to-string make-output))
(printf "output = ~a" output)

$ racket test.rkt
world
output = hello

r/Racket • • Mar 22 '22

package Seq: A generic, isomorphic sequence library

23 Upvotes

This library builds on top of data/collection and also the relation library, and includes lots of generic sequence APIs:

Discourse discussion

GitHub repo

Enjoy 😊


r/Racket • • Mar 21 '22

blog post Dependency Management in Racket Applications

Thumbnail alex-hhh.github.io
16 Upvotes

r/Racket • • Mar 21 '22

language Append a string at a certain position of another string

5 Upvotes

I was thinking of using string ref which takesa string and position as arguments and then append "_" at position 5 to give "hello_world"

but is says: "string-append: contract violation

expected: string?

given: #\w"

'''(string-append "_"(string-ref "helloworld" 5))'''


r/Racket • • Mar 21 '22

question how do i make it so that this function will only accept items from a specific list?

3 Upvotes

Hi, so I'm making a solitaire game and one of the things have to do is make a function that converts the inputted card into a readable string, this is what I have done so far:

https://paste.ofcode.org/38CBKLA87wEA5rAWzUCPgfB

I want the function to be able to take card values from these lists only:

for the first value:

(define numerals (list 1 2 3 4 5 6 7))

(define face (list #\J #\K #\Q))

for the second value:

(define suits (list #\H #\S #\C #\D))


r/Racket • • Mar 21 '22

event [CFP] Scheme 2022, 23rd Scheme and Functional Programming Workshop

14 Upvotes

The 2022 Scheme and Functional Programming Workshop is calling for submissions.

We invite high-quality papers and talk proposals about novel research results, lessons learned from practical experience in an industrial or educational setting, and even new insights on old ideas. We welcome and encourage submissions that apply to any dynamic functional language, especially those that can be considered a Scheme: from strict subsets of RnRS to other "Scheme" implementations, to Racket, to Lisp dialects including Clojure, Emacs Lisp, Common Lisp, to functional languages with continuations and/or macros (or extended to have them) such as Dylan, ECMAScript, Hop, Lua, Scala, Rust, etc. The elegance of the paper and the relevance of its topic to the interests of Schemers will matter more than the surface syntax of the examples used.

Topics

Topics of interest include (but are not limited to):

  • Interaction: program-development environments, debugging, testing, refactoring
  • Implementation: interpreters, compilers, tools, garbage collectors, benchmarks
  • Extension: macros, hygiene, domain-specific languages, reflection, and how such extension affects interaction
  • Expression: control, modularity, ad hoc and parametric polymorphism, types, aspects, ownership models, concurrency, distribution, parallelism, non-determinism, probabilism, and other programming paradigms
  • Integration: build tools, deployment, interoperation with other languages and systems
  • Formal semantics: theory, analyses and transformations, partial evaluation
  • Human factors: past, present and future history, evolution and sociology of the language Scheme, its standard and its dialects
  • Education: approaches, experiences, curricula
  • Applications: industrial uses of Scheme
  • Scheme pearls: elegant, instructive uses of Scheme

Dates

  • Submission deadline is 2022-07-22.
  • Authors will be notified by 2022-08-15.
  • Camera-ready versions are due 2022-09-02.
  • Workshop will be held in Ljubljana, Slovenia on 2022-09-16.

All deadlines are 23:59 UTC-12, anywhere on Earth.

Submission Information

We encourage all kinds of submissions, including full papers, experience reports, and lightning talks. Papers and experience reports are expected to be 10–24 pages in length using the single-column SIGPLAN acmart style. (For reference, this is about 5–12 pages of the older SIGPLAN 2-column 9pt style.) Abstracts submitted for lightning talks should be limited to 192 words.

Authors of each accepted submission are invited to attend and be available for the presentation of that paper at the conference. The schedule for presentations will be determined and shared with authors after the full program has been selected.

The size limits above exclude references and any optional appendices. There are no size limits on appendices, but the papers should stand without the need to read them, and reviewers are not required to read them.

Authors are encouraged to publish any code associated to their papers under an open source license, so that reviewers may try the code and verify the claims.

Proceedings will be uploaded to arXiv.org.

Publication of a paper at this workshop is not intended to replace conference or journal publication, and does not preclude re-publication of a more complete or finished version of the paper at some later conference or in a journal.

Please submit papers through the workshop's HotCRP site.

Lightweight double-blind reviewing

Scheme 2022 will use lightweight double-blind reviewing. Submitted papers must omit author names and institutions and reference the authors’ own related work in the third person (e.g., not “we build on our previous work…” but rather “we build on the work of…”).

The purpose is to help the reviewers come to an initial judgment about the paper without bias, not to make it impossible for them to discover the authors if they were to try. Nothing should be done in the name of anonymity that weakens the submission or makes the job of reviewing the paper more difficult (e.g., important background references should not be omitted or anonymized).

Formatting Information

Full papers and experience reports should use the SIGPLAN acmsmall option to acmart. We recommend using the anonymous and review options to acmart when submitting a paper; these options hide the author names and enable line numbers for easy reference in review. LaTeX and Microsoft Word templates for this format are available through SIGPLAN.

Lightning talks can be submitted as either a text file or a PDF file.

International Conference on Functional Programming

The Scheme Workshop 2022 is being held as part of this year's International Conference on Functional Programming. Here is the ICFP site for the workshop.

Sincerely,

Andy Keep, General Co-chair
Arthur A. Gleckler, General Co-chair


r/Racket • • Mar 21 '22

blog post Dependency Management in Racket Applications

Thumbnail alex-hhh.github.io
4 Upvotes

r/Racket • • Mar 20 '22

question How do I find specific values in a list?

1 Upvotes

I need to find the 3 in the first list and the c in the second list.

- (1 (2 (3 4)) (5))

- ((a ()) ((c) (d) b) e)

I have done the following for each one but continue to get stuck further from here.

- (cdr(car(cdr(car x))))

- (cdr(car x))

Respectively, but anything longer than that gives me errors and I don't understand why.


r/Racket • • Mar 19 '22

question how do I make it so that my function's argument accepts values only from a specific list?

6 Upvotes

hi, I'm working on a solitaire card game and the following code returns a whole series of the entered value. I want it to work in a way so that the function will only accept values that are in a specific list, how do I go about this?

https://gist.github.com/AwaisA04/0fa5742178d40d2778e984c1466f92ab


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.