IRC channel logs

2020-12-30.log

back to list of logs

<rlb>Anyone know what the FUNC_NAME was meant to be for scm_i_has_source_properties in srcprop.c? i.e. it's currently "%set-source-properties" but that looks like a typo (and is a duplicate with the function below it). I'd guess maybe it was supposed to be "%has-source-properties?".
***apteryx is now known as Guest13969
***apteryx_ is now known as apteryx
<tohoyn>sneek: botsnack
<sneek>:)
<fnstudio>hi! (map + list-a list-b) works with lists, but the same approach fails for pairs (as far as i can tell)
<fnstudio>eg this returns an error: (map + (cons 0 0) (cons 1 1))
<fnstudio>as it says it expects lists as input
<fnstudio>not pairs
<fnstudio>of course i can be a tad more verbose and do things manually here, eg (cons (+ (car cons-a) (car cons-b)) ...)
<fnstudio>but i was wondering if there's a cleaner way of doing this
<chrislck>cleaner way to do what?
<chrislck>you want the result to be (cons 1 1) ??
<fnstudio>yep chrislck that'd be ideal
<chrislck>your manual method is the best
<fnstudio>chrislck, oh great, thanks
<fnstudio>it did feel pretty simple and easy to read, but good to have confirmation
<tohoyn>sneek: botsnack
<sneek>:)
<tohoyn> fnstudio: (define (pair-map fn p1 p2) (cons (fn (car p1) (car p2)) (fn (cdr p1) (cdr p2))))
<fnstudio>tohoyn: oh that looks even prettier and more sleek - although in my case i need it just once, so it may not be worth the extra abstraction
<fnstudio>but thanks for mentioning that!
<rekado>here’s another implementation for an arbitrary number of pairs:
<rekado>(define (pmap f . pairs) (match pairs (((first second) ...) (cons (apply f first) (apply f second)))))
<chrislck>(let lp ((pairs pairs) (kars 0) (kdrs 0))
<fnstudio>rekado: uh, that's very clever, thanks - it's now saved in my useful snippets toolbox
<chrislck> (match pairs
<chrislck> (() (cons kars kdrs))
<chrislck> (((kar . kdr) . rest) (lp rest (+ kars kar) (+ kdrs kdr)))))
<fnstudio>is there a "best-practice" way to compute min and max of a list in one go, without having to scroll through it twice?
<fnstudio>i think i'd know how to implement it with a simple procedure
<fnstudio>but i'm wondering if there's a standard way of doing this
<fnstudio>what i mean is to get (apply min lst) and (apply max lst) in one go
<Zelphir>Probably a list eater, which carries 2 extra values, one for the max and one for the min, as arguments to the recursive call, plus of course the list or rest of the list .you still need to go through
<Zelphir>Perhaps not using apply min or apply max.
<Zelphir>You might need to go through the list in a recursive function, keeping track of min and max.
<fnstudio>Zelphir: that makes sense, thanks, very helpful
<Zelphir>But don't take this as law, I am just guessing :D
<fnstudio>Zelphir: yes i also thought of a "manual"/from-scratch approach based on a list-eater
<Zelphir>Perhaps one can abstract from that and make a more general, high-order thing out of it.
<fnstudio>yeah, that's also a good point, thanks
<Zelphir>To which one then gives min and max as procedures and it will accumulate for each given procedure or something.
<fnstudio>i suppose my concern here is a combination of computational complexity (to try and avoid extra loops) and code readability (to keep the code as "conventional" as possible, therefore easier to understand)
<Zelphir>You could use apply with a procedure, which tracks both, I think, and accumulates both.
<Zelphir>You could use fold with a procedure, which tracks both, I think, and accumulates both.
<Zelphir>*corrected.
<fnstudio>Zelphir: ah fold... right, let me have a look - thanks
<RhodiumToad>(fold (lambda (e m) (cons (min e (car m)) (max e (cdr m)))) (cons +inf.0 -inf.0) '(40 30 20 10))
<RhodiumToad>though that loses exactness
<RhodiumToad>(fold (lambda (e m) (if m (cons (min e (car m)) (max e (cdr m))) (cons e e))) #f '(40 30 20 10))
<fnstudio>RhodiumToad: v clever - and good exercise for me to digest it
<Zelphir>Does it lose exactness?
<Zelphir>I was just about to post the same code with different names for the bindings ^^
<Zelphir>(fold (λ (num accumulated)
<Zelphir> (cons (min num (car accumulated))
<Zelphir> (max num (cdr accumulated))))
<Zelphir> ;; initially +inf as min, so that any number will be lower
<Zelphir> ;; initially -inf as max, so that any number will be higher
<Zelphir> (cons +inf.0 -inf.0)
<Zelphir> '(1 -2 9 -3 4 0.2 -3247.2))
<RhodiumToad>it looks like e.g. (max exact inexact) returns an inexact result, which may or may not be desirable
<Zelphir>Ah I see! In my example it would give 9.0 instead of 9.
<Zelphir>OK, I guess one could add some additional logic to handle that and compare with < and >.
<RhodiumToad>the second version I gave is exact if all values in the list are exact and inexact otherwise
<RhodiumToad>also it might make sense to avoid consing if the list might be large
<Zelphir>Try the second version with input: '(40 30 20 10 0.9 -8.1 123 342.1 -434.2)
<Zelphir>Result: (-434.2 . 1000.0)
<Zelphir>I think you need to do something like the following:
<Zelphir>(fold (λ (num accumulated)
<Zelphir> (let ([prev-min (car accumulated)]
<Zelphir> [prev-max (cdr accumulated)])
<Zelphir> (cons (if (< num prev-min) num prev-min)
<Zelphir> (if (> num prev-max) num prev-max))))
<Zelphir> ;; initially +inf as min, so that any number will be lower
<Zelphir> ;; initially -inf as max, so that any number will be higher
<Zelphir> (cons +inf.0 -inf.0)
<Zelphir> '(1 -2 9 -3 4 0.2 -3247.2))
<Zelphir>The inexactness can stem from the input itself as well, as from the +inf.0 and -inf.0.
<RhodiumToad>I did just say that :-)
<RhodiumToad>"the second version I gave is exact if all values in the list are exact and inexact otherwise"
<Zelphir>Oh, sorry, overlooked that^^'
<Zelphir>Was a bit distracted. :D
<Zelphir>So many possibilities and nuances already for this procedure!
<hugo>Hi! I just released my library for parsing shapefiles with Guile. Check it out! https://github.com/HugoNikanor/guile-shapefile
<rekado>hugo: it’s available in Guix now (with commit e47a5711f0)
<hugo>rekado: Nice! How do I view it...?