
Working my way through old Advent of Code problems as a means of learning Racket. How could I tail-recursify the (remove-red ..)
procedure? (define (remove-red json-data [total 0])
(cond [(number? json-data) (+ total json-data)]
[(and (hash? json-data) (not (hash-has-value? json-data "red")))
(+ total (sum (for/list ([v (in-hash-values json-data)]) (remove-red v total))))]
[(list? json-data)
(+ total (sum (for/list ([v (in-list json-data)]) (remove-red v total))))]
[else total]))


@stefan.kruger Your definition of remove-red
is short and precise. Why do you want to “tail-recursify” it?

- Your
total
seems to always be 0. You could just eliminate it. - As JSON is essentially a tree, a straightforward recursion like what you have right now seems appropriate already.
- But if you really want to accomplish that, https://en.wikipedia.org/wiki/Continuation-passing_style could be a way.

Note: In Racket one does not need to worry about hitting a stack limit (there is none). Even though the calls are not tail recursive.

(As long as there is (non-stack) memory available of course)

I (perhaps erroneously) understood it to be good practice.

Not for all functions. In Scheme most loops are written using tail recursive functions. In your example the use of for/sum (oh that’s thing to improve) instead of an explicit loop means you don’t need to do anything.

Tip: Use for/sum
instead of (sum (for/list ...)
.

Oh yes - total is indeed superflous: (define (remove-red json-data)
(cond [(number? json-data) json-data]
[(and (hash? json-data) (not (hash-has-value? json-data "red")))
(sum (for/list ([v (in-hash-values json-data)]) (remove-red v)))]
[(list? json-data)
(sum (for/list ([v (in-list json-data)]) (remove-red v)))]
[else 0]))

Nice. Now it looks like: (define (remove-red json-data)
(cond [(number? json-data) json-data]
[(and (hash? json-data) (not (hash-has-value? json-data "red")))
(for/sum ([v (in-hash-values json-data)]) (remove-red v))]
[(list? json-data)
(for/sum ([v (in-list json-data)]) (remove-red v))]
[else 0]))

Recursion is a nice way to deal with recursive structures, unsurprisingly.

Yes, that’t pretty.

Thanks for your help.

No problem

Would be nice if those who couldn’t make it to Racket School 2019 could purchase the presentations for a fee.

It’s not the videos but at least the program is online: https://school.racket-lang.org/2019/plan/

does anyone know if it’s possible to communicate information, at compile-time, between the pattern and template of a match-expander?

what do you mean by “communicate information”

I’m working on an expander where I have to know, in the template, which identifiers are bound in the pattern. (To be clear, I have to know the actual identifiers, not just their values)

syntax-parse
and syntax
communicate information between them at compile time by binding the pattern variables with ellipsis depths

here’s some context in case there’s a better way: https://gist.github.com/disconcision/5c80494531f7ae0b8c5ac5980ffc0395

@me1531 ah, what you are talking about is the relationship between the match pattern and the match RHS

Is there a way that a match
pattern can cause a define-syntax
or let-syntax
? (define-syntax-parser print-slv
[(_ x:id) (println (syntax-local-value #'x))])
(match 1
[(bind-syntax x 5)
(print-slv x)])
so it works like (let-syntax ([x 5]) (print-slv x))
, and prints 5
at compile time?

@alexknauth no, there isn’t

@greg here’s an interesting racket-mode bug:

Thanks. Will check when back at real computer.

If you don’t see the issue, it’s #;
after ;
being parsed as an s-exp comment

Yes but thanks for clarifying.

@me1531 syntax-parse allows something like this: #lang agile
(require syntax/parse)
(define-syntax-parser print-slv
[(_ x:id) (println (syntax-local-value #'x))])
(syntax-parse #'1
[{~and _ {~do (define-syntax x 5)}}
(print-slv x)])
prints 5
at compile time

Hi all; I’ve just been contacted by the mother of a 11yo who is after some assistance ‘someone in the community here in the UK who may be able to assist with her language that she’s been making in Racket. ‘ I’m the wrong person because I’ve not made a #lang myself - is anyone suitable in London? Failing that, I’d appreciate any advice on how I could support her ‘**** is 11 years old, and has a great interest in Functional Programming. She has taught herself several programming languages since the age of about 6 ( with library books and free online video tutorials) first Python, Javascript, Swift and most recently this past year - Clojure. Clojure is her favourite language and she also is now exploring and using Racket to create her own language. ‘

@spdegabrielle Just to clarify, it’s the child who is asking for advice making languages, right? Not the mother?

- You can replace
(sum (for/list ...))
with(for/sum ...)
- Tail recursion is usually not helpful if the state you’d have to pass around gets bigger and bigger, like a list or tree of some sort that describes where you are in the recursive process. That’s basically the same amount of space consumed by racket when using normal recursion anyway. In those cases, tail recursion makes things messier without doing much else. I’d recommend against it here.

Yes the child- mother asking on their behalf.

Whoa. I checked SLC weather, wondering what clothes to pack for RacketCon. It says the highs Wednesday through Sunday will be close to 100F. :thermometer: Is that typical?

I’m not sure where you’re from, but 100F in SLC is wayyyy different than an East Coast 100F. It’s been in the 90s all this week and I’ve been completely comfortable because there’s basically no humidity.

@joachim.maes has joined the channel

Desert++

@aaronleefuture has joined the channel


Oh good. Yeah, low-humidity heat is a whole other kettle of fish. Like… um… I guess… a drier, less-hot-feeling kettle.

Like a skillet of fish?

Due to the desert setting, SLC typically will get much cooler at night though, often by as much as 20 degrees or more so might be good idea to pack a light coat as well

Deserts are bizarre. In the span of 12 hours it goes from blazing hot to so cold you need quilts and a space heater.