k
2021-3-24 09:43:26

Hello guys ! I’m using the Magic Racket VSCode extension. Can’t find a way to format code properly. I’d like to inline brackets properly. Could you advise me the way?


soegaard2
2021-3-24 10:38:15

@k What do you mean by inline? Here I would just put the cursor before the d in define, and delete backwards.


raoul.schorer
2021-3-24 10:46:43

If you mean using brackets as in C by making indented blocks with brackets by themselves, that’s not recognized as good style in Lisp languages such as racket


raoul.schorer
2021-3-24 10:49:08

Here, it should begin with (define ... And end with ... (- n 3))))))


raoul.schorer
2021-3-24 10:49:38

No new line before ending brackets


k
2021-3-24 10:50:58

@soegaard2 thanks you for the reply . Yes I mean to get (define (f n)


soegaard2
2021-3-24 10:53:58

There might be a key combination for “delete surrounding whitespace”, but I don’t know VSCode that well.


soegaard2
2021-3-24 10:55:20

k
2021-3-24 11:01:41

I’ve just started exploring racket so I wonder if there is a code format tool or plugin for vscode


k
2021-3-24 11:02:12

like for example in scala https://scalameta.org/scalafmt/


soegaard2
2021-3-24 11:08:47

If you are beginning with Racket, I recommend using DrRacket at least for the first month. It looks primitive at first sight, but don’t underestimate it :slightly_smiling_face: It has excellent error reporting, allows images in both the programs and in the result. If/when you get into macros, it is invaluable.


kellysmith12.21
2021-3-24 11:15:09

(The arrows in DrRacket are super helpful!)


k
2021-3-24 11:22:18

thanks will try DrRacket It seems scary at glance :zipper_mouth_face::slightly_smiling_face:


laurent.orseau
2021-3-24 11:31:13

Ask for help here if you’re stuck, but it should be fairly intuitive nonetheless


soegaard2
2021-3-24 11:48:26

@k John has a short tutorial - maybe too short. https://www.youtube.com/watch?v=T4Myub8sJH0&list=PLD0EB7BC8D7CF739A


soegaard2
2021-3-24 11:49:22

chansey97
2021-3-24 11:49:45

I use emacs + racket-mode to write code and use DrRacket to inspect and debug complicated projects.


chansey97
2021-3-24 22:26:48

How to elegantly collect the results from “delimited continuation world”? For example, the following code uses reset/shift to solve 3-SAT problem, which collects all the b1 b2 b3 that satisfy the boolean formula.

Solution–1 (imperative way): (define (flip) (shift c (begin (c #t) (c #f) (void)))) (define (test-sat) (define acc-lst '()) (reset (let ((b1 (flip)) (b2 (flip)) (b3 (flip))) (let ((r (or (and (not b1) b2) (and b1 (not b3))))) (if r (set! acc-lst (cons `(,b1 ,b2 ,b3) acc-lst)) #f)) )) (reverse acc-lst) ) (test-sat) ;; => '((#t #t #f) (#t #f #f) (#f #t #t) (#f #t #f)) Solution–2 (functional way): (struct SOME (x) #:transparent) (struct NONE () #:transparent) (define (flip) (shift c (let* ((c1 (c #t)) (c2 (c #f))) (match c1 [(SOME x) (match c2 [(SOME y) (SOME (append x y))] [(NONE) (SOME x)])] [(NONE) (match c2 [(SOME y) (SOME y)] [(NONE) (NONE)])])) )) (define (test-sat) (match (reset (let ((b1 (flip)) (b2 (flip)) (b3 (flip))) (let ((r (or (and (not b1) b2) (and b1 (not b3))))) (if r (SOME `((,b1 ,b2 ,b3))) (NONE))) )) [(SOME x) x] [(NONE) '()]) ) (test-sat) ;; => '((#t #t #f) (#t #f #f) (#f #t #t) (#f #t #f)) These two solutions return the same result, but which one is better?

Notice that Solution–1 is more intuitive, but uses mutation. Solution–2 is more functional, but less intuitive (IMO). Perhaps there is better idiom that makes the code intuitive and functional? Thanks.