Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Why doesn't this typecheck?
- compress xs@(_:_:_) = (ifte <$> ((==) <$> head <*> head.tail) <$> ((compress.).(:) <$> head <*> tail.tail) <*> ((:) <$> head <*> compress.tail) ) xs
- compress xs@(_:_:_) = (ifte (((==) <$> head <*> head.tail) xs) (((compress.).(:) <$> head <*> tail.tail) xs) (((:) <$> head <*> compress.tail) xs))
- Couldn't match expected type `[a]' with actual type `[a] -> [a]'
- In the expression:
- (ifte <$> ((==) <$> head <*> head . tail)
- <$>
- ((compress .) . (:) <$> head <*> tail . tail)
- <*>
- ((:) <$> head <*> compress . tail))
- $ xs
- In an equation for `compress':
- compress xs@(_ : _ : _)
- = (ifte <$> ((==) <$> head <*> head . tail)
- <$>
- ((compress .) . (:) <$> head <*> tail . tail)
- <*>
- ((:) <$> head <*> compress . tail))
- $ xs
- compress::Eq a => [a]->[a]
- compress [] = []
- compress (x:[]) = (x:[])
- compress (x:y:xs) = ifte ((==) x y) (compress (x:xs)) (x:(compress (y:xs)))
- compress =
- ifte <$> ((==) <$> head <*> head.tail)
- <$> ((compress.).(:) <$> head <*> tail.tail)
- <*> ((:) <$> head <*> compress.tail)
- compress =
- ifte <$> ((==) <$> head <*> head.tail)
- <*> ((compress.).(:) <$> head <*> tail.tail)
- <*> ((:) <$> head <*> compress.tail)
- compress (x:r@(y:_)) = ifte (x==y) id (x:) $ compress r
- compress = map fst . filter (uncurry (/=)) . (zip <$> id <*> tail)
- {-# LANGUAGE NoMonomorphismRestriction #-}
- import Control.Applicative
- u = ((==) <$> head <*> head.tail)
- v = ((compress.).(:) <$> head <*> tail.tail)
- w = ((:) <$> head <*> compress.tail)
- ifte = ( x y z -> if x then y else z)
- --compress xs@(_:_:_) = (ifte <$> u <$> v <*> w) xs
- compress xs@(_:_:_) = (ifte (u xs) (v xs) (w xs))
- --compress xs@(_:_:_) = (ifte <$> u <*> v <*> w) xs
- import Control.Applicative
- ifte :: Bool -> a -> a -> a
- ifte b t f = if b then t else f
- compress :: Eq a => [a] -> [a]
- -- compress = ifte <$> cond <$> t <*> f
- -- We will leave compress undefined so we can load this into ghci.
- -- After some trial and error it is clear that this is the part
- -- that doesn't type check
- compress = undefined
- cond :: Eq a => [a] -> Bool
- cond = (==) <$> head <*> head . tail
- t :: Eq a => [a] -> [a]
- t = (compress .) . (:) <$> head <*> tail . tail
- f :: Eq a => [a] -> [a]
- f = (:) <$> head <*> compress . tail
- f <$> a <*> b <*> c <*> d <*> ...
- ghci> :t ifte <$> cond <$> t <*> f
- ... Eq a => [a] -> [a] -> [a]
- ghci> :t compress
- ... Eq a => [a] -> [a]
- ifte :: Bool -> [a] -> [a] -> [a]
- cond :: [a] -> Bool
- t :: [a] -> [a]
- f :: [a] -> [a]
- -- desired result
- :: [a] -> [a]
- arg1 :: (a -> b -> c -> d)
- arg2 :: f a
- arg3 :: f b
- arg4 :: f c
- res :: f d
- -- for our case,
- -- f = ([a] ->)
- -- a = Bool
- -- b = [a]
- -- c = [a]
- -- d = [a]
- liftA3 f a b c = f <$> a <*> b <*> c
Add Comment
Please, Sign In to add comment