Effectiveness of the functional programming model (Erlang specific)

Hi, I am new to Erlang world. When I think about how we need to solve the following problem (and there is a long list of similar ones), I think that this is really inefficient, because we are talking about big recursion. Apparently, a language like C / Java doesn't need a clumsy recursion to solve this problem, but with Erlang (I think there could be another functional programming language too?), You should do it this way.


Example 3 - Add

This program combines two lists:

append([], List) -> List;

append([First|Rest], List) -> [First | append(Rest,List)].

Can someone give an explanation why this is not a problem?

+3
source share
5 answers

, Erlang .

, , Erlang , "" , , . , .

, . (. SO) , , ++, , , ..

, Java ++, , .

+12

, C. , ( ), , , . -, , , , , C ( , ?), .

, ?;)

, - ( ) . , ... , ( , , )... fib (n).

+6

, , ?

Erlang ( .Net IL), .

, CPU - :

var first_list, second_list

label START_ITER:
  if(first_list is empty)
    goto FINISH

  var first_elem = first_list[0]
  var first_list.start_element = first_list[1]

  second_list[n+1] first_elem
  goto START_ITER

label FINISH:
  return second_list

for - :

var first_list, second_list

var i = 0
var limit = first_list.length

label START_ITER:
  if(i == limit)
    goto FINISH

  second_list[n+i+1] = first_list[i]
  i += 1
  goto START_ITER

label FINISH:
  // Done

, node , first_list. for , .

( goto) , . erlang.

, for , , , .

, " - , , "

+4

- (, , goto;-). , , " " (, ), , ... .

, . Haskell, append in ( Haskell) [First | append(Rest, List)] "thunk", , IF , . , , , - THAT thunk head/tail - , thunk .

() , ( , ;).

0

Source: https://habr.com/ru/post/1718869/


All Articles