Talk:Haskell
Factorial example[edit source]
Regarding Revert Anon; let us charitably assume he doesn't realize this is a humor site on 12:54, June 8, 2012 by user:SPIKE
I don't get why this is not funny:
main = putStrLn "Input number" >> fmap (iterate ((<*>) ((<*>) (pure (<*>)) pure)) (pure unsafeCoerce) !!) readLn >>= (<*>) (pure (print :: Int -> IO ())) ((<*> (pure ((<*>) ((<*>) unsafeCoerce (pure (succ :: Int -> Int))) (pure 0))) ((<*>) ((<*>) ((<*>) (pure (<*>)) pure) (pure ((<*>) unsafeCoerce unsafeCoerce))) ((<*>) ((<*>) (pure (<*>)) pure) (pure ((<*>) unsafeCoerce unsafeCoerce))) ((<*>) (pure ((<*>) ((<*>) ((<*>) ((<*>) unsafeCoerce (pure ((pure :: a -> b -> a) ((pure :: a -> b -> a) unsafeCoerce)))) (pure (pure :: a -> b -> a))) (pure unsafeCoerce)))) ((<*>) (pure ((<*>) ((<*>) (pure ((<*>) :: (a -> b -> c) -> (a -> b) -> a -> c)) pure))) ((<*>) ((<*>) (pure (<*>)) pure) (pure ((<*>) ((<*>) (pure (<*>)) ((<*>) (pure ((<*>) (pure (<*>)))) ((<*> ((<*>) (pure (<*>)) ((<*>) (pure ((<*>) (pure (<*>)))) ((<*>) (pure ((<*>) (pure pure))) ((<*>) ((<*>) (pure (<*>)) pure) (pure ((<*>) (pure ((<*>) (pure ((<*>) unsafeCoerce)))) ((<*>) (pure ((<*>) (pure pure))) ((<*>) (pure ((<*>) unsafeCoerce)) pure)))))))) (pure (pure (pure :: a -> b -> a)))))) (pure (pure (pure unsafeCoerce)))))))))) >> main
When I first saw this example I found it more amazing and more funny than most examples at for example The International Obfuscated C code contest. It's amazing because it actually works and it is funny because it probably only works because of the type system hacks (due to the type annotations) and all unsafeCoerces. Using unsafeCoerce goes against all conventions which are usually employed when programming in Haskell. This example also seems to go well with the theme on the haskell page that interactive programs (i.e. programs that read from stdin) are hard to express using haskell (see for example System.IO.Unsafe.Really.IMeanIt.reallyReallyAbsurdlyUnsafePerformIOShameOnYou and ...had to be augmented by the best parts of Perl to actually do anything useful).
// David
Heh, the Zygohistomorphic Prepromorphism section is outdated and wrong. The real code is
zygoHistoPrepro :: (Unfoldable t, Foldable t) => (Base t b -> b) -> (forall c. Base t c -> Base t c) -> (Base t (EnvT b (Stream (Base t)) a) -> a) -> t -> a zygoHistoPrepro f g t = gprepro (distZygoT f distHisto) g t
taken from http://www.haskell.org/haskellwiki/Zygohistomorphic_prepromorphisms
// Boris