Re: [Chicken-users] Replace an element in a list
Yes, it would be OK to use split-at to break a list into two pieces then reassemble it. That approach is relatively expensive though, since it's O(n) and involves re-allocating potentially many list nodes, which is why folks have suggested alternative approaches. Does it happen to be the case that you're trying to initialize a list one element at a time? I might be reading too much into this, but I get the sense you're trying to do something like for (i = 0; i n; i++) new_list[i] = expression; While that is commonplace and idiomatic in imperative languages, it's not in Scheme. Instead it'd be more Schemely to use something like map or list-tabulate from SRFI 1. Regards, Kevin Wortman On Sat, Dec 13, 2014 at 11:34 PM, Bahman Movaqar bah...@bahmanm.com wrote: Thanks Daniel and John. I found split-at while reading the docs last night. Along with let-values it seems like a natural answer to my questions. What do you folks think? -- Bahman Movaqar http://BahmanM.com - https://twitter.com/bahman__m https://github.com/bahmanm - https://gist.github.com/bahmanm PGP Key ID: 0x6AB5BD68 (keyserver2.pgp.com) On 12/14/2014 03:29 AM, Bahman Movaqar wrote: What is the idiomatic way of replacing the nth element in a list *without* mutating the list? Is the combination of take and take-right the right way to do it? ___ Chicken-users mailing list Chicken-users@nongnu.org https://lists.nongnu.org/mailman/listinfo/chicken-users ___ Chicken-users mailing list Chicken-users@nongnu.org https://lists.nongnu.org/mailman/listinfo/chicken-users
[Chicken-users] Replace an element in a list
What is the idiomatic way of replacing the nth element in a list *without* mutating the list? Is the combination of take and take-right the right way to do it? TIA, -- Bahman Movaqar http://BahmanM.com - https://twitter.com/bahman__m https://github.com/bahmanm - https://gist.github.com/bahmanm PGP Key ID: 0x6AB5BD68 (keyserver2.pgp.com) signature.asc Description: OpenPGP digital signature ___ Chicken-users mailing list Chicken-users@nongnu.org https://lists.nongnu.org/mailman/listinfo/chicken-users
Re: [Chicken-users] Replace an element in a list
There are setters for car and cdr, so let's say you're at a position where you want to replace the head of a list, you can then just do: (set! (car some-list) some-value) -Dan On Sat, Dec 13, 2014 at 3:59 PM, Bahman Movaqar bah...@bahmanm.com wrote: What is the idiomatic way of replacing the nth element in a list *without* mutating the list? Is the combination of take and take-right the right way to do it? TIA, -- Bahman Movaqar http://BahmanM.com - https://twitter.com/bahman__m https://github.com/bahmanm - https://gist.github.com/bahmanm PGP Key ID: 0x6AB5BD68 (keyserver2.pgp.com) ___ Chicken-users mailing list Chicken-users@nongnu.org https://lists.nongnu.org/mailman/listinfo/chicken-users ___ Chicken-users mailing list Chicken-users@nongnu.org https://lists.nongnu.org/mailman/listinfo/chicken-users
Re: [Chicken-users] Replace an element in a list
Bahman Movaqar scripsit: What is the idiomatic way of replacing the nth element in a list *without* mutating the list? Is the combination of take and take-right the right way to do it? Yes, that's how I'd do it. Daniel Leslie scripsit: There are setters for car and cdr, so let's say you're at a position where you want to replace the head of a list, you can then just do: (set! (car some-list) some-value) That's how you do it when you *do* want to mutate the list, or you can use list-set! if you have it (as R7RS does). -- John Cowan http://www.ccil.org/~cowanco...@ccil.org You annoy me, Rattray! You disgust me! You irritate me unspeakably! Thank Heaven, I am a man of equable temper, or I should scarcely be able to contain myself before your mocking visage. --Stalky imitating Macrea ___ Chicken-users mailing list Chicken-users@nongnu.org https://lists.nongnu.org/mailman/listinfo/chicken-users
Re: [Chicken-users] Replace an element in a list
*facepalm* Yes, my way is the way not to do it; thanks to my reading comprehension failure. ;) -Dan On 14-12-13 06:44 PM, John Cowan wrote: Bahman Movaqar scripsit: What is the idiomatic way of replacing the nth element in a list *without* mutating the list? Is the combination of take and take-right the right way to do it? Yes, that's how I'd do it. Daniel Leslie scripsit: There are setters for car and cdr, so let's say you're at a position where you want to replace the head of a list, you can then just do: (set! (car some-list) some-value) That's how you do it when you *do* want to mutate the list, or you can use list-set! if you have it (as R7RS does). ___ Chicken-users mailing list Chicken-users@nongnu.org https://lists.nongnu.org/mailman/listinfo/chicken-users
Re: [Chicken-users] Replace an element in a list
Thanks Daniel and John. I found split-at while reading the docs last night. Along with let-values it seems like a natural answer to my questions. What do you folks think? -- Bahman Movaqar http://BahmanM.com - https://twitter.com/bahman__m https://github.com/bahmanm - https://gist.github.com/bahmanm PGP Key ID: 0x6AB5BD68 (keyserver2.pgp.com) On 12/14/2014 03:29 AM, Bahman Movaqar wrote: What is the idiomatic way of replacing the nth element in a list *without* mutating the list? Is the combination of take and take-right the right way to do it? signature.asc Description: OpenPGP digital signature ___ Chicken-users mailing list Chicken-users@nongnu.org https://lists.nongnu.org/mailman/listinfo/chicken-users