mardi 4 décembre 2018

Avoid using set! into functional programming style algorithm

I'm not sure if I can ask this question here. If this is not the right place, please tell me and I will delete it.

I'm learning Racket and someone has told me something about avoiding using set! in functional programming style. But I'm confused, I don't understand the meaning of "functional programming style". Just to learn, I want to ask this question.

I have the following code:

 (define lst1 '())

 (do ([i n (- i 1)])
   ((zero? i))
   ; Get an item.
   (set! item (random-chooser original-list))
   ; Insert it into an auxiliary list, lst1
   (set! lst1 (cons item lst1))
   ; Remove the item from the original list
   (set! original-list (remove item original-list)))

 (append (list lst1) (list original-list))))))

This code is working perfectly. I have to choose n items from original-list list randomly, without repetition. And then create a list with two sub-lists with the n items chosen in lst1 and as the second sub-list, the remaining items on original-list.

Is there a better way to do this without using set!?




Aucun commentaire:

Enregistrer un commentaire