Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

Testing for congruence in Haskell

Name: Anonymous 2008-12-06 14:08

Hello /prog/ Haskell programers users. Is it possible to test for the congruence of expression in Haskell? I know i can say
this a = if (a 12) == 12 then (a 12) else (a 13)
and expect this (\ x -> x + 1) to eval to 14. But is there anyway to test for the equivalence of two (or more) expressons without actually applying either of them? In other words, can haskell do anything with thunks, aide from than apply them?

(\ x -> x) == (\ x -> x) is not an expression. Is there a corrext syntax for this type of thing?
Thanks,

Name: Anonymous 2008-12-06 18:55

>>13
What you probably want there is fmap succ (Just 1), though Just 1 >>= return . succ would also work. The function argument to >>= must return a value in the monad, but fmap's does not. Thus, to run a pure function on the value(s) in the monad, we can compose it with return :: b -> m b. Compare:
(>>=) :: Monad m => m a -> (a -> m b) -> m b
fmap :: Functor f => (a -> b) -> f a -> f b


In a monad, it is also possible to use liftM. liftM is different because it requires a monad, not a functor, even though it does the same thing, and also there are versions for other argument counts: liftM2, liftM3, etc. We could also use liftM2 (+) (Just 1) (Just 1).

For the second question we could also use filter (`notElem` [".", ".."]) `fmap` getDirectoryContents "/".

To get the value out of the Maybe monad, use fromJust or maybe.

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List