Building ? using kleene closure {not haskell specific}
Hello gentle Haskell folks, I happened to read "Beautiful code"'s chapter 1 today and found Brian Kerninghan's regex implementation. In it he only shows the * meta character. I can easily understand how + can be built but am having trouble with building ? (zero or one). I'd really appreciate it if some one could help me understand it. Regards, Kashyap
Hi, Perhaps I did not understand the question properly, but it looks very straight forward : oneOrNone x = fmap Just x <|> pure Nothing it will have a type of oneOrNone :: (Alternative f) => f a -> f (Maybe a) In fact, there is a function called "optional" in Control.Applicative[1] which does exactly that. Is this what you are looking for? thanks, Hemanth K [1] http://haskell.org/ghc/docs/6.12.2/html/libraries/base-4.2.0.1/Control-Appli... On Fri, Aug 12, 2011 at 1:31 PM, C K Kashyap <ckkashyap@gmail.com> wrote:
Hello gentle Haskell folks,
I happened to read "Beautiful code"'s chapter 1 today and found Brian Kerninghan's regex implementation. In it he only shows the * meta character. I can easily understand how + can be built but am having trouble with building ? (zero or one). I'd really appreciate it if some one could help me understand it.
Regards, Kashyap
_______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
I can easily understand how + can be built but am having trouble with building ? (zero or one).
If there is a regular expression e for the empty word, one can define ? as a? = e | a If there is a regular expression o that never matches one can define e as e = o* If there are character classes one can define o as o = [] Apart from that, I have no idea.. Sebastian
On Fri, Aug 12, 2011 at 4:34 PM, Sebastian Fischer <fischer@nii.ac.jp>wrote:
I can easily understand how + can be built but am having trouble with building ? (zero or one).
If there is a regular expression e for the empty word, one can define ? as
a? = e | a
If there is a regular expression o that never matches one can define e as
e = o*
If there are character classes one can define o as
o = []
Apart from that, I have no idea..
Sebastian
Thanks Sebastian ... this is what I was asking for. I'll try and digest it. Regards, Kashyap
participants (3)
-
C K Kashyap -
Sai Hemanth K -
Sebastian Fischer