by ubavic on 4/29/23, 6:18 PM with 70 comments
by barrister on 4/30/23, 12:03 AM
-- 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)]
by sidkshatriya on 4/29/23, 7:35 PM
Egison can be used to build a symbolic math backend and do all kinds of pattern matching. But its really niche requirement and it's never really occurred to me in all this time "hey, this might be a good time to use Egison"
I wonder when it might be a good idea to use Egison and if there are some current users in production.
by dang on 4/29/23, 9:11 PM
The Egison Programming Language - https://news.ycombinator.com/item?id=17524239 - July 2018 (67 comments)
Egison – Pattern-matching-oriented Programming Language - https://news.ycombinator.com/item?id=7992661 - July 2014 (33 comments)
Egison: A Lisp Written in Haskell with Advanced Pattern Matching - https://news.ycombinator.com/item?id=7924168 - June 2014 (27 comments)
by wizzwizz4 on 4/29/23, 6:58 PM
> Interestingly, this basic definition of map has been almost unchanged for 60 years since McCarthy first presented the definition of maplist in [62].
but it jumps from what looks like standard Haskell, to the new syntax, without explanation.
by 2h on 4/29/23, 7:26 PM
def twinPrimes :=
matchAll primes as list integer with
| _ ++ $p :: #(p + 2) :: _ -> (p, p + 2)
why do people do this? this is unreadable to me. what is the second line, a
comment? then they somehow found a way to cram 11 symbols in a single line after
that. bravo?by crabbone on 4/30/23, 6:41 AM
by pasquinelli on 4/29/23, 10:47 PM
that doesn't mean anything, it should be "Egison makes programming dramatically simpler!"
by sevensevennine on 4/30/23, 1:42 PM
Take a look at this example from the text, which contains an obvious domain modeling error while demonstrating cool things:
def suit := algebraicDataMatcher
| spade
| heart
| club
| diamond
def card := algebraicDataMatcher
| card suit (mod 13)
def poker cs :=
match cs as multiset card with
| [card $s $n, card #s #(n-1), card #s #(n-2), card #s #(n-3), card #s #(n-4)]
-> "Straight flush"
This matches a new kind of poker hand called the "wrap around straight flush," where a straight can wrap around Q, K, A, 2, 3. assertEqual "poker hand 1"
(poker [Card Spade 3, Card Spade 2, Card Spade 1, Card Spade 0, Card Spade 12])
"Straight flush"
TRUE
IOW, in their eagerness to demonstrate a really cool match on a mod 13 expression (something I haven't seen before), the author models the domain incorrectly.It's also somewhat confusing that the cards are modeled as n-1 - ace=0, 2=1, 3=2, etc, which is also confusing. I tried for about 10 minutes to fix it, but the only notation documentation I found is math that is way over my head.
by 1letterunixname on 4/30/23, 12:56 AM
by omneity on 4/30/23, 3:33 AM
by BaculumMeumEst on 4/29/23, 11:35 PM