It is a singly linked list, so the natural position at which to add is the 
front. conj will add to the front of lists and to the end of vectors.

Consider using a vector if you want to append to the end. It'd be more 
semantically correct.

More background: If you append to the end of a singly linked list, generally, 
you'd have to follow the references starting from the first element until you 
reached the last element. Clojure uses persistent data structures, meaning when 
you add an item to a collection you share structure with the original item. In 
the case of a singly linked list, adding an item to the front is as simple as 
creating a new list that points to the old list. E.g., adding 5 to the linked 
list 2 -> 3 -> 4 can be thought of as 5 -> 2 -> 3 -> 4.

---
Joe R. Smith
j...@uwcreations.com <mailto:j...@uwcreations.com> 
@solussd


On Jul 17, 2021, at 2:21 PM, Tanya Moldovan <tanya.moldo...@gmail.com 
<mailto:tanya.moldo...@gmail.com> > wrote:



Hi,

conj <https://clojuredocs.org/clojure.core/conj>  adds at the end of a vector, 
but at the beginning of a list. It is how it is implemented. I think this 
<https://stackoverflow.com/questions/5734435/put-an-element-to-the-tail-of-a-collection>
  and this <https://medium.com/@greg_63957/conj-cons-concat-oh-my-1398a2981eab> 
 sums it up why.

You could achieve what you want by using concat 
<https://clojuredocs.org/clojure.core/concat> (note this returns a LazySeq):

user=> (concat grid '((off 1 2 3 6)))
(-> (grid 10 10) (toggle 2 3 4 5) (off 2 3 4 5) (on 2 3 4 5) (off 1 2 3 6))

Though I'm not exactly sure what is the end goal of this but I'd rethink the 
way it is done. 



On Sat, 17 Jul 2021 at 14:24, SideStep <nesvarbu.vi...@gmail.com 
<mailto:nesvarbu.vi...@gmail.com> > wrote:

 <https://stackoverflow.com/posts/68420449/timeline> 
 
 

I have a representation of a matrix in grid.clj file:


(-> (grid 10 10)
    (toggle 2 3 4 5)
    (off 2 3 4 5)
    (on 2 3 4 5))


It's a list of functionts, first one initializes a grid, others modify it.
Clojures' 'code is data' supposed to make it easy for me to modify that 
representation by adding an instrucion at the end of collection. List is an 
ordered collection right? Order matters. How do I add an instruction to the end 
of the list then?
Something like this:


(def grid (read-string (slurp "grid.clj")))
(conj grid '(off 1 2 3 6))


Yet I can't add to the end of the list, which is a data structure that is 
evaluatable as code. How is it 'code as data' if I can't add to the end of the 
ordered collection that is meant for code (as data)?

 

-- 
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clojure@googlegroups.com 
<mailto:clojure@googlegroups.com> 
Note that posts from new members are moderated - please be patient with your 
first post.
To unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com 
<mailto:clojure%2bunsubscr...@googlegroups.com> 
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en 
<http://groups.google.com/group/clojure?hl=en> 
--- 
You received this message because you are subscribed to the Google Groups 
"Clojure" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to clojure+unsubscr...@googlegroups.com 
<mailto:clojure+unsubscr...@googlegroups.com> .
To view this discussion on the web visit 
https://groups.google.com/d/msgid/clojure/efd72013-a85e-46e8-b9db-10dde1a8a235n%40googlegroups.com
 
<https://groups.google.com/d/msgid/clojure/efd72013-a85e-46e8-b9db-10dde1a8a235n%40googlegroups.com?utm_medium=email&amp;utm_source=footer>
 .

-- 
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clojure@googlegroups.com 
<mailto:clojure@googlegroups.com> 
Note that posts from new members are moderated - please be patient with your 
first post.
To unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com 
<mailto:clojure+unsubscr...@googlegroups.com> 
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en 
<http://groups.google.com/group/clojure?hl=en> 
--- 
You received this message because you are subscribed to the Google Groups 
"Clojure" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to clojure+unsubscr...@googlegroups.com 
<mailto:clojure+unsubscr...@googlegroups.com> .
To view this discussion on the web visit 
https://groups.google.com/d/msgid/clojure/CADBYUPvzMtspZxxc5PhmkXyNg7kJqgMG%3DWVkUe2FKsf%2BGsdaFA%40mail.gmail.com
 
<https://groups.google.com/d/msgid/clojure/CADBYUPvzMtspZxxc5PhmkXyNg7kJqgMG%3DWVkUe2FKsf%2BGsdaFA%40mail.gmail.com?utm_medium=email&amp;utm_source=footer>
 .

-- 
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clojure@googlegroups.com
Note that posts from new members are moderated - please be patient with your 
first post.
To unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en
--- 
You received this message because you are subscribed to the Google Groups 
"Clojure" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to clojure+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/clojure/0100017ab5f8ffe9-ab08f4ce-32ff-480f-b780-afa11b7938d2-000000%40email.amazonses.com.

Reply via email to