Created
October 6, 2011 09:08
-
-
Save missingfaktor/1266912 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- A simple definition without using any fancy functions. | |
haskell>let ifMaybe pred value = if pred value then Just value else Nothing | |
haskell>:t ifMaybe | |
ifMaybe :: (a -> Bool) -> a -> Maybe a | |
haskell>ifMaybe (const True) 2 | |
Just 2 | |
haskell>ifMaybe (const False) 2 | |
Nothing | |
-- Using functions from standard library. | |
haskell>let ifMaybe pred value = mfilter pred $ Just value | |
haskell>:t ifMaybe | |
ifMaybe :: (a -> Bool) -> a -> Maybe a | |
haskell>ifMaybe (const True) 2 | |
Just 2 | |
haskell>ifMaybe (const False) 2 | |
Nothing | |
-- A point-free version that uses section of (.). | |
haskell>let ifMaybe = (. Just) . mfilter | |
haskell>:t ifMaybe | |
ifMaybe :: (a -> Bool) -> a -> Maybe a | |
haskell>ifMaybe (const True) 2 | |
Just 2 | |
haskell>ifMaybe (const False) 2 | |
Nothing | |
-- Same thing written with (<.|) combinator. | |
haskell>let ifMaybe = mfilter <.| Just | |
haskell>:t ifMaybe | |
ifMaybe :: (a -> Bool) -> a -> Maybe a | |
haskell>ifMaybe (const True) 2 | |
Just 2 | |
haskell>ifMaybe (const False) 2 | |
Nothing | |
haskell> |
@missingfaktor: you can write them like this, but it's not really an improvement:
(|*>) :: (b -> c) -> (a -> b1 -> b) -> (a -> b1 -> c)
f |*> g = curry (f . uncurry g)
(<*|) :: (a -> b -> c) -> (b1 -> b) -> (a -> b1 -> c)
f <*| g = curry (uncurry f . second g)
On Oct/06/2011 17:30, Rahul Göma Phuloré wrote:
@igstan: No, they aren't. However this little exercise was useful for me, as I am currently trying to learn and internalize Haskell way of thinking. :-)
Yes, I know :). So, in the same spirit of learning, here's another form
that gave me troubles understanding it in some other context. Notice
that each definition takes four params.
``` haskell
(|*>) :: (b -> c) -> (a -> b1 -> b) -> (a -> b1 -> c)
(|*>) f g a b = f (g a b)
(<*|) :: (a -> b -> c) -> (d -> b) -> (a -> d -> c)
(<*|) f g a b = f a (g b)
```
##
Ionuț G. Stan | http://igstan.ro
With some help of @djinn
and @pl
commands of lambdabot...
Prelude Control.Arrow Control.Applicative> let regular = (. (,)) . (.)
Prelude Control.Arrow Control.Applicative> :t regular
regular :: ((a, b) -> c) -> a -> b -> c
Prelude Control.Arrow Control.Applicative> let f |.> g = regular $ f . uncurry g
Prelude Control.Arrow Control.Applicative> :t (|.>)
(|.>) :: (b1 -> c) -> (a -> b -> b1) -> a -> b -> c
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@igstan: Here is how far I managed to reach. :-)
I changed the names of the functions because these require their arguments tupled. Can you help me make them take arguments in regular curried style?