
@abbyrjones72 that’s great, url?

URL?

@pierre has joined the channel

:wave::skin-tone–3: racketeers

How do I make a macro that transforms: (make-thing foo self)
to: (define foo (thing #f))
(set-thing-x! foo foo)
Assuming thing
is (struct thing (x) #:transparent #:mutable)

is it OK to just do syntax->list, replace, and transform back? I’m trying to use syntax-parameterize and failing.

#lang racket
(require (for-syntax syntax/parse))
(struct thing (x) #:transparent #:mutable)
(define-syntax (create-thing stx)
(syntax-parse stx
[(_create-thing id)
(syntax/loc stx
(begin
(define id (thing #f))
(set-thing-x! id id)))]))
(create-thing foo)
foo

@hoshom ^

No, it’s only supposed to replace uses of “self” with itself

So (make-thing foo self)
is not the macro call?

it is

but for example if “thing” had 2 fields

i’d do (make-thing 1 self)

the first field in the struct would be 1, but the second, that says self
, I want to set! that to itself

I think you want shared
: https://docs.racket-lang.org/reference/shared.html?q=shared

I suppose I should make a function and not a macro though so I’ll just make do

that’s interesting, thanks

well well well!

@hoshom
#lang racket
(require (for-syntax racket/function
syntax/parse))
(struct thing (x y) #:transparent #:mutable)
(define self (gensym 'self))
(define-syntax (replace stx)
(syntax-parse stx
[(_ field id)
(cond
[(and (identifier? #'field) (free-identifier=? #'self #'field)) #'id]
[else #'field])]))
(define-syntax (make-thing stx)
(syntax-parse stx
[(_ id:id field ...)
#'(define id (shared ([id (thing (replace field id) ...)]) id))]))
(make-thing foo 10 self)
(make-thing bar self 20)
(make-thing baz self self)
(make-thing blah 10 20)
foo
bar
baz
blah
;; #0=(thing 10 #0#)
;; #0=(thing #0# 20)
;; #0=(thing #0# #0#)
;; (thing 10 20)

@sorawee That’s neat, that does the trick. Thanks!

Actually, this allows self
to be used outside of the macro

This is a version that prohibits it

#lang racket
(require racket/stxparam)
(struct thing (x y) #:transparent #:mutable)
(define-syntax-parameter self
(λ (stx) (raise-syntax-error (syntax-e stx) "can only be used inside make-thing")))
(define-syntax-rule (make-thing id field ...)
(define id
(shared ([x (thing (syntax-parameterize ([self (make-rename-transformer #'x)])
field) ...)]) x)))
(make-thing foo 10 self)
(make-thing bar self 20)
(make-thing baz self self)
(make-thing blah 10 20)
foo
bar
baz
blah



it’s very remedial, but I learn slow

@abbyrjones72 Well, I’m glad you found Lisp on your path. Not every programmer stops by and try to learn such languages because they look so “weird” and “different” and “old”. SICP was a fundamental read for me too. With Racket I’m home, so, welcome there!

Ty so much :slightly_smiling_face:

I guess the good thing about being unemployed (at least from programming) is that I don’t have to divide my loyalties between what I want to learn and what I have to learn. I seriously doubt I will ever work in LISP professionally, but in this stage of my life, as I formulate research in healthcare, adding to the body of knowledge is an important, fulfilling venture in itself. I hope that I can contribute something to at some point.

@abbyrjones72 Very nice blog so far! I look forward to more of it.

I posted a new one today. If you get bored and want to check my work, please don’t hesitate to correct me.

and ty :slightly_smiling_face:

I have been programming since I was 10, and although I don’t do it for my job anymore (I’ve turned into an architect and manager, sigh), I still enjoy and do it for fun at home. One thing you might try for programming exercises: https://adventofcode.com - it’s no longer “live” but the exercises (also from previous years) are available. I participated for the first time this year (didn’t finish), and I made it a point of using Racket for the exercises, it’s a great way to practice.

i’ve been working through the http://exercism.io\|exercism.io racket track, I’ve found it’s a good way to jump into the ecosystem myself.

Also makes it nice there is now a mentor system, there are a few people that have been really nice to point some stuff out

I will definitely try both sites.

(also very new to Racket, as in just started a week ago)

I am new to Racket, LISP, and computer science in general. Little to no math background, but it is my hope to answer every question in the SICP to improve myself.

last time I’ve seen it the exercism track for Racket needed some work. I submitted a couple of PRs improving some of the exercises but they did take a long time to be merged

Hmm, I’m only a few exercises in, we’ll see once I get further along. It does gate you a good amount now if you ask for feedback

@benjamin.clos if you have any trouble you can ask about it here

Fellow Apple IIe learner here. Really glad to see posts from other folks who took roundabout or non-traditional paths into computing. Keep writing!

Much appreciated. :slightly_smiling_face: will probably be taking that offer up once I get to some of harder data manipulation ones.

I posted blog 3 just a few minutes. As always, enjoy and rip it apart. I’m here to learn :slightly_smiling_face:


Is there a scribble package akin to mathpartir, for typesetting inference rules and (fairly simple) derivation trees? For context, I’m typesetting lecture notes that will run with MathJax, so I can use any basic LaTeX stuff but not any packages…

@abbyrjones72 nice blog posts! Just in case - there is sicp package available that gives more support/compatibility with SICP: https://docs.racket-lang.org/sicp-manual/

Oh my goodness, this is fantastic. Thank you @githree. I am still only halfway through Chapter 1, but this is so good to know and I will add this to my blogs.

That’s the power of Racket :slightly_smiling_face:

@abbyrjones72 Would you like to see my solution to the sum-of-squares problem? It might be interesting because it’s very different, and it applies a couple of those often-reusable idioms that make programming require less thought.

Sure!

I will also be happy to include that alternate solution in my blog. I’m all about more efficient solutions.

This might look like a mess, and indeed @soegaard2’s solution is much more elegant and efficient. But it popped into my head instantly, just applying habitual ways of mentally framing it that have been helpful many times before. I would not have naturally hit on this approach if it weren’t for other people having shown me these tricks long ago.

I think I need to add both of your solutions to the blog so I can learn from then. TY!

I always love to see how other people solve things, i almost always learn something

If you’re only just getting accustomed to passing arguments, this solution is probably way too much. When I first saw this sort of thing, I thought it was too tricky. I think it’s pretty typical of pure-functional programming (I could be wrong: I can’t speak with authority about that), and it’s gets easier to read with time. But right now, it might be best to save someplace and look at again later.

It is way out of my league, but at the moment. If it’s too hard for me now, then that is good.

heh, that was the problem I worked on last with exercism

though, the scope was different

@abbyrjones72 OK, it’s probably like looking a few chapters ahead in a book teaching a foreign language. I thought of it because of your observation that the problem was so hard, it seemed discouraging. After a while, problems like that will “light up” familiar, friendly ways of looking at them, which make them easy.

(They also create new blind spots: I totally did not see @soegaard2’s solution. That one exploits algebra—the problem domain rather than programming tricks!)

I like hard problems because it means I am learning something.

@benjamin.clos Thanks for telling me about http://exercism.io\|exercism.io! I just signed up for it; now waiting for the email…

just downloaded Exercism lesson one.

look who’s there

I took the time to register here, at last

:clap:

@bkovitz That strikes me as a very SQL-ish way of solving the problem. I hope you don’t find that insulting.

is it possible to access a struct field value from its name?

@pierre no, not really

After some poking around (for a course on logic and computation), I just went with \frac, slightly tweaked. That’s what mathpartir uses at its heart, anyway.

true enough, but I was hoping someone had define-syntax
’ed some nicer wrappers around that, so that it felt nicer to write in Racket :slightly_smiling_face:

@bill_temps Not at all. I’d love to see another solution.

@bill_temps Say, would it be fair to say that a lot of pure-functional programming is SQL-ish? Especially the “map–filter” style?

Cool, thanks! When I tried this using syntax parameters, shared
is what I was missing. I guess I still have a long way to go to get a feel of how bindings really work.

@ruyvalle has joined the channel