zlacker

[parent] [thread] 1 comments
1. tobias+(OP)[view] [source] 2025-12-07 01:23:38
How is "If" as a function even a drawback? It is largely seen as something desired, no? I would see that as a huge advantage, which allows for very powerful programming and meta-programming techniques.
replies(1): >>gucci-+7c
2. gucci-+7c[view] [source] 2025-12-07 04:20:37
>>tobias+(OP)
One potential issue is that unlike most other languages, it doesn't create a new scope. But almost nothing in Mathematica introduces a new scope, and Python also uses unscoped "if"s, so it's rarely much of a problem in practice.

But with pattern matching, you almost never need to use "If[]" anyways:

  fib[0] := 0
  fib[1] := 1
  fib[n_ /; n < 2] := Undefined
  fib[n_Integer] := fib[n - 1] + fib[n - 2]

  fib[8]
  (* Output: 21 *)

  fib /@ Range[10]
  (* Output: {1, 1, 2, 3, 5, 8, 13, 21, 34, 55} *)

  fib[-1]
  (* Output: Undefined *)

  fib["a string"]
  (* Output: fib["a string"] *)
[go to top]