为什么不这个类型检查呢?

aaa*_*aaa 0 debugging haskell types typechecking pointfree

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))

,没有.

注意:ifte = (\ x y z -> if x then y else z),<$><*>Control.Applicative.

编辑:错误是:

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
Run Code Online (Sandbox Code Playgroud)

我在尝试为Ninety-Nine Haskell问题的问题8编写一个无点解决方案时遇到了这个问题.我试图通过修改我写的有点解决方案来做到这一点

compress::Eq a => [a]->[a]
compress [] = []
compress (x:[]) = (x:[])
compress (x:y:xs) = ifte ((==) x y) (compress (x:xs)) (x:(compress (y:xs)))
Run Code Online (Sandbox Code Playgroud)

Bra*_*don 6

首先,缩进.其次,考虑使用一些变量.

即使有更明智的格式,你也可以看到它

compress =
  ifte <$> ((==) <$> head <*> head.tail)
       <$> ((compress.).(:) <$> head <*> tail.tail)
       <*> ((:) <$> head <*> compress.tail)
Run Code Online (Sandbox Code Playgroud)

什么时候应该

compress =
  ifte <$> ((==) <$> head <*> head.tail)
       <*> ((compress.).(:) <$> head <*> tail.tail)
       <*> ((:) <$> head <*> compress.tail)
Run Code Online (Sandbox Code Playgroud)

第三,即使你必须高深莫测,怎么样

compress (x:r@(y:_)) = ifte (x==y) id (x:) $ compress r
Run Code Online (Sandbox Code Playgroud)

或者,免费

compress = map fst . filter (uncurry (/=)) . (zip <$> id <*> tail)
Run Code Online (Sandbox Code Playgroud)

  • 甚至`压缩=地图头.group` (3认同)

sdc*_*vvc 5

这是以更易读的方式编写的代码

{-# 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))
Run Code Online (Sandbox Code Playgroud)

我希望你现在看到错误 - 正确的版本是

--compress xs@(_:_:_) = (ifte <$> u <*> v <*> w) xs
Run Code Online (Sandbox Code Playgroud)