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?
Classic! They ought to add a history section.
Hehe :grin:
@eero.linna has joined the channel
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"))
I think you want cond
But that will not drop through, you could have a bunch of when
clauses I suppose.
cond
also stops at first true value
as to when
the problem is that it returns #void if not matched which kinda sucks for my application
Yeah I can see that, you could try breaking it further down (define (is-local type)
(if (eq? type 'apple)
'((local . "true"))
'()))
etc.
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:
Ah sorry, I’m not going to be much help then. Only a beginner.
thanks anyway!
@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.
But I feel like the code here is trying to deal with three separate tests/concepts.
Personally I’d express each one, then assemble the results.
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))
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")))
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.
If you want the fall-through form, a couple links:
But again, I doubt I’d use that here (or ever, really :smile:)
Oh derp I mean to “at” @githree; sorry
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)
with fall-through it should go down to 7 cases combinations
so thank you @greg! That should help
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.
yep :slightly_smiling_face:
I didn’t know what to look for (drop-through/fall-through) but it makes sense now!
BTW, judging by how convoluted the API is I’m not surprised it has not been documented…
loves Great Lebowski APIs
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.”
sighs