3 ms·
It's weird, because they've always just clicked for me. I mean, you have text and you compare it to a pattern. The regex is just a crazy shorthand for the pat
by Natsu 16y ago
It's weird, because they've always just clicked for me. I mean, you have text and you compare it to a pattern. The regex is just a crazy shorthand for the pattern you're looking for. What pattern should you match? Well, first write down examples of what you want to match and see what they have in common.
I think it's easier to get if you match a pattern to some text by hand, taking a simple pattern like ^a+bc$ and trying to see if it matches:
Here are a few test cases:
"aaaaaaaaabbbc" ?
1) ^a+ means it starts with 1 or more 'a's, so those eat all my 'a's and I'm matching bbbcc against 'bc' now
2) b* means 0 or more 'b's; those eat up everything but the c at the end.
3) c$ matches exactly one c at the end, so yeah, we have a match. If there had been one more c (or anything else), the match would fail because it wouldn't hit the $ (which is end of string).
aaac ?
1) ^a+ eats all the 'a's again.
2) b* ... what b? It's "zero or more" so yes, we can skip it.
3) c$ Yes, there's one c at the end. We have a match.
bc ?
1) We need one* or more 'a's at the start. That's the difference between + and . Match fails immediately.
acbbc ?
1) ^a+ matches the a.
2) b means it's optional, so we don't fail... yet. But we don't match anything, either.
3) c$ That c matches, but we're not at the end of the string yet. There's still "bbc" left to match and we're out of pattern. Fail.
You can do something similar with the tools other people have linked in comments. This was just to give an example pattern and a few test cases to get you started.
It's weird, though. Even though this clicks for me, I don't feel like I get monads at all. Yeah, I've read the 3 monad laws (great... so something being a monad means that I can monad it, un-monad it and do something vaguely similar to composition on monads) and I can do some very simple Lisp which uses built-in stuff they say is built from monads (like printing). But I don't know how they work internally at all, or how they're fundamentally different from all the other Lisp statements ("causes side affects" isn't terribly specific... why is printing to the screen a side effect? Is setq a monad?).
In other words, there's probably some topic for every programmer that they really have to struggle with. As easily as regexes come to me, I struggle to understand other stuff.
I've been at it for quite a while, incidentally. But I'm not going to give up any time soon. So keep working on it.
- lincolnq 16y agoI wrote some stuff about monads on HN a while ago that some people found helpful: http://news.ycombinator.com/item?id=247446 http://news.ycombinator.com/item?id=247446
- Natsu 16y agoThanks. I just read it. It does help, but I think there's still a lot I don't know. I mean, 'bind' and 'return' still seem like the names are backwards to me. Somehow, I feel like 'return' should give me a value, rather than shoving a value into a monad. But that's probably because I'm used to C and its relatives. Perhaps I simply need to read up more on FP itself. I have a huge bookshelf full of half-read books (and yes, plenty of fully-read books) but nowhere near enough free time...
- lincolnq 16y agoOh. Yeah. The names are poorly chosen, but we're stuck with them. :(
- Natsu 16y agoHN ate some stars in the above post and I didn't notice until now. Sorry about that. Look for the italics to see where the stars should be.