sydney.lambda
2019-9-15 08:05:12

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 :)


philip.mcgrath
2019-9-15 12:02:52

@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))


sydney.lambda
2019-9-15 22:55:43

@philip.mcgrath That’s great! Contracts, for/ forms, and intermediate allocations are all things I could do with improving my knowledge of. Thank you :)


aymano.osman
2019-9-16 00:01:37

@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?


samdphillips
2019-9-16 02:58:32

Never heard of it :slightly_smiling_face: https://github.com/samdphillips/euler


samdphillips
2019-9-16 03:01:44

@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.


samdphillips
2019-9-16 03:06:14

Also it doesn’t need to do “division”, just addition.