您好,我的一些有关类型的uni Haskell练习存在问题。我可以为(*),map等函数定义类型,但是当我必须定义以下类型时,就会出现问题:
f 7 (g 'a')
Run Code Online (Sandbox Code Playgroud)
我如何开始呢?我知道我们应该使用一些最通用的统一器,但是我在互联网上找不到任何有关它的信息吗?
此外,还有另一项任务是在考虑类型的可能类别的情况下找到最通用的类型。
- 折叠(++)
- 平方(fx)
- g代表功能:
- g [] =无
- g [x] =只是x
- g(x:y:l)=如果x <y则x否则x y
这里最普通的类型是什么?在Haskell中这意味着什么,应该在这里做什么?如有任何解释,我将不胜感激。这不仅仅是准备考试的任何家庭作业,任何帮助都将受到欢迎。
从显式括号开始:
f 7 (g 'a') == (f 7) (g 'a')
Run Code Online (Sandbox Code Playgroud)
知道7 :: Num a => a和'a' :: Char,马上就知道
f :: Num a => a -> b
g :: Char -> c
Run Code Online (Sandbox Code Playgroud)
for some unconstrained types b and c. However, we also know that b is a function type, because the return value of f is applied to the return value of g. Further, we know the return value of g has type c. So we can refine our guess for what b must be:
f :: Num a => a -> (c -> d) -- parentheses are redundant
g :: Char -> c
Run Code Online (Sandbox Code Playgroud)
There's not much else you can do at this point, other than make the following observations:
f 7 :: c -> d
g 'a' :: c
f 7 (g 'a') :: d
Run Code Online (Sandbox Code Playgroud)