2017-05-30 101 views
3

我试图理解Haskell中表达式const (++)的类型。我知道const(++)的个别类型,我知道您可以省略参数以返回部分应用的功能。Haskell中的常量类型(++)

如果我输入:t const (++),我会得到const (++) :: b -> [a] -> [a] -> [a]。我认为(++)需要两个列表(我知道但是Haskell中的所有函数都是curried函数,实际上只带一个参数)并返回一个列表。这个列表是const函数的第一个参数,它等待另外一个参数。所以,我认为这种类型将是const (++) :: [a] -> [a] -> b -> [a].

但是,例如:const (++) 1 "hello" "you"返回"helloyou"。根据const的定义const x _ = x,如何从const操作返回的东西不是第一个参数?在我的思考过程中,我的位置不正确?

回答

6

怎么说,从常量操作返回的事情已经不是第一个参数

它。 const的第一个参数是(++)。所以const (++) 1的操作结果实际上是(++),这是第一个参数。所以const (++) 1(++),意思是const (++) 1 "hello" "you"(++) "hello" "you",那就是"helloyou"

+2

*和*,'利弊t(++)1“hello”“you”'与'(((const(++))1)“hello”相同“)”you“与'((const(++)1 )“hello”)“you”与“((++)”hello)相同“)”you“是相同的就是'(++)”hello“”you“是”hello“+ +“你”。 –

2
(++) :: [a] -> [a] -> [a] 
     ≡ ([a] -> [a] -> [a]) 

const :: c -> b -> c 

现在,在const (++),你想给(++)作为参数const。到const的参数的类型应该是c,让您统一,随着(++)

 c ~ ([a] -> [a] -> [a]) 

类型,并获得专业版

const :: ([a] -> [a] -> [a]) -> b -> ([a] -> [a] -> [a]) 

如果看起来混乱,考虑const 1的简单的情况。如果1有类型Int,那么我们就必须有c ~ Int在这种情况下,

const :: Int -> b -> Int 

由于功能箭头是右结合的(++) -specialisation也可以,如果你确实写

const :: ([a] -> [a] -> [a]) -> b -> [a] -> [a] -> [a] 

现在应用到(++),你饱和函数的论点,并最终与

const (++) :: b -> [a] -> [a] -> [a]