<Boarders>
I don't know how to link to a specific part of the documentation but it is where it has (define (len l)) ...
<Boarders>
What does a correct version of using match to de-structure a list look like?
<Boarders>
if that is something it is possible to do with match
<Boarders>
I can't figure out how to use it in any example apart from a pattern match on literal values
<greghendershott>
Boarders: There are many ways to use `list`, `list*`, `list-rest`, and `cons` as match patterns for lists, depending on what you want to do.
<Boarders>
could you give a concrete example of such a thing?
<greghendershott>
For example matching the first item and the rest (i.e. `car` and `cdr` or `first` and `rest`) could be any of these: `(cons this more)`. `(list* this more)`, `(list this more ...)`. All will bind `this` to the first item, and `more` to the rest.
<Boarders>
what do you do for the nil case?
<greghendershott>
Usually I just use `(list)` as the clause, before another clause that tries to destructure a non-empty list.
<greghendershott>
I mean I use `(list)` as the pattern in a first clause. Then another clause using whatever, let's say `(list* this more)`, that would fail if the list were empty.
<greghendershott>
Instead of `(list)` as the pattern for an empty list, you could use `'()` or `empty` or whatever.
<greghendershott>
I usually tend to use the same style as for the other clauses.
<Boarders>
hmm sorry for the thickheadedness but could you give an example of something akin to: (head lst (match lst ('() (error "ahh")) ((const hd tail) hd)))
<greghendershott>
Oh right right you also have quasiquote patterns for lists. Like `(,this . ,more)`.
<Boarders>
cons*
<Boarders>
I'm not very proficient in racket or scheme so I am probably missing something completely obvious
<greghendershott>
Yep, you almost have it there. Let me just fiddle for a second to make sure I have it right for you. :) ....
<Boarders>
err I also forgot to write define etc
<greghendershott>
Sorry the pasterack Google captcha slowed me down.
<ArthurStrong>
Probably dumb question. I want to use only one branch of IF. I have to add #f on other branch. But these #f's are dumping into output. What to do?
<Boarders>
btw @greghendershott: how should I think about that (list) pattern match?
<greghendershott>
It's really a matter of taste.
<technomancy>
ArthurStrong: maybe `when' would be better, if it's a side-effecting thing.
<ArthurStrong>
technomancy: thakns for the idea
<greghendershott>
The `(list)` pattern matches the same thing that a `(list)` expression evaluates to.
<ArthurStrong>
Also, how to suppress #<void>'s? I'm calling a "void" function from for/list. And these void's clutters output
<greghendershott>
The nice thing is that the match patterns mirror the syntax of how you would produce such a thing.
<greghendershott>
So it has a very "by example" feel.
<technomancy>
ArthurStrong: if that happens usually it means you are mixing side-effects and values
<technomancy>
usually a sign you should restructure your code
<ArthurStrong>
maybe...
<Boarders>
oh I see, that makes sense
<Boarders>
thank you for your help!
<greghendershott>
Boarders: You're welcome. `match` can feel strange at first, and the docs show so many kinds of patterns. But it's a really beautiful thing when you get the hang of it.
<Boarders>
I am coming from Haskell so I wanted to reach for pattern matches immediately, but as you say that document is a bit heavy :)
<ArthurStrong>
I want to call a "void" function n times. Doing this by for/list. And #<void>'s clutter output. What should I do?
<Boarders>
(I must also say that that linked paste-rack is a thing of beauty)
<technomancy>
ArthurStrong: `for/list' is used to return a list. if you're calling a function that returns void, you shouldn't be returning a meaningful value. `for' would be better.
<Boarders>
(as is your racket emacs mode :) )
<greghendershott>
Boarders: Ah, cool. One thing to be aware of is that Racket `match` does not do exhaustiveness checking for you. If you don't cover some case, you'll drop off the end of the `match` and get a runtime error. So that's not as spiffy as Haskell, but otherwise I think they're similar.
<ArthurStrong>
technomancy: oh, that solved everything. Thanks a lot
<technomancy>
ArthurStrong: cool. mixing side-effects and logic is a really common beginner mistake for folks new to FP but you get the hang of it, and Racket usually gives you hints when you're doing stuff like that
<ArthurStrong>
technomancy: by side-effects you mean I/O?
<technomancy>
ArthurStrong: that's one way to put it, yeah
<Boarders>
greghendershott: looks amazing :)
<technomancy>
ArthurStrong: impure vs pure is maybe the clearest way
<technomancy>
*clearest way to describe it
<greghendershott>
Boarders: Thanks I just need to hire technomancy as a naming consultant. racket-check-syntax-mode is long and bad.
<greghendershott>
technomancy is good at naming projects. :)
<ArthurStrong>
technomancy: thanks
Boarders has quit [Remote host closed the connection]
Boarders has joined #racket
<ArthurStrong>
BTW. What is better, let, let* or define? I'm stuck with the latter.
<ArthurStrong>
And I'm satisfied with defines inside function
<greghendershott>
The audience for that is people developing Racket, but, you might find some ideas you like.
ng0 has quit [Ping timeout: 240 seconds]
nikoala has quit [Ping timeout: 240 seconds]
<ArthurStrong>
Contracts are just confusing :( Can anyone point for some simple explanation for C/C++ programmer? I just want to assure that one function's argument is always a list of integers. As simple as this. And another arg is integer. Nothing else.
<ArthurStrong>
Are they borrowed from another PL?
badkins has joined #racket
<ArthurStrong>
Isn't it the same as Hoare triple?
badkins has quit [Ping timeout: 240 seconds]
badkins has joined #racket
<ArthurStrong>
Am I right contract is like superset and generalization of assert()'s?
<ArthurStrong>
Should I better start at (raise-argument-error)?
badkins has quit [Ping timeout: 260 seconds]
<greghendershott>
ArthurStrong: Sure, you can think of contracts like automatic asserts for the arguments and return values of a function -- including when a function is used as an argument to another function, like say when using `map`.