@tosxychor has joined the channel
anyone using racket-mode on emacs?
yes?
racket-mode is not active by default when i open a .rkt file
here is my current config (require 'lang-racket)
(use-package racket-mode
:ensure t
:mode "\\.rkt[dl]?\\'")
Here is my .emacs: https://gist.github.com/soegaard/54c39c881a2db5a7bd7fbaed0dd0904e
thanks
;; Laptop: up-arrow key is broken, so we need an alternative.
(global-set-key (kbd "M-p") #'beginning-of-buffer)
I had forgotten that M-p wasn’t standard…
And now I have a laptop with a working up-arrow.
:slightly_smiling_face:
which one is this? (add-to-list 'auto-mode-alist '("\\.scrbl\\'" . racket-mode))
What do you mean, by “which one” ?
i mean what extension is .scrbl ?
That’s scribble files. There aren’t a dedicated scribble-mode, so I just use racket-mode.
ok, all right
Is there a cleaner way to do this? (match-define (vector _ ..3 child1 _ child2 _ ..2 childs my-other-children _ another-child _ ..2) children)
I want children at some very specific indices
like specifying 3, 5, 8, 9, 11 indices, and matching those in one single statement?
Personally I think it would be cleaner to just use vector-ref
Hmmm, I wanted to save space and do it in one line instead of doing say 5 vector-ref definition lines however… Is there any way to do that in one like instead of: (define child1 (vector-ref children 4)
(define child2 ...
?
(match-define (list c1 c2 c3 c4 c5) (map (lambda (n) (vector-ref children n)) (list 3 5 8 9 11))
Not much cleaner though.
Amazing! Thanks!
In what contexts can you use define
/match-define
? I recently learned you can use them in the then-body/body of cond
/match
. Can you use them before the body of a lambda? Any general rules to follow here?
I thought you had to use let
, etc in these contexts but apparently I was wrong lol…
You can use define
and friends in any “internal definition context” and also at the module body level.
Several forms introduce internal definition context. E.g., let
, match
, cond
, when
, lambda
.
In the doc, if you see body ...
, that position should be in the internal definition context.
One exception is begin
and splicing-*
forms. These forms splice their body, so they essentially inherit the context from their parent context. What this means is that:
(define (foo)
(begin (define x 1) x))
is OK, but:
(if #t (begin (define x 1) x) #f)
is not.
Hmm, so I guess I can’t do it in an if
statement either: (define (do-things)
(if (> 5 0)
(begin (match-define (vector racket-child) (vector 'racket))
(println racket-child))
(list)))
The above raises an error
the function signature seems to be (if test-expr then-expr else-expr)
, so I guess that makes sense. Must be body, not expr
Would https://docs.racket-lang.org/reference/contracts.html be a good place to read to get a good grasp on this?
sorawee just described why you get an error for that case, but it’s because you’re using begin
you can introduce a local definition context anywhere you want by using let
the let
doesn’t even have to have any bindings, so you can replace that (begin ...)
with (let () ...)
Some people find (block ...)
more readable than (let () ...)
because the intent of introducing a local definition context is a little clearer. The block
syntax is available in the racket/block
module: https://docs.racket-lang.org/reference/block.html?q=block#%28form._%28%28lib._racket%2Fblock..rkt%29._block%29%29\|https://docs.racket-lang.org/reference/block.html?q=block#%28form._%28%28lib._racket%2Fblock..rkt%29._block%29%29
FWIW, let
, if
, lambda
, etc. are syntaxes, so I wouldn’t use the word “function signature” with them. I would instead call it “grammar”.