[Nix-dev] Nix 1.2 released
Florian Friesdorf
flo at chaoflow.net
Wed Dec 12 19:12:19 CET 2012
Eelco Dolstra <eelco.dolstra at logicblox.com> writes:
> Hi,
>
> On 12/12/12 17:15, Shea Levy wrote:
>
>>>> The elem library function evaluates all list elements instead of
>>>> returning "true" after finding a matching element.
>>> Sure about that? This seems lazy enough:
>>>
>>> elem =
>>> builtins.elem or
>>> (x: list: fold (a: bs: x == a || bs) false list);
>>>
>> Shouldn't the bs come first? i.e. (a: bs: bs || x == a)?
>
> Not if you want to check from left to right.
Sorry, but I'm not getting it yet. What I understand:
list = [0, 0, 1, 2, 3];
x = 1;
elem x list
-> a: 3, x == a: false, bs: false
-> a: 2, x == a: false; bs: false
-> a: 1, x == a: true; bs: false
-> a: 0, x == a: false; bs: true
-> a: 0, x == a: false; bs: true
Probably missing something very basic.
For completeness sake, fold from lib/lists:
fold =
if builtins ? elemAt
then op: nul: list:
let
len = length list;
fold' = n:
if n == len
then nul
else op (builtins.elemAt list n) (fold' (add n 1));
in fold' 0
else op: nul:
let fold' = list:
if list == []
then nul
else op (head list) (fold' (tail list));
in fold';
The following would not evaluate all, I'd currently claim:
elem' = x: list: if list == []
then false
else head list == x || elem' x (tail list);
--
Florian Friesdorf <flo at chaoflow.net>
GPG FPR: 7A13 5EEE 1421 9FC2 108D BAAF 38F8 99A3 0C45 F083
Jabber/XMPP: flo at chaoflow.net
IRC: chaoflow on freenode,ircnet,blafasel,OFTC
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 835 bytes
Desc: not available
Url : http://lists.science.uu.nl/pipermail/nix-dev/attachments/20121212/ee29616c/attachment.bin
More information about the nix-dev
mailing list