3 ms·
Apparently functional programming is considered a form of declarative programming. That doesn't make much sense to me, but I guess relative to writing assembly
by deckar01 8y ago
Apparently functional programming is considered a form of declarative programming. That doesn't make much sense to me, but I guess relative to writing assembly or C it is declarative. The distinction seems arbitrary, because even C and assembly contain abstractions for more complex operations taking place in the OS and in the hardware.
- deleted 8y ago[deleted]
- taffer 8y agoThe difference is referential transparency: In declarative programming, an expression always returns the same result, no matter where and in which order you use it. Contrast this with an expression like a += 1; in C, which returns a different result each time.
- rgoulter 8y agoA cliche example is "squares of values". imperative code would be something like: fn squares(array A) array B; for (idx, x) in A { B[idx] = x * x } return B; } (or "B.add" or some other mutating action). whereas 'declarative' code 'declares' what it wants: fn squares(array A) { return A.map { |x| x * x } } I think "declarative" is used more in terms of "declare the result you want, not how you'll get it". It's easier to focus on what the output of the program is with declarative code. Higher-level abstractions are probably necessary, but I don't think it's fundamental to how I'd use the term. :-) Although I wouldn't be surprised to see a variety of ways people use the term.
- jtms 8y agoIs that example code elixir?
- rgoulter 8y agoIt's not anything in particular. Rosetta Code has "apply a callback to an array" example with Elixir. https://rosettacode.org/wiki/Apply_a_callback_to_an_array#Elixir https://rosettacode.org/wiki/Apply_a_callback_to_an_array#El... Enum.map([1, 2, 3], fn(n) -> n * 2 end) Enum.map [1, 2, 3], &(&1 * 2)