Oh boy, I bet you guys have never heard of project Euler before ;) This is my solution to the first problem, which is to “Find the sum of all the multiples of 3 or 5 below 1000.”: (define (multiple-of? . ns)
(define (multiple? n)
(compose zero?
(curryr remainder n)))
(apply disjoin (map multiple? ns)))
(foldl + 0 (filter (multiple-of? 3 5) (range 1000)))
I just wanted to see if anyone thinks my approach could be simplified whilst still using the same sort of method? I like the approach of building up a combined predicate function, but I reckon there’s likely a simpler way to do it that I’m overlooking. Thanks :)
@sydney.lambda Here’s a similar implementation that’s a bit simpler, at least to my taste: #lang racket
(define ((multiple-of/c . ns) x)
;; the "/c" suffix suggests a function that returns a predicate
(for/or ([n (in-list ns)])
(zero? (remainder x n))))
(let ([use? (multiple-of/c 3 5)])
;; filter and range each build intermediate lists.
;; This version avoids any intermediate allocation.
(for/sum ([x (in-range 1000)]
#:when (use? x))
x))
@philip.mcgrath That’s great! Contracts, for/ forms, and intermediate allocations are all things I could do with improving my knowledge of. Thank you :)
@popa.bogdanp I noticed that you provide a module to support mime type detection koyo/mime
which provides a function I’ve found myself needing to construct myself (https://koyo.defn.io/mime/index.html#%28def._%28%28lib._koyo%2Fmime..rkt%29._path-~3emime-type%29%29). Do you think it would be a good idea create a shared package we could all use?
Never heard of it :slightly_smiling_face: https://github.com/samdphillips/euler
@sydney.lambda Here is the solution I came up with https://github.com/samdphillips/euler/blob/master/0001.rkt
I had an epiphany a few weeks ago revisiting those problems and I wrote solution2 which steps through the space by 3 and 5. So it just iterates on the answers.
Also it doesn’t need to do “division”, just addition.