3 ms·
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),
by laszlokorte 2y ago
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`
- Jtsummers 2y agoYour 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]
- Y_Y 2y agoIn 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
- soegaard 2y agofun flip_first([Posn(x, y), more, ...]): [Posn(y, x), more, ...]