Eelco Dolstra <eelco.dols...@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 <f...@chaoflow.net>
  GPG FPR: 7A13 5EEE 1421 9FC2 108D  BAAF 38F8 99A3 0C45 F083
Jabber/XMPP: f...@chaoflow.net
IRC: chaoflow on freenode,ircnet,blafasel,OFTC

Attachment: pgp8iF8aNwDVe.pgp
Description: PGP signature

_______________________________________________
nix-dev mailing list
nix-dev@lists.science.uu.nl
http://lists.science.uu.nl/mailman/listinfo/nix-dev

Reply via email to