pihentagy
2021-1-1 11:57:33

@pihentagy has joined the channel


pihentagy
2021-1-1 12:01:08

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


pihentagy
2021-1-1 12:01:35

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


pihentagy
2021-1-1 12:02:05

python equivalent:




sorawee
2021-1-1 12:07:34

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


pihentagy
2021-1-1 12:08:08

thanks, that’s what I was looking for


pihentagy
2021-1-1 12:08:50

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


pihentagy
2021-1-1 12:10:41

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


kellysmith12.21
2021-1-1 12:11:39

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


sorawee
2021-1-1 12:12:11

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


kellysmith12.21
2021-1-1 12:13:30

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.


pihentagy
2021-1-1 12:14:02

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


kellysmith12.21
2021-1-1 12:14:47

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


kellysmith12.21
2021-1-1 12:17:09

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


soegaard2
2021-1-1 12:32:48

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


soegaard2
2021-1-1 12:36:29

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


soegaard2
2021-1-1 12:37:55

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



pihentagy
2021-1-1 14:11:42

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:


spdegabrielle
2021-1-1 14:14:50

Try reset settings to defaults in drracket preferences.


pihentagy
2021-1-1 14:15:55

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


soegaard2
2021-1-1 14:16:28

@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).


soegaard2
2021-1-1 14:17:24

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


soegaard2
2021-1-1 14:17:49

@pihentagy ^


pihentagy
2021-1-1 14:18:27

I have them turned on, haven’t I?


soegaard2
2021-1-1 14:19:26

yes - which language are you using?


pihentagy
2021-1-1 14:20:51

#lang racket


soegaard2
2021-1-1 14:22:39

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


soegaard2
2021-1-1 14:23:00

And then begin to enter something.


pihentagy
2021-1-1 14:23:59

Hm, still not there :thinking_face:


soegaard2
2021-1-1 14:24:29

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


pihentagy
2021-1-1 14:25:01

ok, typed that, pressed Ctrl+R


soegaard2
2021-1-1 14:25:44

Try clicking on define.


soegaard2
2021-1-1 14:25:52

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


pihentagy
2021-1-1 14:26:19

pihentagy
2021-1-1 14:30:23

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


pihentagy
2021-1-1 14:32:57

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?


sorawee
2021-1-1 14:33:15

HtDP?


sorawee
2021-1-1 14:33:27

sorawee
2021-1-1 14:33:44

It has rockets :slightly_smiling_face:


soegaard2
2021-1-1 14:33:50

How old?


pihentagy
2021-1-1 14:33:58

11 yrs


soegaard2
2021-1-1 14:36:11

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


soegaard2
2021-1-1 14:37:11

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


pihentagy
2021-1-1 14:40:30

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


soegaard2
2021-1-1 14:42:03

If they like to.

But you can set them challenges: • draw a square • draw a triangle • draw a house • draw your initials etc


soegaard2
2021-1-1 14:42:35

Do you happen to know Logo?


pihentagy
2021-1-1 14:42:51

no


soegaard2
2021-1-1 14:45:11

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.


soegaard2
2021-1-1 14:46:09

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


soegaard2
2021-1-1 14:46:56

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



pihentagy
2021-1-1 14:55:46

thanks, she will love it!


plragde
2021-1-1 14:59:22

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


anything
2021-1-1 15:04:45

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.


soegaard2
2021-1-1 15:05:55

That’s a cool gallery!


anything
2021-1-1 15:27:41

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.




spdegabrielle
2021-1-1 15:37:53

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


anything
2021-1-1 15:43:10

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


soegaard2
2021-1-1 16:08:05

I had forgotten that note.


pihentagy
2021-1-1 16:21:58

are there sources for http://turtleart.org\|turtleart.org images?


soegaard2
2021-1-1 16:25:15

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


soegaard2
2021-1-1 16:25:56

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


greg
2021-1-1 16:34:12

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


greg
2021-1-1 16:39:50

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.


greg
2021-1-1 16:40:01

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


greg
2021-1-1 16:40:53

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


greg
2021-1-1 16:41:09

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


greg
2021-1-1 16:41:22

(In real-world code.)


plragde
2021-1-1 18:06:36

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


spdegabrielle
2021-1-1 18:08:42

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


plragde
2021-1-1 18:09:04

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


spdegabrielle
2021-1-1 18:09:52

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


spdegabrielle
2021-1-1 18:16:17

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


dyllongagnier
2021-1-1 20:50:47

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


dyllongagnier
2021-1-1 20:51:48

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


dyllongagnier
2021-1-1 20:54:50

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


dyllongagnier
2021-1-1 20:58:47

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
2021-1-1 21:09:15

@louis has joined the channel


yilin.wei10
2021-1-1 22:06:41

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


yilin.wei10
2021-1-1 22:10:02

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


yilin.wei10
2021-1-1 22:10:22

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


yilin.wei10
2021-1-1 22:10:43

This is what I’m using for them


yilin.wei10
2021-1-1 22:11:05

(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 (&lt; 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)))


badkins
2021-1-2 00:05:21

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


anything
2021-1-2 00:44:22

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


plragde
2021-1-2 00:58:59

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.


anything
2021-1-2 01:01:52

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