infinisil changed the topic of #nix-lang to: Channel for discussing Nix as a language - https://nixos.org/nix/manual/#chap-writing-nix-expressions - Logs: https://logs.nix.samueldr.com/nix-lang/
manveru has quit []
manveru has joined #nix-lang
mpickering has quit [Ping timeout: 260 seconds]
mpickering has joined #nix-lang
__monty__ has joined #nix-lang
__monty__ has quit [Read error: Connection reset by peer]
__monty__ has joined #nix-lang
__monty__ has quit [Quit: leaving]
<infinisil> > toLinked' = prev: list: if builtins.length list == 0 then null else let this = { prev = prev; value = builtins.head list; next = toLinked' this (builtins.tail list); }; in this
<{^_^}> error: syntax error, unexpected '=', expecting ')', at (string):172:11
<infinisil> > toLinked2 = prev: list: if builtins.length list == 0 then null else let this = { prev = prev; value = builtins.head list; next = toLinked2 this (builtins.tail list); }; in this
<{^_^}> toLinked2 defined
<infinisil> > toLinked = list: toLinked2 null list
<{^_^}> toLinked defined
<infinisil> > test = toLinked [ 1 2 3 ]
<{^_^}> test defined
<infinisil> > test
<{^_^}> { next = <CODE>; prev = null; value = <CODE>; }
<gchristensen> oh dear
<infinisil> > test.next.next.prev.prev.value
<{^_^}> 1
<infinisil> :P
<infinisil> gchristensen: The discussion about parsing in #nixos-dev made me think of this
<infinisil> Could be used as the input band in a parser, because looping through a [ 1 2 3 ] with an index is probably inefficient
<infinisil> Although, maybe not
<infinisil> Haven't tested it
<gchristensen> does nix not use a linked list internally?
<gchristensen> I thought that was a pretty standard optimization for FPs
<infinisil> It might
<infinisil> "/* Return a list consisting of everything but the first element of a list. Warning: this function takes O(n) time, so you probably don't want to use it! */"
<infinisil> gchristensen: From the nix source code..
<gchristensen> neat lol
<gchristensen> which function?
<infinisil> tail
<infinisil> Apparently it stores stuff in an array, not a linked list
<gchristensen> maybe it is a reverse linked list? :X
<gchristensen> probably not
<infinisil> Seems to be an array of pointers to values
<gchristensen> nice.
<infinisil> Hmm.. so tail is inefficient..
<infinisil> elemAt is fast though!
<infinisil> Nice, so looping through a list via index is fast
<infinisil> And my doubly linked list from before has O(n^2) complexity to create..
<gchristensen> probably why tail is slow
<infinisil> Good to remember
<gchristensen> in fact, tail would be slow with a linked list since you'd have to navigate the list
<gchristensen> (I think)
<infinisil> When a list element is (value, next), where next is a pointer to the following (value', next'), then tail is just getting the next element
<gchristensen> oops right
<gchristensen> I forgot we were returning the list, and not just the last element :)
<infinisil> Yea
<infinisil> > toLinked (lib.range 1 100)
<{^_^}> { next = <CODE>; prev = null; value = <CODE>; }
<infinisil> > printLinked = list: if list == null then "" else list.value + "," + printLinked list.next
<{^_^}> printLinked defined
<infinisil> > printLinked (toLinked (lib.range 1 100))
<{^_^}> cannot add a string to an integer, at (string):108:51
<infinisil> > printLinked = list: if list == null then "" else toString list.value + "," + printLinked list.next
<{^_^}> printLinked defined
<infinisil> > printLinked (toLinked (lib.range 1 100))
<{^_^}> "1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50,51,52,53,54,55,56,57,58,59,60,61,62,63,64,65,66,67,68,69,7...
<infinisil> > printLinked (toLinked (lib.range 1 1000))
<{^_^}> "1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50,51,52,53,54,55,56,57,58,59,60,61,62,63,64,65,66,67,68,69,7...
<infinisil> > printLinked (toLinked (lib.range 1 10000))
<{^_^}> error: stack overflow (possible infinite recursion)
<infinisil> Heh
<infinisil> > toLinkedAltHelper = linked: n: list: { value = builtins.elemAt n list; prev = if n == 0 then null else builtins.elemAt (n-1) linked; next = if n == builtins.length list - 1 then null else builtins.elemAt (n+1) linked; }
<{^_^}> error: undefined variable 'n-1' at (string):150:122
<infinisil> > toLinkedAltHelper = linked: n: list: { value = builtins.elemAt n list; prev = if n == 0 then null else builtins.elemAt (n - 1) linked; next = if n == builtins.length list - 1 then null else builtins.elemAt (n + 1) linked; }
<{^_^}> toLinkedAltHelper defined
<infinisil> > toLinkedAltHelper = linked: list: n: { value = builtins.elemAt n list; prev = if n == 0 then null else builtins.elemAt (n - 1) linked; next = if n == builtins.length list - 1 then null else builtins.elemAt (n + 1) linked; }
<{^_^}> toLinkedAltHelper defined
<infinisil> > toLinkedAlt = list: let linked = map (toLinkedAltHelper linked list) (lib.range 0 (builtins.length list - 1)); in linked
<{^_^}> toLinkedAlt defined
<infinisil> > toLinkedAlt [ 1 2 3 ]
<{^_^}> [ <CODE> <CODE> <CODE> ]
<infinisil> > toLinkedAlt = list: let linked = map (toLinkedAltHelper linked list) (lib.range 0 (builtins.length list - 1)); in builtins.head linked
<{^_^}> toLinkedAlt defined
<infinisil> > toLinkedAlt [ 1 2 3 ]
<{^_^}> { next = <CODE>; prev = <CODE>; value = <CODE>; }
<infinisil> > (toLinkedAlt [ 1 2 3 ]).next.next.prev.prev.value
<{^_^}> value is a list while an integer was expected, at (string):151:192
<infinisil> > toLinkedAltHelper = linked: n: list: { value = builtins.elemAt list n; prev = if n == 0 then null else builtins.elemAt linked (n - 1); next = if n == builtins.length list - 1 then null else builtins.elemAt linked (n + 1); }
<{^_^}> toLinkedAltHelper defined
<infinisil> > toLinkedAlt = list: let linked = map (toLinkedAltHelper linked list) (lib.range 0 (builtins.length list - 1)); in builtins.head linked
<{^_^}> toLinkedAlt defined
<infinisil> > (toLinkedAlt [ 1 2 3 ]).next.next.prev.prev.value
<{^_^}> value is an integer while a list was expected, at (string):151:152
<infinisil> Darn
<infinisil> > builtins.elemAt [ 1 ] 1
<{^_^}> list index 1 is out of bounds, at (string):177:1
<infinisil> > builtins.elemAt [ 1 ] 0
<{^_^}> 1
<infinisil> > toLinkedAltHelper = linked: list: n: { value = builtins.elemAt list n; prev = if n == 0 then null else builtins.elemAt linked (n - 1); next = if n == builtins.length list - 1 then null else builtins.elemAt linked (n + 1); }
<{^_^}> toLinkedAltHelper defined
<infinisil> > (toLinkedAlt [ 1 2 3 ]).next.next.prev.prev.value
<{^_^}> 1
<infinisil> There we go
<infinisil> > printLinked (toLinkedAlt (lib.range 1 10000))
<{^_^}> error: stack overflow (possible infinite recursion)
<infinisil> > printLinked (toLinkedAlt (lib.range 1 1000))
<{^_^}> "1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50,51,52,53,54,55,56,57,58,59,60,61,62,63,64,65,66,67,68,69,7...
<infinisil> Oh well
<infinisil> > index = n: list: if n == 0 then list.value else index (n - 1) list.next
<{^_^}> index defined
<infinisil> > index 9999 (toLinkedAlt (lib.range 1 10000))
<{^_^}> error: stack overflow (possible infinite recursion)
<infinisil> > index 999 (toLinkedAlt (lib.range 1 1000))
<{^_^}> 1000
<infinisil> > index 999 (toLinked (lib.range 1 1000))
<{^_^}> 1000
<infinisil> > index 9999 (toLinked (lib.range 1 10000))
<{^_^}> error: stack overflow (possible infinite recursion)
<infinisil> > randomList = length: r (map (_: rand) (lib.range 1 length))
<{^_^}> randomList defined
<infinisil> > p (randomList 10)
<{^_^}> "[ 1468586160 1063916329 2360792750 3621166159 1041944540 1021819109 2937958330 3205148267 2859666888 299892321 ]"
<infinisil> > builtins.deepSeq (toLinked (lib.range 1 10000))
<{^_^}> <PRIMOP-APP>
<infinisil> > builtins.deepSeq (toLinked (lib.range 1 10000)) null
<{^_^}> null
<infinisil> > builtins.deepSeq (toLinkedAlt (lib.range 1 10000)) null
<{^_^}> null
<infinisil> Aha!
<infinisil> > builtins.deepSeq (toLinked (lib.range 1 100000)) null
<infinisil> If the 10000 elements took about 4 seconds and it has O(n^2) complexity, I expect 100000 elements to take about 400 seconds
<{^_^}> error: stack overflow (possible infinite recursion)
<infinisil> Or that lol
<infinisil> > builtins.deepSeq (toLinkedAlt (lib.range 1 100000)) null
<{^_^}> error: stack overflow (possible infinite recursion)
<infinisil> gchristensen: ^^ toLinked is tail based, toLinkedAlt is elemAt based. This comparison here really shows the difference in runtime
<infinisil> > builtins.deepSeq (toLinked (lib.range 1 50000)) null
<{^_^}> null
<infinisil> > builtins.deepSeq (toLinkedAlt (lib.range 1 50000)) null
<{^_^}> null
<gchristensen> nice
__monty__ has joined #nix-lang
{`-`} has joined #nix-lang
Guest29651 is now known as benley
benley has left #nix-lang ["WeeChat 2.1"]
__monty__ has quit [Quit: leaving]
{^_^} has quit [Remote host closed the connection]
{^_^} has joined #nix-lang