stefan.kruger
2019-7-9 11:00:30

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
2019-7-9 11:00:44

soegaard2
2019-7-9 11:22:51

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


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

soegaard2
2019-7-9 11:29:30

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.


soegaard2
2019-7-9 11:30:16

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


stefan.kruger
2019-7-9 12:02:26

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


soegaard2
2019-7-9 12:10:24

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.


soegaard2
2019-7-9 12:11:03

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


stefan.kruger
2019-7-9 12:11:57

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]))


stefan.kruger
2019-7-9 12:14:22

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]))


stefan.kruger
2019-7-9 12:15:50

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


soegaard2
2019-7-9 12:17:22

Yes, that’t pretty.


stefan.kruger
2019-7-9 12:18:02

Thanks for your help.


soegaard2
2019-7-9 12:18:34

No problem


steveh2009
2019-7-9 13:46:21

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


pocmatos
2019-7-9 13:58:30

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


me1531
2019-7-9 15:07:31

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


lexi.lambda
2019-7-9 15:08:57

what do you mean by “communicate information”


me1531
2019-7-9 15:10:55

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)


alexknauth
2019-7-9 15:12:25

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


me1531
2019-7-9 15:16:28

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


samth
2019-7-9 15:18:35

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


alexknauth
2019-7-9 15:24:55

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?


samth
2019-7-9 15:28:21

@alexknauth no, there isn’t


samth
2019-7-9 15:28:49

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


greg
2019-7-9 15:49:05

Thanks. Will check when back at real computer.


samth
2019-7-9 15:49:30

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


greg
2019-7-9 15:50:55

Yes but thanks for clarifying.


alexknauth
2019-7-9 16:01:22

@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


spdegabrielle
2019-7-9 17:43:02

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. ‘


notjack
2019-7-9 18:36:34

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


notjack
2019-7-9 19:40:49
  1. You can replace (sum (for/list ...)) with (for/sum ...)
  2. 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.

spdegabrielle
2019-7-9 20:13:30

Yes the child- mother asking on their behalf.


greg
2019-7-9 20:59:47

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?


sean
2019-7-9 21:06:07

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
2019-7-9 21:38:38

@joachim.maes has joined the channel


samdphillips
2019-7-9 22:03:45

Desert++


aaronleefuture
2019-7-10 00:03:02

@aaronleefuture has joined the channel



greg
2019-7-10 00:50:35

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


notjack
2019-7-10 01:11:30

Like a skillet of fish?


abmclin
2019-7-10 01:25:21

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


notjack
2019-7-10 05:40:04

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