Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Learning functional programming - having trouble conceptualizing "no if statements" [duplicate]

I was discussing programming with a friend, who is an advocate for functional programming. He mentioned that you don't need to use if statements, but I can't seem to conceptualize how you would implement

if (something):
    do this;

else:
    do something_else;

In a functional paradigm?

Edit: my friend specifically mentioned that there are cases where you wouldn't need to use an if expression, even though you can. For example:

if x is odd:
    x + 1
else:
    x / 2

Is there a way to implement the above without using any if statements or conditionals?

like image 557
Jon Martin Avatar asked Feb 16 '23 06:02

Jon Martin


2 Answers

Without more context it's hard to know exactly what your friend meant, but two things come to mind that he could have reasonably meant:

In functional languages if conditionals are expressions, not statements, so you'd be using if expressions and not if statements. This difference means that you write things like:

let x =
  if condition
  then value1
  else value2

Instead of:

let x be a mutable variable
if condition
then x = value1
else x = value2

So this allows you to write in functional style without mutating variables.


The other thing he could have meant is that many functional languages offer constructs like pattern matching or guards that you can use instead of if statements. Pattern matching allows you to inspect the structure of a value and take it apart at the same time. As an example you can write this:

match my_list with
| x :: xs -> x + sum xs
| [] -> 0

Instead of this:

if my_list is empty
then
  let x be the first element of my_list
  let xs be the list containing the remaining elements of my_list
  x + sum xs

Using pattern matching is preferable because it avoids calling functions on a value whose structure does not support it. In the example above, a function that returns the first element of a list would presumably cause an error when called on an empty list (which might happen if we mess up the if condition). But if we use pattern matching to get at the first element this can't happen because the syntax of the matching construct ensures that we only get x and xs if my_list is really not empty.

Pattern guards allow you to add arbitrary conditions to pattern matching:

match f(x) with
| 0 -> "f(x) was zero"
| 1 -> "f(x) was one"
| x when x > 1 -> "f(x) was greater than one"
| _ -> "f(x) was negative"

This can be cleaner if you're pattern matching anyway, but that hardly means you shouldn't use if expressions in functional languages. If you don't have a situation where you want pattern match on a value, introducing a pattern match just so that you can use a guard makes little sense over using an if statement.

like image 101
sepp2k Avatar answered May 23 '23 06:05

sepp2k


The part that should confuse you isn't the if, it's the "do".

In functional programming, you don't "do" anything.
You just define the result to be some function of the input.

The function may of course have conditionals (like cond ? a : b in languages like C#, Java, C++, etc.), but a and b are expressions that evaluate to some common type; they are not statements -- so the result is either a or b, depending on cond.

like image 24
user541686 Avatar answered May 23 '23 06:05

user541686