How to resolve the algorithm Multiple distinct objects step by step in the EchoLisp programming language
How to resolve the algorithm Multiple distinct objects step by step in the EchoLisp programming language
Table of Contents
Problem Statement
Create a sequence (array, list, whatever) consisting of n distinct, initialized items of the same type. n should be determined at runtime. By distinct we mean that if they are mutable, changes to one do not affect all others; if there is an appropriate equality operator they are considered unequal; etc. The code need not specify a particular kind of distinction, but do not use e.g. a numeric-range generator which does not generalize. By initialized we mean that each item must be in a well-defined state appropriate for its type, rather than e.g. arbitrary previous memory contents in an array allocation. Do not show only an initialization technique which initializes only to "zero" values (e.g. calloc() or int a[n] = {}; in C), unless user-defined types can provide definitions of "zero" for that type. This task was inspired by the common error of intending to do this, but instead creating a sequence of n references to the same mutable object; it might be informative to show the way to do that as well, both as a negative example and as how to do it when that's all that's actually necessary. This task is most relevant to languages operating in the pass-references-by-value style (most object-oriented, garbage-collected, and/or 'dynamic' languages). See also: Closures/Value capture
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Multiple distinct objects step by step in the EchoLisp programming language
Source code in the echolisp programming language
;; wrong - make-vector is evaluated one time - same vector
(define L (make-list 3 (make-vector 4)))
L → (#(0 0 0 0) #(0 0 0 0) #(0 0 0 0))
(vector-set! (first L ) 1 '🔴) ;; sets the 'first' vector
L → (#(0 🔴 0 0) #(0 🔴 0 0) #(0 🔴 0 0))
;; right - three different vectors
(define L(map make-vector (make-list 3 4)))
L → (#(0 0 0 0) #(0 0 0 0) #(0 0 0 0))
(vector-set! (first L ) 1 '🔵) ;; sets the first vector
L → (#(0 🔵 0 0) #(0 0 0 0) #(0 0 0 0)) ;; OK
You may also check:How to resolve the algorithm Sorting algorithms/Counting sort step by step in the PL/I programming language
You may also check:How to resolve the algorithm Reverse a string step by step in the Gambas programming language
You may also check:How to resolve the algorithm Factorial step by step in the Clio programming language
You may also check:How to resolve the algorithm MD5 step by step in the Lingo programming language
You may also check:How to resolve the algorithm Move-to-front algorithm step by step in the Elixir programming language