|
From: | Bahman Movaqar |
Subject: | Re: [Chicken-users] Idiomatic way to access nth element of a list |
Date: | Sun, 14 Dec 2014 20:24:25 +0330 |
User-agent: | Mozilla/5.0 (X11; Linux x86_64; rv:31.0) Gecko/20100101 Thunderbird/31.3.0 |
On 12/14/2014 09:18 PM, Richard wrote:
Hello Bayman, The get to the nth element of a list one would use list-ref. For example: (list-ref (list 1 2 3 4) 2) -> 3 The problem with linked lists is that in order to get to the nth element the list has to be traversed, which does bring a performance penalty. You can use vectors instead of list if you want to access/modify elements in constant time. If you need a vector that can dynamically grow take a look at the dyn-vector egg: http://wiki.call-cc.org/eggref/4/dyn-vector. Bahman Movaqar writes:How would a seasoned Schemer access the nth element of a list? (drop)? Does it have any performance penalty? Thanks Richard and Shawn for the answer. For my use case I find it acceptable to use the cleaner more comprehensive list interface (rather than vectors) at the expense of the performance penalty. -- 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
[Prev in Thread] | Current Thread | [Next in Thread] |