There are styles of programming with no conditionals, especially in purely functional languages. A common technique inside compilers is to separate out which code gets run, and which result gets used. If everything is side-effect-free, then there's no harm in running the code whose result won't get used. In highly parallel systems, you can just run both versions and select at the end. So you can rewrite: if foo to: a…
Ask HN: Why Are Conditionals Important?
21–30 of 35 posts
Re: Ask HN: Why Are Conditionals Important?
#22To simplify, software (generally) does this: input >>> output By definition the output is conditional on the input, at least if the program works correctly. That's why conditional statements are important - they express the relationship between input and output. Isn't this really basic stuff? Or am I missing something?
How is the output of the following not conditional upon the input? Are there any conditional statements in it?
F(x, y) = x + y
Re: Ask HN: Why Are Conditionals Important?
#23Earlier quoted context omitted.
I suspect they are thinking about things like cellular automata. If you squint just right, you may think there are no conditionals. Just straight-line data flow to compute cell values based on a fixed neighborhood of the preceding state. A big fluid dynamics simulation has a similar characteristic. However, this squinty interpretation can just as simply show you that the whole thing is just a DFA, as mentioned a few…
But cellular automata have conditionals. That is the entire point of having a state in each cell and then doing something based on that state.
The earlier description of a cycle of functional updates to state sounds like the classic basis for latched digital circuit design. But, assuming the commenters were interested in software examples, I brought up analogous programs for cellular automata and grid simulations.
Also, since the state transition is purely functional with a finite input and output, it could theoretically be implemented with any functionally equivalent method, including flat lookup tables. No branching or conditional execution is required, even though it might be convenient.
Re: Ask HN: Why Are Conditionals Important?
#24To simplify, software (generally) does this: input >>> output By definition the output is conditional on the input, at least if the program works correctly. That's why conditional statements are important - they express the relationship between input and output. Isn't this really basic stuff? Or am I missing something?
You can have output that’s conditional on the input without having conditional statements. How is the output of the following not conditional upon the input? Are there any conditional statements in it? F(x, y) = x + y
Yes, of course you can.
Re: Ask HN: Why Are Conditionals Important?
#25Re: Ask HN: Why Are Conditionals Important?
#26Earlier quoted context omitted.
But cellular automata have conditionals. That is the entire point of having a state in each cell and then doing something based on that state.
I suspect the confusion in this thread really boils down to a lack of agreement about what "conditional" means. I think some commenters above think it means conditionally branched instruction streams. They don't consider arithmetic or logical operators, lookup tables, and demuxers to be conditionals. The earlier description of a cycle of functional updates to state sounds like the classic basis for latched digital ci…
If I have a lookup table of all the values of the sine and then use that lookup table to get the result of sine for specific value I haven't computed sine, I just looked it up.
Re: Ask HN: Why Are Conditionals Important?
#27Conditionals allow a program to react to the data it is presented with. This can be data from the outside world, or data from some other module internal to a program composed out of modules (i.e. subroutines, for example). Without conditionals - or their moral equivalent - programs would not be able to react to their inputs or to changes in their internal state. -- There are Turing-complete languages without conditio…
Also, say I implement the visitor pattern in some language on a classical machine - aren't there branch instructions being used under the covers anyway ? (the code will probably be more optimized / easier to optimize for the compiler but you get my point)
Re: Ask HN: Why Are Conditionals Important?
#28Conditionals allow a program to react to the data it is presented with. This can be data from the outside world, or data from some other module internal to a program composed out of modules (i.e. subroutines, for example). Without conditionals - or their moral equivalent - programs would not be able to react to their inputs or to changes in their internal state. -- There are Turing-complete languages without conditio…
this is interesting - thanks for your response. I'm having a hard time understanding the program you supplied though, the subsequent description did not really help (probs more my fault than your own). I certainly understand the visitor pattern (and have had a sneaky suspicion it is important) but the lambda calculus, not so much. What type of syntax is the program you supplied in - is it more of a pseudocode or an a…
~$ racket
Welcome to Racket v7.2.0.6.
> (define True (lambda (t f) (t)))
> (define False (lambda (t f) (f)))
> (define If (lambda (b tr fa) (b tr fa)))
> (define P (lambda (x)
(If x
(lambda () (display "It was true!"))
(lambda () (display "It was false!")))))
> (P True)
It was true!
> (P False)
It was false!
>
Seen differently, it's also close to the mathematical notation of the lambda calculus.To learn about the lambda calculus, check out chapter 5 of "Types and Programming Languages", Benjamin C. Pierce, MIT Press 2002. You might also get something out of "Semantics Engineering with PLT Redex", Felleisen, Findler & Flatt, MIT Press 2009.
Re: Ask HN: Why Are Conditionals Important?
#29Earlier quoted context omitted.
this is interesting - thanks for your response. I'm having a hard time understanding the program you supplied though, the subsequent description did not really help (probs more my fault than your own). I certainly understand the visitor pattern (and have had a sneaky suspicion it is important) but the lambda calculus, not so much. What type of syntax is the program you supplied in - is it more of a pseudocode or an a…
The syntax is concise definition of named values where the values are all anonymous functions. True = (lambda (t f) (t)) -- Let True be the function that takes two parameters t and f and returns the first, t. -- False = (lambda (t f) (f)) -- Let False be the function that takes two parameters t and f and returns the second, f. -- If = (lambda (b tr fa) (b tr fa)) -- Let If be the function that takes three parameters,…
Re: Ask HN: Why Are Conditionals Important?
#30Earlier quoted context omitted.
this is interesting - thanks for your response. I'm having a hard time understanding the program you supplied though, the subsequent description did not really help (probs more my fault than your own). I certainly understand the visitor pattern (and have had a sneaky suspicion it is important) but the lambda calculus, not so much. What type of syntax is the program you supplied in - is it more of a pseudocode or an a…
The syntax is close to Scheme and with small changes will run in a Scheme interpreter. For example, here's a session at the REPL of Racket: ~$ racket Welcome to Racket v7.2.0.6. > (define True (lambda (t f) (t))) > (define False (lambda (t f) (f))) > (define If (lambda (b tr fa) (b tr fa))) > (define P (lambda (x) (If x (lambda () (display "It was true!")) (lambda () (display "It was false!"))))) > (P True) It was tr…