mark.warren
2019-1-22 10:05:02

Hello again, I was looking through some old blog entries in the racket blog and it mentioned the number of Rosetta Code tasks that had been done. I looked at the tasks not yet done and decided to the Hunt the Wumpus task. If possible could someone review my code and tell me if it is ‘rackety’ enough to put on Rosetta Code?


mark.warren
2019-1-22 10:05:46

soegaard2
2019-1-22 10:23:45

Classic! They ought to add a history section.



mark.warren
2019-1-22 10:25:27

Hehe :grin:


eero.linna
2019-1-22 12:37:57

@eero.linna has joined the channel


githree
2019-1-22 15:06:27

Is there any existing function/form similar to case but which doesn’t stop at first match but continue for all clauses? Say I have a function like this: (define (build-alist type) (append (case type ['apple '((local . "true"))] [(banana apple) '((fruit . "true") (weight . "kg"))])))

but the results I want is: > (build-alist 'banana) '((fruit . "true") (weight . "kg"))

but: > (build-alist 'apple) '((local . "true") (fruit . "true") (weight . "kg"))


mark.warren
2019-1-22 15:50:26

I think you want cond


mark.warren
2019-1-22 15:53:26

But that will not drop through, you could have a bunch of when clauses I suppose.


githree
2019-1-22 15:56:42

cond also stops at first true value


githree
2019-1-22 15:57:40

as to when the problem is that it returns #void if not matched which kinda sucks for my application


mark.warren
2019-1-22 15:59:55

Yeah I can see that, you could try breaking it further down (define (is-local type) (if (eq? type 'apple) '((local . "true")) '()))


mark.warren
2019-1-22 16:01:46

etc.


githree
2019-1-22 16:03:10

I’m afraid it won’t help much - this was just a minimal example - in my real one type can have about 20 different values - so I was looking for something much easier to maintain as my current implementation is huge. :scream:


mark.warren
2019-1-22 16:03:42

Ah sorry, I’m not going to be much help then. Only a beginner.


githree
2019-1-22 16:04:00

thanks anyway!


greg
2019-1-22 17:12:01

@mark.warren You could write a macro to do a cond-like form with drop-through, kind of like a C switch statement. I can find a couple links if you’re interested.


greg
2019-1-22 17:12:19

But I feel like the code here is trying to deal with three separate tests/concepts.


greg
2019-1-22 17:12:28

Personally I’d express each one, then assemble the results.


greg
2019-1-22 17:13:09

You could write it all sorts of ways. This is kind of verbose, but: (define (build-alist type) (define local? (case type [(apple) '((local . "true"))] [else '()])) ;or raise error? (define fruit? (case type [(apple banana) '((fruit . "true"))] [else '()])) ;or raise error? (define weight (case type [(apple banana) '((weight . "kg"))] [else '()])) ;or raise error? (append local? fruit? weight))


greg
2019-1-22 17:13:26

Which passes your desired tests, IIUC: (require rackunit) (check-equal? (build-alist 'banana) '((fruit . "true") (weight . "kg"))) (check-equal? (build-alist 'apple) '((local . "true") (fruit . "true") (weight . "kg")))


greg
2019-1-22 17:14:59

You could drag a slider between “very concise” and “very verbose”. Where you want it depends on your preference, team/audience if any, etc. etc.


greg
2019-1-22 17:17:10

If you want the fall-through form, a couple links:




greg
2019-1-22 17:17:54

But again, I doubt I’d use that here (or ever, really :smile:)


greg
2019-1-22 17:18:28

Oh derp I mean to “at” @githree; sorry


githree
2019-1-22 17:19:12

It does seem more maintainable than my current code but there are 17 pairs and 20 value types (it’s for some undocumented rest api)


githree
2019-1-22 17:20:02

with fall-through it should go down to 7 cases combinations


githree
2019-1-22 17:20:15

so thank you @greg! That should help


greg
2019-1-22 17:21:38

Ah. If it’s WORM (write-once, read-many) kind of code, that is, if the API isn’t likely to change and you won’t need to update it much, I understand preferring fall-through and DRY-ing it.


githree
2019-1-22 17:22:07

yep :slightly_smiling_face:


githree
2019-1-22 17:23:03

I didn’t know what to look for (drop-through/fall-through) but it makes sense now!


githree
2019-1-22 17:29:46

BTW, judging by how convoluted the API is I’m not surprised it has not been documented…


greg
2019-1-22 18:21:55

loves Great Lebowski APIs


greg
2019-1-22 18:48:08

People here are too polite. Someone is supposed to correct me: “No, it’s the Big Lebowski.” Setting me up to say, “Yeah, well, that’s just your opinion, man.”


greg
2019-1-22 18:48:21

sighs