Another option is to explore "concat"
On Thu, Apr 24, 2014 at 8:46 AM, Jiacai Liu <[email protected]> wrote: > Well,I understand you. > list only support front insert since it only takes O(1). > If insert in the back,it would require O(n) time. > Maybe clojure has its own reasons for having both cons and conj. > Thank again for telling me their differences. > > > On Thursday, April 24, 2014 11:36:13 PM UTC+8, Gary Trakhman wrote: > >> Well, maybe I shouldn't bash MIT-scheme unintentionally :-). I bet some >> people use it for serious stuff, and I have no bad experiences with it. >> >> >> On Thu, Apr 24, 2014 at 11:33 AM, Gary Trakhman <[email protected]>wrote: >> >>> MIT-lisp is more for teaching than industrial use.. >>> >>> We also have hash-maps, vectors, sets, queues, conj works with all of >>> them, leaving the details up to the collection. >>> >>> Cons always adds to the front. Conj only adds to the back for certain >>> collections (vector, queue). >>> >>> You could work around it by reversing, consing, then reversing again, or >>> just use vectors and conj if you're always working on the tail. >>> >>> >>> On Thu, Apr 24, 2014 at 11:27 AM, Jiacai Liu <[email protected]> wrote: >>> >>>> Oh,Thank you for answer. >>>> But if I just want to insert a element to list,and meantime want to >>>> insert in the back.How should I do? >>>> Another question: why clojure need both cons and conj? In mit-lisp cons >>>> just do all things. >>>> >>>> >>>> On Thursday, April 24, 2014 11:12:49 PM UTC+8, Gary Trakhman wrote: >>>> >>>>> Conj is polymorphic on its first argument. If you pass it a vector, >>>>> it'll add to the back, if a list, the front. The collection is >>>>> responsible >>>>> for deciding the most efficient way to add an element. >>>>> >>>>> Cons always adds to the front, creating a linked-list node pointing to >>>>> the rest. Also, cons takes the sequence as the second argument. >>>>> >>>>> >>>>> On Thu, Apr 24, 2014 at 11:10 AM, Jiacai Liu <[email protected]>wrote: >>>>> >>>>>> hi everyone: >>>>>> I use Clojure to solve SICP 2.22 >>>>>> <http://www.billthelizard.com/2011/01/sicp-221-223-mapping-over-lists.html> >>>>>> . >>>>>> The problem is to rewrite a map fn in a iterative way,here it want to >>>>>> get the square of each element in a list >>>>>> Method 1: >>>>>> (defn square-list [items] >>>>>> (defn iter [things answer] >>>>>> (if (empty? things) >>>>>> answer >>>>>> (iter >>>>>> (rest things) >>>>>> (cons (square (first things)) answer)))) >>>>>> (iter items nil)) >>>>>> This method just return opposite result. >>>>>> (square-list (range 1 10)) >>>>>> ;===>(81 64 49 36 25 16 9 4 1) >>>>>> >>>>>> I think all I need is to swap the position of (square (first >>>>>> things))andanswer,then >>>>>> I can get the right order >>>>>> I write fn below: >>>>>> Method 2 >>>>>> (defn square-list [items] >>>>>> (defn iter [things answer] >>>>>> (if (empty? things) >>>>>> answer >>>>>> (iter >>>>>> (rest things) >>>>>> (conj answer (square (first things)))))) >>>>>> (iter items nil)) >>>>>> However, it still return opposite order. >>>>>> Did I misuse cons and conj or I ignore something? >>>>>> >>>>>> >>>>>> -- >>>>>> You received this message because you are subscribed to the Google >>>>>> Groups "Clojure" group. >>>>>> To post to this group, send email to [email protected] >>>>>> >>>>>> Note that posts from new members are moderated - please be patient >>>>>> with your first post. >>>>>> To unsubscribe from this group, send email to >>>>>> [email protected] >>>>>> >>>>>> 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 [email protected]. >>>>>> >>>>>> For more options, visit https://groups.google.com/d/optout. >>>>>> >>>>> >>>>> -- >>>> You received this message because you are subscribed to the Google >>>> Groups "Clojure" group. >>>> To post to this group, send email to [email protected] >>>> Note that posts from new members are moderated - please be patient with >>>> your first post. >>>> To unsubscribe from this group, send email to >>>> [email protected] >>>> 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 [email protected]. >>>> For more options, visit https://groups.google.com/d/optout. >>>> >>> >>> >> -- > You received this message because you are subscribed to the Google > Groups "Clojure" group. > To post to this group, send email to [email protected] > Note that posts from new members are moderated - please be patient with > your first post. > To unsubscribe from this group, send email to > [email protected] > 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 [email protected]. > For more options, visit https://groups.google.com/d/optout. > -- You received this message because you are subscribed to the Google Groups "Clojure" group. To post to this group, send email to [email protected] Note that posts from new members are moderated - please be patient with your first post. To unsubscribe from this group, send email to [email protected] 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 [email protected]. For more options, visit https://groups.google.com/d/optout.
