[Haskell-cafe] Re: Suggestions for improvement
raghu at mri.ernet.in
Tue Oct 5 00:47:15 EDT 2010
At 2010-10-03T22:45:30+02:00, Dominique Devriese wrote:
> You need a function like the following for that:
> comma :: (a -> b) -> (a -> c) -> a -> (b,c)
> comma f g x = (f x, g x)
> Then you could say:
> blowup = (uncurry (++)) . comma (blowup . allButLast) lastToTheLength
> Ignore this if you haven't read about Applicative or type classes yet,
> but using the Applicative instance for arrow types (->) a, you can
> also write
> comma = liftA2 (,)
> blowup = (uncurry (++)) . liftA2 (,) (blowup . allButLast) lastToTheLength
I tried both of them, but they don't seem to work:
-- Pointfree blowup.
blowup1 :: String -> String
blowup1 = (uncurry (++)) . comma1 (blowup1 . allButLast) lastToTheLength
comma1 :: (a -> b) -> (a -> c) -> a -> (b,c)
comma1 f g x = (f x, g x)
blowup2 :: String -> String
blowup2 = (uncurry (++)) . comma2 (blowup2 . allButLast) lastToTheLength
-- Imported Control.Applicative earlier.
comma2 :: (a -> b) -> (a -> c) -> a -> (b,c)
comma2 = liftA2 (,)
GHCi, version 6.12.3: http://www.haskell.org/ghc/ :? for help
Loading package ghc-prim ... linking ... done.
Loading package integer-gmp ... linking ... done.
Loading package base ... linking ... done.
Loading package ffi-1.0 ... linking ... done.
Prelude> :l Chapter01.hs
[1 of 1] Compiling Chapter01 ( Chapter01.hs, interpreted )
Ok, modules loaded: Chapter01.
*Chapter01> comma1 allButLast lastToTheLength "abcd"
*Chapter01> comma2 allButLast lastToTheLength "abcd"
*Chapter01> blowup1 "abcd"
*Chapter01> blowup2 "abcd"
It looks like both the above versions of blowup go into some infinite
recursion, and have to be interrupted.
N. Raghavendra <raghu at mri.ernet.in> | http://www.retrotexts.net/
Harish-Chandra Research Institute | http://www.mri.ernet.in/
See message headers for contact and OpenPGP information.
More information about the Haskell-Cafe