Re:Why Crystal is the most promising programming language of...
by kazinator on Friday June 15, 2018 @12:49PM
(#17321276)
How do we know that
def fac(n): n < 2 ? n : n * fac(n - 1)
is the whole function? Maybe someone snipped def fac(n): n < 2 ? n : n * fac(n - 1)
+ more stuff
One of the parentheses you have there come from having an unambiguous, complete, top-level form. We know nothing has been cut off; any characters after the last closing parenthesis are not part of this form. Curly-brace languages agree with this and do the same.One level of parentheses comes from using a dialect which uses a variable definition and lambda to define a function, instead of having function-defining syntax like defun.
n < 2 ? n : n * fac(n - 1)
requires knowledge of the ternary operator and its precedence. Someone who has no clue about the ternary operator will not make heads or tails out of this. The Lisp is impossible to mis-parse, even by a programmer who doesn't know Lisp. You might not know how if works, but you can't miss the fact that (if ...) is a unit, which is enclosing four things: if, (= n 0), 1, and (* n ...).You've loaded the readability dice by writing it on one line. To help with the readability issues in both languages, we can use multiple lines and indentation:
def fac(n):
n < 2
? n
: n * fac(n - 1)
(defun fac (n)
(if (< n 2)
n
(* n (fac (- n 1)))))
Re:Why Crystal is the most promising programming language of...
by singularity2001 on Friday June 15, 2018 @10:23AM
(#17319961)
yes, lisp is a fossil of a time before precedence was invented.
what I dream of is a Lisp with an additional mechanism to define precedence of functions/operators. And infix operators. Almost like Haskell.
Re:Why Crystal is the most promising programming language of...
by kazinator on Friday June 15, 2018 @12:26PM
(#17321034)
> mechanism to define precedence
Read syntax:
https://en.wikipedia.org/wiki/CGOL [1973!]
Macros:
https://stackoverflow.com/questions/25434538/define-function...
Sample factorial function:
(define fac (lambda (n) (if (= n 0) 1 (* n (fac (- n 1)))))) ; <<--- look at those parens!
And with Clojure, you don't have proper tail recursion, so you'll have to add some Clojure-only thing in there to prevent the above code from blowing up for large numbers.
There's really no getting around it -- Lisp, for many people, is just hard to parse. Consider:
def fac(n): n < 2 ? n : n * fac(n - 1)
Yay! No noise. But I agree, there are some cool things about Clojure.