5 ms·
The above is a super concise syntax example that showcases multiple things at the same time. It is function definition mixed with pattern matching, with some ad
by vvillena 2y ago
The above is a super concise syntax example that showcases multiple things at the same time. It is function definition mixed with pattern matching, with some advanced pattern matching features, like the '...' and '||' symbols. You can rewrite the example into the code below, if you find it to be more readable.
fun is_sorted(list):
match list:
| []: #true
| [_]: #true
| [head, next, tail, ...]:
head .<= next && is_sorted([next, tail, ...])
There's also a dot in `head .<= next`, because the syntax is sugar for `head.<=(next)`. Not sure why this is needed, but a quick read on the docs suggests this is done to enable static dispatch for some calls.
- scotty79 2y agoIt doesn't look terrible but colon after "match list" is optional. And how would the last line look if the previous one didn't end in a colon?
- psychoslave 2y agoNot sure that can be called sugar when it it has just as many characters, two spaces rather than two brackets, with remaining lexies in same order, and the cognitive load is at least as important as it exposes the dot-notation within an infix three characters operator. The global project might be nice, but in this particular case it's kind of opt for worst of all pre-existing conventions while trying to please everyone. The challenge is tough, so that's no wonder it will have hard time matching the goal.
- patates 2y agoDo you know why the tail is needed? Like, what would be the problem with this: fun is_sorted(list): match list: | []: #true | [_]: #true | [head, next, ...]: head .<= next && is_sorted([next, ...])
- Y_Y 2y agoI wondered this too, let's find out. I am looking all this up as I go, partially as an experiment to find out how easy it is to use Rhombus. $ raco pkg install rhombus --auto which should work if you have some vaguely recent install of racket on your machine and handle the dependencies without prompting. (I started installing this several hours ago on a slow machine but it's still going.)
- soegaard 2y agoIt's supposed to take a couple of minutes. Please file a bug report so this can be tracked down. https://github.com/racket/rhombus https://github.com/racket/rhombus
- Y_Y 2y agoI'm not going to file a bug report, that's more time than I'm willing to spend on a project I'm not involved in. I will say I'm using Termux 0.118 and Racket 8.13 on kernel 5.10 and Android 14 and am willing to answer questions.
- soegaard 2y ago> I'm not going to file a bug report, that's more time than I'm willing to spend on a project I'm not involved in. In that case, I make it quick - and refrain from any followup questions. Did you by chance pick the "Minimal Racket" distribution? If so, your command essentially compiled large parts of Racket and generated the entire set of documentation. The distribution "Full Racket" is the recommended distribution for most purposes. It comes with everything pre-compiled.
- Y_Y 2y agoNo, but yes. https://github.com/termux/termux-packages/blob/master/packages/racket/build.sh https://github.com/termux/termux-packages/blob/master/packag... I just installed the package called `racket` from Termux's upstream, and it seems that they're using racket-minimal for that. Bit of a gotcha, but at least it doesn't seem like there's a bug. Thanks for the tip.
- soegaard 2y agoThanks for the response. I'll contact the Termux people and ask if the choice of `racket-minimal` was intentional.
- Y_Y 2y ago~ $ racket -I rhombus Welcome to Racket v8.13 [bc]. > fun is_sorted(list): match list: | []: #true | [_]: #true | [head, next, ...]: head .<= next && is_sorted([next, ...]) ; readline-input:6:19: next: cannot use repetition binding as an expression ; in: next ; [,bt for context] From there I got to "repetition binding" in the docs: https://plt.cs.northwestern.edu/pkg-build/doc/rhombus/Repetitions.html https://plt.cs.northwestern.edu/pkg-build/doc/rhombus/Repeti... From what I can understand, the "..." isn't independent of the previous expression in the list (as I would have expected from e.g. Prolog or Haskell). Instead you are defining a kind of pattern to reuse later, so tail gives a name to the rest of the list (so that it doesn't get associated with next).
- patates 2y agoAh, now that makes sense, the three dots need to be assigned to a variable. It's just how they do it is completely new to me. Thank you for finding out!