4 ms·
I believe, on the pattern matching side, it has to do with the fact that it's a higher level feature that feels much more declarative. Inside of writing a seque
by opnitro 6y ago
I believe, on the pattern matching side, it has to do with the fact that it's a higher level feature that feels much more declarative. Inside of writing a sequence of instructions to check whether a list is 3 and the first two are equal, one writes the declarative syntax:
(define (f x)
(match x
[`(,x ,x ,y) y]
[_ #f]))
- variaga 6y agoI'm sure it makes sense to someone well-versed in Rust syntax, but to an outsider like me that code looks like the kind of "line noise" that made me switch from perl to python. The procedural 'if x.len == 3 and x[0] == x[1]' seems a lot clearer as to what is actually being tested.
- opnitro 6y agoA) this is in Racket, as that's the editor window I had up. B) your point is fair, one benefit of this is that it also binds names to things.
- lachlan-sneff 6y agoThat code in the comment above isn't actually rust. It's a lisp, I believe. In Rust, you'd write something more like: fn f(x: &[i32]) -> bool { match x { [x, y, _] if x == y => true, _ => false, } }
- steveklabnik 6y ago... and you could write fn f(x: &[i32]) -> bool { x.len() == 3 && x[0] == x[1] } if you wanted. Honestly, I probably would.