About cycles structures

What happens

I have a doubt respect the use of cycles structures differents to while and for loop

What do you understand or find about that problem

I know that now we have to avoid use while and for loops however there are structures that have a similar behaviour for example in Lisp, which is language that I using now, we have cycles structures as (dolist (x var)) this sturctures have exactly the same apparent behaviour that “for x in var” from python for example, both following codes make exactly same thing.

//Python.
fruits = ["apple", "banana", "cherry"]
for x in fruits:
    print (x)

//LISP
(setq fruits (list "apple"  "banana" "cherry"))
(dolist (x fruits)
  (print x))

Then, could I use that structure (dolist) since itsn’t a for or while loop?, or on the contrary I should avoid due that x object is changing as in a for loop.

You make any workaround? What did you do?

In this moment I’m sure that I could totally avoid the use of cycles structures however when I need to replace a nested loop I have to send counters to the functions for going through nested list.

Hi, in a lot of langs you can found ways to avoid the use of loops, for example on JS:

const fruits = ["apple", "banana", "cherry"];
fruits.map((val) => console.log(val));

And with that, you can do the same thing as in LISP or PYTHON, but with that kind of functions (named high order functions) you can’t mutate the object/array itself.

On JS or TS you can use the for loop to make the same thing, but with that, you aren’t using a high order function and you are using a simple loop, you should figure out when something is a loop or not.

for example a little fragment about the discussion if map is a loop or not in perl:

map is a higher level concept than loops, borrowed from functional programming. It
doesn't say "call this function on each of these items, one by one, from beginning to
end," it says "call this function on all of these items." It might be implemented as a loop,
but that's not the point -- it also might be implemented asynchronously -- it would still be
map.

source: Stackoverflow

I hope that you can understand that is hard to define what kind of things are loops and what things aren’t, with that you should use only the functions that are compliant with a functional paradigm, on the web you can search and find for each lang how to work with functional programming.

1 Like