
@pihentagy has joined the channel

Hi, just started to play with Racket (new to lisps, not new to programming). I started to solve http://adventofcode.com\|adventofcode.com puzzles (<https://adventofcode.com/2020/day/2|this one>) and has a rookie question: How would you write this in a more idiomatic way? Input: i have a line with format: “number-number letter: password” and try to parse the parts

Is there a way to do something and do a pattern match in one step, like in python the list comprehension below?

python equivalent:

I guess there must be a version without a custom https://docs.racket-lang.org/measures-with-dimensions/Compounds.html#%28form._%28%28lib._typed%2Fmeasures-with-dimensions%2Fchemistry..rkt%29._compound%29%29\|compound :stuck_out_tongue:


#lang racket
(define (explode line)
(match-define (pregexp #px"^(\\d+)-(\\d+) (.+?): (.+)$"
(list _ a b letter pwd))
line)
(list (string->number a) (string->number b) letter pwd))
(explode "4-5 abc: abcdef")
;; '(4 5 "abc" "abcdef")

thanks, that’s what I was looking for

Thanks for this solution. Though I could use regexen, I’d better resist to use them.

I am new to lisps, how can one know which function exists in other languages? So whether match-let*
is Racket-specific.

#lang racket
(define (explode line)
(match-let* ([(list policy password) (string-split line ":")]
[(list nums letter) (string-split policy)]
[(list a b) (string-split nums "-")])
(list a b letter password)))

match-let*
is Racket specific. It doesn’t exist in, say, Scheme AFAIK. It’s also not a function, but a macro.

There are many Lisp dialects, and they can be quite different. Racket comes from the Scheme branch of the Lisp family, but Racket include many more features than most other Schemes.

btw is there a birdseye view about how these languages relate to each other? Some kind of Map of lisps?

<https://en.wikipedia.org/wiki/Lisp_(programming_language)#Timeline|timeline from Wikipedia>

That’s certainly not a complete chart, but it does provide some perspective on the history.

I like the match-let*
solution. An alternative: (define (explode line)
(match-define (list policy password) (string-split line ":"))
(match-define (list nums letter) (string-split policy))
(match-define (list a b) (string-split nums "-"))
(list a b letter password))

And one without match
: (define (explode2 line)
(define (split str [pat " "]) (apply values (string-split str pat)))
(define-values (policy password) (split line ":"))
(define-values (nums letter) (split policy))
(define-values (a b) (split nums "-"))
(list a b letter password))

Oh. I missed the numbers were to be converted from strings to numbers, but that’s straightforward to add.


DrRacket used to have a quarter-circle (with the definition under cursor) in the top right corner of the editor, but now I cannot see it. Even after removing ~/.conf/racket
and restarting it. Cannot see where is that option :disappointed:

Try reset settings to defaults in drracket preferences.

It turns to dark mode (my system has dark mode theme) Thats not a problem, but still not seeing the doc popup

@pihentagy They are called “blue boxes”. They only appear if you have turned on “background expansion” (check the dot in the lower right corner of DrRacket).

Hmm. They have been renamed to “signature boxes”:

@pihentagy ^

I have them turned on, haven’t I?

yes - which language are you using?

#lang racket

Okay. I think you need to close DrRacket and start it again.

And then begin to enter something.

Hm, still not there :thinking_face:

Try (define foo (+ 1 2)) in the upper window.

ok, typed that, pressed Ctrl+R

Try clicking on define.

If that doesn’t work - I am out of ideas.


ok, thanks on another linux box it works :zipper_mouth_face:

I’d teach some programming to my kid. Is there a (guided and fun) tutorial for kids for the first real programming language in Racket?

HtDP?


It has rockets :slightly_smiling_face:

How old?

11 yrs

I would start with some pictures. Either from HtDP or the quick tutorial:
https://docs.racket-lang.org/quick/index.html
But there are also some turtles available:
https://docs.racket-lang.org/turtles/Traditional_Turtles.html

If you want a book: https://nostarch.com/realmofracket.htm (also available at Amazon)

So you just show them the primitives and let them play?

If they like to.
But you can set them challenges: • draw a square • draw a triangle • draw a house • draw your initials etc

Do you happen to know Logo?

no

The turtle idea come from Seymour Papert and was part of the Logo programming language. The idea is that you control a turtle on beach. You give it commands like forward, turn 90, etc and by walking in the sand it draws a picture.

It was a popular idea 20–30 years ago in education and you can find old books nice images and ideas to try out.

It’s what inspired the Frozen exercises at Hour of Code: https://studio.code.org/s/frozen


thanks, she will love it!

Realm of Racket uses full Racket. The HtDP teaching languages are better for beginners.

It won’t hurt to take a look at Turtle Art too (http://turtleart.org/). This is a nice software designed by Artemis Papert, daughter of Seymour Papert.

That’s a cool gallery!

HtDP is not quite about Racket. It’s more about how to think, how to design software. Realm of Racket seems to be the book for learning the Racket Way, but I haven’t studied it completely and I confess the whole gaming business is a bit boring for me. I wasn’t a beginner when I read HtDP but it did change my life for sure. I think that book is quite profound. Maybe it’s not a big deal for most here, but it’s surely a big deal for most who don’t know any functional programming and have been sort of idiosyncratically studying programming on their own.

Some fun turtle exercises: https://legacy.cs.indiana.edu/classes/c211/lab10.html\|https://legacy.cs.indiana.edu/classes/c211/lab10.html


@plragde @soegaard2 I remember liking https://world.cs.brown.edu/1/\|HTDW. Would that be appropriate for an 11yo ? ( I’m assuming @pihentagy would need to use the 1st Ed. Teachpack packages https://docs.racket-lang.org/teachpack/world.html\|https://docs.racket-lang.org/teachpack/world.html )

“The chicken wants to cross the road, for reasons that remain a universal mystery.” :smile: (Page 17.)

I had forgotten that note.


There are code in the books: https://www.blurb.com/books/651323-turtle-art (click preview)

Also one can click the yellow “block” at the lower, right: http://turtleart.org/programming/book1/imagepage.html?0

As an “IDE” maker I appreciate someone who cares about correct srcloc as much as Sam. :slightly_smiling_face:

For one thing, we cheat by putting all the closing brackets and parens on line line, so collapse lines 8–11. So now it’s just 8 lines.

I’m kidding but also that is the lisp formatting convention. :slightly_smiling_face:

Seriously: I would use the match with a regular expression pattern, as in @sorawee’s example.

Also for input I would definitely use match
not match-let
because it definitely could fail.

(In real-world code.)

Isn’t most of this now in HtDP/2e?

It still refers to ‘DrScheme’ so I don’t think it has had an update in 10+ years

@anything HtDP changed my life also, and I was a tenured full professor of computer science when I first read it. (Still am. Tenure was useful for some of the things I did with Racket.) I don’t like emphasis on gaming, either; with my own kids, I used images and animation, but also just boring old computation on numbers and symbols and Booleans.

I quite fancy updating it and rolling into https://docs.racket-lang.org\|https://docs.racket-lang.org

Oops I misread - and this is where I admit I haven’t read 2e

Found some interesting perf information for various forms of dynamic dispatch in CS Racket.

• Suppose normal dynamic function invocation (no inline) has a cost of 1x. • (send …) has a cost of about 14x. • (send-generic …) has a cost of about 12.8. • (with-method …) is much faster at a speed of 2x. • struct generics run at about 8.7x.

#lang racket
(module mod1 racket
(provide test%)
(define test%
(class object%
(super-new)
(define/public (method x)
(add1 x)))))
(module mod3 racket
(require racket/generic)
(provide gen:generic-test gen-method)
(define-generics generic-test
(gen-method generic-test x)))
(module mod4 racket
(require (submod ".." mod3))
(provide (struct-out generic-test-impl))
(struct generic-test-impl ()
#:methods gen:generic-test
[(define (gen-method self x)
(add1 x))]))
(module mod2 racket
(require (submod ".." mod1))
(require (submod ".." mod3))
(require (submod ".." mod4))
(provide (all-defined-out))
(define (add1-method)
(define obj (new test%))
(lambda (x) (send obj method x)))
(define (add1-wrapped x)
(add1 x))
(define (add1-bind)
(define obj (new test%))
(with-method ([method-gen (obj method)])
(lambda (x) (method-gen x))))
(define (add1-send-generic)
(define method-gen (generic test% method))
(define obj (new test%))
(lambda (x) (send-generic obj method-gen x)))
(define (add1-struct-generic)
(define obj (generic-test-impl))
(lambda (x) (gen-method obj x))))
(module mod5 racket
(provide work)
(define (work func)
(let loop ([x 0])
(if (= x 100000000)
x
(loop (func x))))))
(require 'mod1 'mod2 'mod3 'mod4 'mod5)
(time (work add1-wrapped))
(time (work (add1-method)))
(time (work (add1-send-generic)))
(time (work (add1-bind)))
(time (work (add1-struct-generic)))

It’s interesting to me that with-method is so much faster than everything except for passing functions directly. It makes me wonder if Racket should consider adding in a more imperative iterator interface like Java using classes so you can use with-method for more performance? Might also make sense to have an equivalent to with-method for struct generics if one does not already exist.

@louis has joined the channel

If you’re using Racket’s cffi, you can use make-ctype
to make a nicer conversion function.

I think you can use ptr-ref
with the ctype
(in your case _string/utf-8
) to get what you need.

The Vulkan strings which have a max-length are a little more annoying.

This is what I’m using for them

(define (_bytes/nul-terminated-constant len)
(define _ctype (_array _sbyte len))
(make-ctype
_ctype
(λ (bstr)
(define a (malloc _ctype))
(define str-len (bytes-length bstr))
(memcpy a 0 bstr 0 str-len)
;; TODO: Error when string is too large
(when (< str-len len)
(memset a str-len 0 1))
(ptr-ref a _ctype))
(λ (a)
; Since the byte string is null-terminated we need to do an O(n) lookup for the null byte
(define str-len
(let/ec ret
(for/fold
([i 0])
([b (in-array a)])
(if (eq? b 0)
(ret i)
(+ i 1)))))
(define bstr (make-bytes str-len))
(memcpy bstr 0 (array-ptr a) 0 str-len)
bstr)))

This year was my first one doing Advent of Code. My solutions are here: https://github.com/lojic/LearningRacket/tree/master/advent-of-code-2020
And that LearningRacket directory has a ton of Racket examples. My AoC code wasn’t always very idiomatic though, so take it lightly :)

Very cool! Thanks for sharing that. For kids, I do think that gaming, images and animations are kinda nice. If I were a kid, I probably would have spent a great deal of time learning to do things graphically. I think that’s a good skill. Now that we are mostly interested in abstract stuff, we could use those graphical skills to popularize what we do. (But such is life.)

I give you images and animations but (1) my kids were turned off by some forms of competition (2) they did more reading than anything, so were good at abstraction and “inner motivation”. We need to think more about alternative pathways.

I remember I enjoyed some competition, but I can’t see the point of such thing today, [so it does seem boring to me too]. I like the “alternative pathways”.