5 ms·
Ask HN: Discussions/examples of logical &&/|| vs. if..then/case?
- al2o3cr 3y agoWhat do you mean by "versus" here? The short-circuit operators behave like this pseudocode: expr1 || expr2 if expr1 == false expr2 else true In languages with macros, they may even be _implemented_ like that.
- sargstuff 3y agoMore along the lines of pre-lisp introduction of "IF', per logical &&/|| for flow groupings [0] as primitive intro/demo concept of type theory (using/abusing basic combinatory logic[1] in high level program vs. easier to follow/modify language provided abstractions). logical && equivalent of do group of statments as long as all succeed logical || equivalent of do until at least one of the groupings in sequence succeeds. Per give "IF" would be written as: ( ( ((expr == false) && (expr2)) || ((true)) ) ) ---------------- [0] : https://thepalindrome.org/p/matrices-and-graphs https://thepalindrome.org/p/matrices-and-graphs [1] : John's Lambda Calculus and Combinatory Logic Playground : http://news.ycombinator.com/item?id=35125828 http://news.ycombinator.com/item?id=35125828
- kazinator 3y agoIt's not clear what exactly you mean by "pre-lisp" here. Lisp existed before && and || syntax, for more than a decade. There is nothing relevant in this area that is "pre-lisp", really. The Lisp 1 manual from 1960 already describes this: "AND is a special from to test if all the propositional expressions following AND in the list are true. The propostions are evaluated in sequence until one is found that is false or until the end of hte list is reached. The value of AND is respectively F or T." "OR is a special form to test [ ... similar description elided ]" Found on page 48. (The modern Common Lisp and returns the value of the last form if it is reached, and the modern or returns the value of the first form that is not nil. This is more useful since you can do (let ((obj (or (try-getting-object :from-here) (try-getting-object-another-way) (make-new-object :with-param 42)))) ...) Given that short-circuiting, multi-argument AND and OR already existed in Lisp in 1960, we don't have to think about defining them in terms of C operators. The versions which return a useful value are a bit challenging. // if x is not zero or null, return x; otherwise return y // ... without evaluating x twice #define or(x, y) ... good luck! We need GCC typeof and expression compound statement. Something like: #define or(x, y) ({ __typeof(x) __tempvar = (x); __tempvar ? __tempvar : (y); }) I just remembered GCC has a specific extension for this. You can omit the middle operand of the ? : ternary (a feature not in ISO C): #define or(x, y) ((x) ? : (y)) https://gcc.gnu.org/onlinedocs/gcc/Conditionals.html https://gcc.gnu.org/onlinedocs/gcc/Conditionals.html
- sargstuff 3y agook, difference of predicate vs. propositional based programming language. aka unix bash vs. lisp
- kazinator 3y agoWhat? That's the same thing; e.g predicate calculus deals with logical propositions.
- sargstuff 3y agoAnd the reasoning for making the distinction between 1st order logic vs. 2nd order logic is? More appropriate terminology: Use of logic and/or to form stream of programming language statments via concatination, where &,|,&&,|| used instead of if..then, case, etc. Use cases of in-language take of higher level programming language stream of statements in mannar akin to rex, sed, awk. Non-compiler, simpler/smaller hand rolled version of end result of linearization a compiler would normally do. lisp example:[0] [0] LinearLisp: a proof of concept in full linearization for AST-walking interpreters
- sargstuff 3y agotruth table is correspondce between conditional arguement if p then q vs. programming language IF <logic expr> then <statement1> Truth table being the Backus Naur Arts and Entertainment description. p, q in conditional arguement doesn't mean to much until define what truth values of p, q are. IF .. THEN statement being a higher order form of conditional arguement if p then q; not to much use until define the TYPE of expr and TYPE of statments valid within programming language statement IF .. THEN
- tacostakohashi 3y agoPerl went to town on having lots of logical operators and blurring (rather arbitrary) line between "flow control" and "logical operators", e.g.: open( my $fh_file, "<", $file ) or die "Could not open file '$file' $!"; do_thing() unless predicate(); do_thing() if predicate(); do_thing() until predicate(); ... and lots more.