Guest User

Untitled

a guest
Aug 18th, 2018
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.79 KB | None | 0 0
  1. Why doesn't this typecheck?
  2. compress xs@(_:_:_) = (ifte <$> ((==) <$> head <*> head.tail) <$> ((compress.).(:) <$> head <*> tail.tail) <*> ((:) <$> head <*> compress.tail) ) xs
  3.  
  4. compress xs@(_:_:_) = (ifte (((==) <$> head <*> head.tail) xs) (((compress.).(:) <$> head <*> tail.tail) xs) (((:) <$> head <*> compress.tail) xs))
  5.  
  6. Couldn't match expected type `[a]' with actual type `[a] -> [a]'
  7. In the expression:
  8. (ifte <$> ((==) <$> head <*> head . tail)
  9. <$>
  10. ((compress .) . (:) <$> head <*> tail . tail)
  11. <*>
  12. ((:) <$> head <*> compress . tail))
  13. $ xs
  14. In an equation for `compress':
  15. compress xs@(_ : _ : _)
  16. = (ifte <$> ((==) <$> head <*> head . tail)
  17. <$>
  18. ((compress .) . (:) <$> head <*> tail . tail)
  19. <*>
  20. ((:) <$> head <*> compress . tail))
  21. $ xs
  22.  
  23. compress::Eq a => [a]->[a]
  24. compress [] = []
  25. compress (x:[]) = (x:[])
  26. compress (x:y:xs) = ifte ((==) x y) (compress (x:xs)) (x:(compress (y:xs)))
  27.  
  28. compress =
  29. ifte <$> ((==) <$> head <*> head.tail)
  30. <$> ((compress.).(:) <$> head <*> tail.tail)
  31. <*> ((:) <$> head <*> compress.tail)
  32.  
  33. compress =
  34. ifte <$> ((==) <$> head <*> head.tail)
  35. <*> ((compress.).(:) <$> head <*> tail.tail)
  36. <*> ((:) <$> head <*> compress.tail)
  37.  
  38. compress (x:r@(y:_)) = ifte (x==y) id (x:) $ compress r
  39.  
  40. compress = map fst . filter (uncurry (/=)) . (zip <$> id <*> tail)
  41.  
  42. {-# LANGUAGE NoMonomorphismRestriction #-}
  43. import Control.Applicative
  44.  
  45. u = ((==) <$> head <*> head.tail)
  46. v = ((compress.).(:) <$> head <*> tail.tail)
  47. w = ((:) <$> head <*> compress.tail)
  48.  
  49. ifte = ( x y z -> if x then y else z)
  50.  
  51. --compress xs@(_:_:_) = (ifte <$> u <$> v <*> w) xs
  52. compress xs@(_:_:_) = (ifte (u xs) (v xs) (w xs))
  53.  
  54. --compress xs@(_:_:_) = (ifte <$> u <*> v <*> w) xs
  55.  
  56. import Control.Applicative
  57.  
  58. ifte :: Bool -> a -> a -> a
  59. ifte b t f = if b then t else f
  60.  
  61. compress :: Eq a => [a] -> [a]
  62. -- compress = ifte <$> cond <$> t <*> f
  63. -- We will leave compress undefined so we can load this into ghci.
  64. -- After some trial and error it is clear that this is the part
  65. -- that doesn't type check
  66. compress = undefined
  67.  
  68. cond :: Eq a => [a] -> Bool
  69. cond = (==) <$> head <*> head . tail
  70.  
  71. t :: Eq a => [a] -> [a]
  72. t = (compress .) . (:) <$> head <*> tail . tail
  73.  
  74. f :: Eq a => [a] -> [a]
  75. f = (:) <$> head <*> compress . tail
  76.  
  77. f <$> a <*> b <*> c <*> d <*> ...
  78.  
  79. ghci> :t ifte <$> cond <$> t <*> f
  80. ... Eq a => [a] -> [a] -> [a]
  81.  
  82. ghci> :t compress
  83. ... Eq a => [a] -> [a]
  84.  
  85. ifte :: Bool -> [a] -> [a] -> [a]
  86. cond :: [a] -> Bool
  87. t :: [a] -> [a]
  88. f :: [a] -> [a]
  89.  
  90. -- desired result
  91. :: [a] -> [a]
  92.  
  93. arg1 :: (a -> b -> c -> d)
  94. arg2 :: f a
  95. arg3 :: f b
  96. arg4 :: f c
  97. res :: f d
  98.  
  99. -- for our case,
  100. -- f = ([a] ->)
  101. -- a = Bool
  102. -- b = [a]
  103. -- c = [a]
  104. -- d = [a]
  105.  
  106. liftA3 f a b c = f <$> a <*> b <*> c
Add Comment
Please, Sign In to add comment