News for hackers. Stuff that matters.
Re:Rhombus Language by soegaard on Tuesday March 18, 2025 @06:59AM (#43397937)
It's not magic. It's pattern matching.

The `[Posn(x, y), ...]` is a pattern that matches a list of positions. The `[Posn(y, x), ...]` is a template that produces a list of positions.

Re:Rhombus Language by arunix on Tuesday March 18, 2025 @09:46AM (#43399356)
That syntax suggests the first pair will be flipped, and the rest will just be as they were, i.e. more like flip_first
Re:Rhombus Language by soegaard on Tuesday March 18, 2025 @09:58AM (#43399475)
When the pattern `[Posn(x, y), ...]` matches a list of positions, since `Posn(x, y)` is followed by `...` the `x` is bound to a sequence of first coordinates and `y` is bound to a sequence of second coordinates.

In the template the `Posn(y, x)` is followed by `...` so the same number of positions is produced as the common length of x and y.

Re:Rhombus Language by Y_Y on Tuesday March 18, 2025 @11:06AM (#43400340)
Typical pattern matching like in Haskell or Prolog wouldn't do this.
Re:Rhombus Language by shawn_w on Tuesday March 18, 2025 @12:51PM (#43401638)
It makes perfect sense if you're used to the pattern matching of Scheme and Racket syntax macros.
Re:Rhombus Language by Y_Y on Thursday March 20, 2025 @05:52AM (#43421273)
I guess so, though that might be counterproductive to the goals of Rhombus.
Re:Rhombus Language by laszlokorte on Tuesday March 18, 2025 @02:57PM (#43403387)
What would be the corresponding syntax for matching and flipping only the first Posn?

If I had to guess:

  fun flip_all([Posn(x, y), rest]):
    [Posn(y, x), rest]
If I am correct then the only difference between flipping all and flipping just the first is `...` vs `rest`
Re:Rhombus Language by Jtsummers on Tuesday March 18, 2025 @03:35PM (#43403958)
Your version would expect a list of two elements, you use & to indicate that you're collecting the remainder of the list as in:

  fun flip_all([Posn(x, y), & rest]):
You'd need to amend the logic of the function body to iterate or recurse over the remainder. You'd also ended up with a nested list using your version but you can use & again to splice in the result, so if you wanted a recursive version of that it would be something like (skipping the base case):

  fun
    | flip_all([]): []
    | flip_all([Posn(x, y), & rest]):
        [Posn(y, x), & flip_all(rest)]
(not tested, don't have Rhombus available on this system but that should be correct)

If you really want flip_first it'd be:

  fun flip_first([Posn(x, y), & rest]):
    [Posn(y, x), & rest]
Re:Rhombus Language by Y_Y on Tuesday March 18, 2025 @06:10PM (#43405784)
In Haskell:

  flip_one Posn(a,b) = Posn(b,a)
  flip_first p:ps = flip one : ps
  flip_all ps = map flip ps
To my taste that's a lot nicer
Re:Rhombus Language by soegaard on Tuesday March 18, 2025 @07:01PM (#43406231)

    fun flip_first([Posn(x, y), more, ...]):
      [Posn(y, x), more, ...]