24 ms·
The conditional expression or more specifically everything being an expression is my favorite thing about Lisp. I did not know that McCarthy pushed to add it to
by agentgt 10y ago
The conditional expression or more specifically everything being an expression is my favorite thing about Lisp. I did not know that McCarthy pushed to add it to Algol which apparently today is the ternary operator for most languages.
It is annoying that so many languages (C, Java, C#, etc) have both a conditional statement (if-else) and conditional expression (ternary ?:). Really the if-else should be an expression (I think the ternary operator is hideous).
- adwn 10y agoIn Rust, if-else is an expression, and there's no separate ternary operator.
- agentgt 10y agoYep I know. Rust although syntactically looks like an Algol [1] like language it actually barely is one :) . It is more akin to a ML language [2]. [1]: https://en.wikipedia.org/wiki/Generational_list_of_programming_languages#ALGOL_based https://en.wikipedia.org/wiki/Generational_list_of_programmi... [2]: https://en.wikipedia.org/wiki/Generational_list_of_programming_languages#ML_based https://en.wikipedia.org/wiki/Generational_list_of_programmi...
- clarkd99 10y agoMy current project is over 90,000 lines of C and I have programmed in C since the 1970's (as well as many other languages). I have never used a ternary operator and I think it reads quite poorly. I just use a few lines of easily read code instead. I also never use a do/while construction with the condition at the bottom of the loop. When I need such a construct I just put in an "if" and "break" where ever needed instead. I don't think C should be dissed because of a few unneeded and probably unused features. In C, you can put more than 1 statement on a line if they are separated by a ';' but I have never done that in hundred's of thousands of lines of C code.
- flogic 10y agoI don't program in C but I use the ternary operator quite regularly. It's quite handy when you're populating large data structures. For limited use in that case I don't find the readability that diminished maybe even enhanced.
- mroll 10y agoIf you like everything being an expression, check out tcl. A lot of ideas from lisp show up in tcl, especially the idea of everything as an expression. Tcl embodies this idea while also having the look of an algol-like language. Funny it can pull this off while having basically no syntax.
- qwertyuiop924 10y agoTCL, while it has its warts, is a really cool language. I mean, it even basically has fexprs, something most Lisps put by the wayside years ago. When you don't actually care about speed, you can do some pretty cool stuff.
- mroll 10y agoYeah the level of dynamicness (dynamism?) you can get in tcl is unparalleled as far as I can see. Having no types or syntax and access to the entire runtime at any point in the program opens up all kinds of crazy doors. But you're right, that slows it down. But you might be interested to know there is currently an effort to get Tcl to compile to native/near-native code. Here is a paper on the new techniques being developed and a link to a talk given by one of the lead tcl core team members. http://www.tcl-lang.org/community/tcl2015/assets/talk14/TheTclQuadcodeCompiler.pdf http://www.tcl-lang.org/community/tcl2015/assets/talk14/TheT... https://www.youtube.com/watch?v=RcrqmZV88PY&t=38s https://www.youtube.com/watch?v=RcrqmZV88PY&t=38s
- qwertyuiop924 10y agoPicolisp can match it in dynamism. Seriously, Picolisp is absolutely insane.
- mroll 10y agoI just read a bit of documentation from the Picolisp page. It looks really cool, but can it reach arbitrarily far up the call stack? That's the quality of tcl that I don't see other places. The capabilities of 'upvar' and 'uplevel'. [Edit] I should say "one of the qualities." The other important one is that Tcl has no types. Even all the lisps I know have types.
- lemming 10y agoIs there any literature on how to translate languages where everything is an expression to ones where it isn't, for example when compiling/transpiling to java? If/then can be translated to the ternary operator, but terms like try/catch are trickier.
- wtetzner 10y agoUsually when I'm faced with putting a try/catch in an expression in Java, I end up wrapping it in it's own method. It's not the nicest solution, but it would allow targeting Java without disrupting the code structure too much.
- corecoder 10y agoAn easy to follow example is Coffeescript (where everything is an expression), which transpiles in javascript (where not everything is).
- arethuza 10y agoIt's been a while since I used Common Lisp but isn't it recommended to use "if" for conditional expressions and "when"/"unless" for conditional statements?
- TeMPOraL 10y agoIt's a matter of code readability. "when" and "unless" are expressions too, but they look the best when they're used instead of "if" for single-branched conditionals when you don't care about return value.
- arethuza 10y agoYes - from what I recall it definitely helped to make things clearer to use "if" and "when" appropriately.
- kazinator 10y agoThe choice exists only if you have a single form to evaluate subject to the condition. If there are two or more, of course this is imperative programming and (when condition form1 form2 ...) recommends itself over (if condition (progn form1 form2 ...)). If there is only form1, either one will do, so then it's down to a decision based on whether form1 returns a useful value and/or performs a side effect.
- sorokod 10y agoif-else is an expression in Kotlin ( https://kotlinlang.org/docs/reference/control-flow.html https://kotlinlang.org/docs/reference/control-flow.html )
- DonHopkins 10y agoExpressions are limited, because they can only return one result. In stack based languages like Forth and PostScript, any function can take and return any number of parameters. In fact they can decide at runtime how many to take and return. PostScript is a lot like Lisp in that it's purely and simply homoiconic: PostScript code is just normal PostScript data. The "ifelse" operator takes a boolean and two expressions (executable PostScript polymorphic arrays, or any other PostScript object, executable or not -- non-executable objects are just pushed onto the stack), and executes one or the other depending on the value of the boolean parameter. I think Lisp's multiple-value-bind is an inelegant hack, compared to the simplicity of PostScript.
- qwertyuiop924 10y agoPS seems like a nice language. Such a shame it's stuck on the printer: it seems like it would be a good alternative to FORTH in less memory-constrained environments.
- DonHopkins 10y agoNeWS is a Network extensible Window System developed by James Gosling at Sun in the 80's, which used PostScript not just to draw on the screen (like NeXT), but also (unlike NeXT but like AJAX) to implement entire window managers, user interface toolkits, applications, and intelligent responsive front-ends to networked applications, all in the NeWS window server (like a web browser). NeWS was architecturally similar to what is now called AJAX, except that NeWS coherently: + used PostScript code instead of JavaScript for programming. + used PostScript graphics instead of DHTML and CSS for rendering. + used PostScript data instead of XML and JSON for data representation. http://www.donhopkins.com/drupal/node/97 http://www.donhopkins.com/drupal/node/97 See the PSIBER and PizzaTool code I posted in the message above for an example of what was possible with NeWS!
- qwertyuiop924 10y agoYes, NeWS was cool. But NeWS no longer exists, sadly.
- Retra 10y agoProbably a bit off-topic: I always get frustrated with C++ because `if` is not an expression, you can't do conditional initialization under RAII easily.