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?
@k What do you mean by inline? Here I would just put the cursor before the d in define, and delete backwards.
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
Here, it should begin with (define ...
And end with ... (- n 3))))))
No new line before ending brackets
@soegaard2 thanks you for the reply . Yes I mean to get (define (f n)
There might be a key combination for “delete surrounding whitespace”, but I don’t know VSCode that well.
This page shows how to find out: https://code.visualstudio.com/docs/getstarted/keybindings
I’ve just started exploring racket
so I wonder if there is a code format tool or plugin for vscode
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.
(The arrows in DrRacket are super helpful!)
thanks will try DrRacket It seems scary at glance :zipper_mouth_face::slightly_smiling_face:
Ask for help here if you’re stuck, but it should be fairly intuitive nonetheless
@k John has a short tutorial - maybe too short. https://www.youtube.com/watch?v=T4Myub8sJH0&list=PLD0EB7BC8D7CF739A
I use emacs + racket-mode to write code and use DrRacket to inspect and debug complicated projects.
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.