Does erlang have a hidden rownum on a list? -


this example of current code:

dataset = [1,2,3,4,5,6,7,8,9].  sequence = [3,4,5,6].  reduceddataset = lists:foldl( fun(seqnumber, output) ->                                  row = lists:nth(seqnumber, dataset),                                  [row|output]                               end,                               [],                               sequence                              ). 

reduceddataset ends [6,5,4,3] , if change lists:foldr, reduceddataset [3,4,5,6].

i didn't expect when absorbed left right, 3rd value 3 , should proceed 6, when absorbed right left, 3rd value 7, , proceed 4.

does mean there's hidden row number on list, , foldl , foldr differ in sort order of final list?

tl;dr

no, there no hidden index or "row number" in erlang list.

discussion

it may helpful explore nature of list operations bit more in context of functional lists of "lists bunch of conses" variety.

i wrote explanation of folds while might useful you: explanation of lists:fold function

keep in mind functional lists have pointers go one-way. is, singly linked lists. there no concept of "rownum" or "index" in c style array. each call lists:nth/2 traversing list nth element before returning element.

we write lists:nth/2 if want version crashes on bad input (and, looking up, turns out it is written this):

nth(1, [element | _]) ->     element; nth(n, [_ | rest]) when n > 1 ->     lists:nth(n - 1, rest). 

(as side note, consider not inlining funs require write multi-line definitions function arguments...)


Comments

Popular posts from this blog

angular - Ionic slides - dynamically add slides before and after -

minify - Minimizing css files -

Add a dynamic header in angular 2 http provider -