4 ms·
Here is my brief explanation of the example given on the website in order to clear up some confusion, that I gleaned from the thesis paper. Admittedly, this lan
by barrister 3y ago
Here is my brief explanation of the example given on the website in order to clear up some confusion, that I gleaned from the thesis paper. Admittedly, this language does require knowledge of Haskell to really comprehend:
-- Extract all twin primes from the infinite list of prime numbers with pattern matching!
def twinPrimes :=
matchAll primes as list integer with
| _ ++ $p :: #(p + 2) :: _ -> (p, p + 2)
"matchAll is composed of an expression called target, matcher, and match clause,
which consists of a pattern and body expression."
In the example `primes` is a list of primes as in Haskell: `[2,3,5,7...]` considered the "target", and the "matcher" `list integer` may be thought of as a Haskell type like `[Int]`. So I suppose you could simply write it as `primes :: [Int]` and mean the same thing. This notion of a "Haskell list" is important because in the "match clause" the "pattern" is a combination of concatenation using the operator `++` and cons from Lisp using the operator `::`---note that this deviates from Haskell syntax in a somewhat confusing way where Haskell uses `:` and `::` for typing. Nonetheless, the integer list is deconstructed according to concatenation first, in every way, i.e. `[] ++ [2,3,..]`, `[2]++[3,5..]`, etc.., then according to cons'ing with the head stored in the variable `$p`. Yet, the "rest" of the list in this case is actually matched according to the pattern `x::y::_`, therefore, the second element must be 2 from the first, which is why the first pattern `[]++(2::3::_)` is discarded. The `#p` notation simply means to reuse the previous value of p to create a literal match, therefore for the first pattern `p is 2` and `#(p + 2) is 4` thus the pattern becomes 2 followed by 4 followed by the rest, which again doesn't exist. Finally if a match does exist, the values are constructed according to the "body expression", in this case a pair, and all of the results kept in a list. Therefore the type of this value is
twinPrimes :: [(Int, Int)]
- wizzwizz4 3y agoI really hope that | _ ++ ($p :: #(p + 2) :: _) -> (p, p + 2) is valid syntax. That would've cleared the whole thing up for me.