Note that there are some explanatory texts on larger screens.

plurals
  1. POUse of OR as branch control in FP
    primarykey
    data
    text
    <p>I undertook an interview last week in which I learnt a few things about python I didn't know about (or rather realise how they could be used), first up and the content of this question is the use of <code>or</code> for the purposes of branch control. </p> <p>So, for example, if we run:</p> <pre><code>def f(): # do something. I'd use ... but that's actually a python object. def g(): # something else. f() or g() </code></pre> <p>Then if <code>f()</code> evaluates to some true condition then that value is returned, if not, <code>g()</code> is evaluated and whatever value it produces is returned, whether true or false. This gives us the ability to implement an <code>if</code> statement using <code>or</code> keywords.</p> <p>We can also use <code>and</code> such that <code>f() and g()</code> will return the value of <code>g()</code> if <code>f()</code> is true and the value of <code>f()</code> if <code>g()</code> is false.</p> <p>I am told that this (the use of <code>or</code> for branch control) is a common thing in languages such as lisp (hence the lisp tag). I'm currently following <a href="http://mitpress.mit.edu/sicp/">SICP</a> learning Scheme, so I can see that <code>(or (f x) (g x))</code> would return the value of <code>(g x)</code> assuming <code>(f x)</code> is <code>#f</code>.</p> <p>I'm confused as to whether there is any advantage of this technique. It clearly achieves branch control but to me the built in keywords seem more self-explanatory.</p> <p>I'm also confused as to whether or not this is "functional"? My understanding of pure functional programming is that you use constructs like this (an example from my recent erlang experiments):</p> <pre><code>makeeven(N,1) -&gt; N+1; makeeven(N,0) -&gt; N; makeeven(N) -&gt; makeeven(N,N rem 2). </code></pre> <p>Or a better, more complicated example using <a href="http://bartoszmilewski.wordpress.com/2009/10/21/what-does-haskell-have-to-do-with-c/">template meta-programming in C++</a> (discovered via cpp-next.com). My thought process is that one aspect of functional programming boils down the use of <a href="http://en.wikipedia.org/wiki/Piecewise">piecewise defined functions in code</a> for branch control (and if you can manage it, tail recursion).</p> <p>So, my questions:</p> <ul> <li>Is this "functional"? It appears that way and my interviewers said they had backgrounds in functional programming, but it didn't match what I thought was functional. I see no reason why you couldn't have a logical operator as part of a function - it seems to lend itself nicely to the concept of higher order functions. I just hadn't thought that the use of logical operators was how functional programmers achieved branch control. Right? Wrong? I can see that circuits use logic gates for branch control so I guess this is a similar (related) concept?</li> <li>Is there some advantage to using this technique? Is it just language conciseness/a syntax issue, or are there implications in terms of building an interpreter to using this construct?</li> <li>Are there any use cases for this technique? Or is it not used very often? Is it used at all? As a self-taught guy I'd never seen it before although that in itself isn't necessarily surprising.</li> </ul> <p>I apologise for jumping over so many languages; I'm simply trying to tie together my understanding across them. Feel free to answer in any language mentioned. I also apologise if I've misunderstood any definitions or am missing something vital here, I've never formally studied computer science.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload