6 ms·
Express Intuition Directly with Essentially New Syntax Egison makes programming dramatically simple! (define $twin-primes (match-all primes (list in
by nerdponx 8y ago
Express Intuition Directly with Essentially New Syntax
Egison makes programming dramatically simple!
(define $twin-primes
(match-all primes (list integer)
[<join _ <cons $p <cons ,(+ p 2) _>>>
[p (+ p 2)]]))
Uh, ok?
- nine_k 8y agoIt helps if you remember that $primes is a list of primes, and <...> is pattern-matching. So what it does is matching of such elements (named p) that the next element (see cons) equals p+2, which would be a twin prime, since p is a prime. Then it generates an element consisting of a pair [p, p+2]. The pattern apparently matches such a list comprised of elements (see <join ... >) that are [p, p+2] (the inner <cons ...>). You definitely remember that Lisp lists are singly-linked lists, and (cons head tail) prepends a new head element to the list tail. This is why it is matched, pertty similarly to how it's done in Haskell.
- sp332 8y agoOh, I thought it was doing some kind of prime search. But you have to give it a list of primes to start with? How about one of: print [(x,x+2) for x in primes if (x+2 in primes)] print [(x,y) for x, y in zip(primes, primes[1:]) if x+2 == y] The simpler one doesn't even require the primes list to be sorted, so I thought the second one would be more equivalent. It's still a lot easier to read and write than Egison one.
- nine_k 8y agoYour first snippet has quadratic performance. The second is closer to the idea. Pattern-matching would be even more obvious is a piece of Haskell: pair :: [x] -> Maybe (x, x) pair p:q:rest = if q = p + 2 then Just (p, q) else Nothing pair _ = Nothing findTwins primes = [x | x <- map pair primes] I suppose that Eigson's power lies in matching of more complex structures, as they mention non-linear matching, matching with multiple results, matching with lexical scoping.
- tromp 8y agoYour Haskell code doesn't quite parse. But the following works: import Data.List primes = Data.List.nubBy (\a b -> gcd a b > 1) [2..] findTwins primes = [(p,q) | (p:q:_) <- tails primes, q == p+2] main = mapM_ print . take 10 . findTwins $ primes Finding primes arbitrarily far apart can be done in Haskell as findBigTwins gap primes = [(p,q) | (p:qs) <- tails primes, let q = p+gap, q `elem` takeWhile (<= q) qs]
- egi 8y agoThank you for your comment! The power of Egison's pattern matching becomes more obvious when we consider more general pattern such as (p, p+ 100) not only (p, p+2). We can write the first 5 prime pairs whose form is (p, p+100) with a small modification for pattern matching for twin primes. (take 5 (match-all primes (list integer) [<join _ <cons $p <join _ <cons ,(+ p 100) _>>>> [p (+ p 100)]])) ;=>{[3 103] [7 107] [13 113] [31 131] [37 137]}
- nerdponx 8y agoI still don't see how this should require nested cons-es and 3 different types of enclosing braces. Not only that, but you have redundancy between the ,(+ p n) part of the pattern and the emitted result of [p (+ p n)]. Point being: I applaud the attempt here, but it's not something I'll ever want to use.
- sp332 8y agoI guess I had just lost the context. It's pretty cool as a pattern-matching demo.
- allenz 8y ago> first snippet has quadratic performance Not if primes is a set.
- Ultimatt 8y agoOr in my fave language: my @primes = (1..∞).grep: *.is-prime; my @twin_primes = map { ($_, $_+2) }, @primes.grep: (*+2).is-prime;
- sp332 8y agoGonna go out on a limb and guess... Perl 6?
- ORioN63 8y ago`my` gives away perl infinite symbol gives away 6 So, yeah. Correct me if I'm wrong.
- Ultimatt 8y agoYup both right. All the clues were there >:3 I could have used Inf instead of the unicode but thats not really whimsical enough for Hacker News. A slightly less listy pattern style is to declare types. Like I'd prefer this sort of declarative style: subset Prime of Int where *.is-prime; subset TwinPrime of Prime where (* + (2|-2)).is-prime; my @twin_primes = grep TwinPrime, ^Inf; Just feels a lot more clear what my intent is and I get some cheap types for later, to use in function parameters that require twin primes.
- nerdponx 8y agoHuh, this is actually making me want to give Perl 6 another try. How is performance on a task like this compared to, say, Python?
- phkahler 8y agoYep, that's where I stopped. What I think is going on... Some people think predominantly in abstraction and don't really care about syntax. As Paul Graham once said, LISP doesn't really have syntax - you write parse trees directly. That may be fine for people who have achieved that level/style of programming, but it's a tiny minority. Why should I write (+ p 2) when most people think (p+2)? This difference gets worse with more complex expressions. For the masses (and I mean a lot of very capable people) this is just garbage. For some it's wonderful. I can't say it's wrong, just not my thing. Perhaps it's the Blub paradox - I've never had the LISP epiphany so I can't say. I have had the python epiphany, and it's quite the opposite of this.
- nerdponx 8y agoI like Lisps but I still don't like this. This is like the Perl of Lisps. To my eye, this is not a parse tree, it's syntax vomit on top of a parse tree.
- lispm 8y agoIf you have lots of math expressions to write, you can use an infix reader in Common Lisp. It has a programmable reader. Such a thing - reading infix expressions - was already a standard feature of the MIT Lisp Machine OS in the 80s. In Lisp you actually don't write parse trees. Lisp uses a serialized hierarchical data format of a tokenizer. S-expressions are not Lisp syntax trees, but a data format.
- qop 8y agoI relish comments like these from Lisp fans that actually KNOW Lisp. Someday we'll resurrect it all... Reader macros blew my mind when I first read about them. Being able to change the very laws of the universe from within my simple little programs... Insane!
- kbp 8y ago> LISP doesn't really have syntax - you write parse trees directly. Lisp has plenty of syntax, it's just extensible and largely defined in operators. Commonly used macros like DEFUN, WITH-OPEN-FILE, and LOOP all add their own syntax to the language; that's what macros are for. When you put them together, typical Lisp code ends up looking like (defun count-words (filespec) (with-open-file (f filespec) (loop with counts = (make-hash-table :test 'equal) for line = (read-line f nil) while line do (loop for word in (cl-ppcre:split "\\s+" line) do (incf (gethash word counts 0))) finally (return counts)))) Which I don't think is that alien. It translates pretty cleanly into Python: def count_words(filespec): with open(filespec) as f: counts = collections.defaultdict(int) for line in f: for word in line.split(): counts[word] += 1 return counts Do you find the former significantly harder to understand? I don't see how it's any more like "writing a parse tree directly." > Why should I write (+ p 2) when most people think (p+2)? This difference gets worse with more complex expressions. As mentioned in other comments, there are infix packages available for when you're writing math-heavy code (or if you just need infix operators), but I also think that a lot of people don't write very much code with complicated inline arithmetic in it, and overstate how much the awkward math syntax would affect them (there are of course plenty of projects that are math-heavy and where it does make a large difference). When you're defining variables/functions/classes/etc or calling functions or looping or whatever, there's not much difference between Lisp and most other languages aside from whether the bracket is { or ( and which side of the keyword it goes on; in my experience, that sort of code tends to make up the large bulk of most codebases in most languages. edit: Another place where Lisp's default syntax is significantly different from a lot of languages is something like object.f(x).g(y).h(z), which in Lisp looks like (h (g (f object x) y) z). This can be remedied with common macros like -> that let you write (-> object (f x) (g y) (h z)).