IRC channel logs

2023-04-16.log

back to list of logs

<cow_2001>awww, i can't make guile go into an infinite loop by walking along cyclic "lists"
<RhodiumToad>... really?
<cow_2001>try (last-pair cyclic-list)
<cow_2001>oh
<cow_2001>it's only the built in last pair
<cow_2001>if i write my own last-pair it goes on and on
<RhodiumToad>the builtin last-pair is explicitly documented as returning error for cyclic structures
<cow_2001>yes
<cow_2001>i first assumed it was built in behaviour but then ,describe last-pair proved otherwise
<RhodiumToad>note though that list? is false for improper lists
<RhodiumToad>so anything that checks its args for list? rather than pair?/null? will error on cyclics
<cow_2001>i've recently learned how they check for cycles and it's *clever* :|
<cow_2001>let two pointers walk along the list, one at +1 strides the other at +2. if they meet, it's cyclic, otherwise, not
<RhodiumToad>still O(n) in the list length, though
<RhodiumToad>the main benefit is O(1) storage
<cow_2001>RhodiumToad: your notes go into my sicp solutions!
<RhodiumToad>(I don't know if the scheme spec requires list? to be false for cyclics)
<cow_2001> https://index.scheme.org says "Returns #t if obj is a list. Otherwise, it returns #f. By definition, all lists have finite length and are terminated by the empty list."
<RhodiumToad>looks like it does
<cow_2001>what do you mean by O(1) storage?
<RhodiumToad>right, so improper and cyclic lists return false
<RhodiumToad>the tortoise/hair algorithm requires only two pointers regardless of the list length
<RhodiumToad>*hare
<cow_2001>or! right! i forgot that an improper list is one where the cdr of the last pair is not '(), but the last element of the list
<cow_2001>ah, no need to keep record of which pairs you've already visited, which would have been O(n), but just a O(1) tortoise and hare
<cow_2001>(last-pair '(1 2 . 3)) returns (1 . 3), so it only checks for cyclics
<cow_2001>err (2 . 3)
<RhodiumToad>well, that is indeed the last pair
<cow_2001>;p
<RhodiumToad>most functions that make sense on improper lists do in fact accept them
<RhodiumToad>looking at the code, last-pair does the hare/tortoise algorithm itself rather than calling list? first
<cow_2001>managed to segfault guile
<cow_2001> https://paste.debian.net/1277511/
<cow_2001>version 3.0.9
<cow_2001>oh right. '(1 2 3) is in the static (constant?) part of memory town
<cow_2001>(list 1 2 3) is changeable
<flatwhatson>yeah, exactly
<cow_2001>no, that's not it? i just went into guile and tried it and it didn't crash
<cow_2001>(define a '(1 2 3)) RET (set-cdr! a 8) RET a RET and we see $1 = (1 . 8)
<flatwhatson>sure, your paste example doesn't crash in the repl either
<flatwhatson>probably the difference is compilation
<cow_2001>trying the same in a file and it doesn't crash
<cow_2001>confusion will be my epitaph
<flatwhatson>modifying constant pairs is wrong, regardless of how reliably it crashes
<flatwhatson>also ideally it would fail instead of crashing
<cow_2001>okay! got it! (define a '(1)) (set-cdr! a 2) segfaults, but (define b '(1 2)) (set-cdr! b 3) does not
<cow_2001>what good did it make? no clue
<cow_2001>RhodiumToad: :D https://git.sr.ht/~kakafarm/sicp/tree/master/item/sicp/solutions/3_19.scm
<cow_2001>wait, i don't need that deepest null check
<cow_2001>okay, fixed
<dokma>Does this work for you fellas?
<dokma>(use-modules (web socket client))
<dokma>(make-websocket "ws:ws.vi-server.org")
<dokma>Or am I misunderstanding how it should be used?
<dokma>I've installed https://git.dthompson.us/guile-websocket.git
<dokma>Is there perhaps official support for websockets in Guile 3.0 ??