我正在大学的课程中学习Haskell,并且有一个考试练习,我们需要定义一个函数,它接受一个函数列表 [(Int ->Int)]和一个Type的另一个参数Int并返回一个Int.所以类型应该是
compose :: [(Int ->Int)] -> Int -> Int.
Run Code Online (Sandbox Code Playgroud)
该函数应从左到右返回列表中函数的组合,并将其应用于第二个参数.我尝试了以下方法:
compose :: [(Int -> Int)] -> Int -> Int
compose [] x = x
compose (f:fs) x
|fs == [] = f x
|f : (compose fs x)
Run Code Online (Sandbox Code Playgroud)
但编译器抛出一个错误:
003Exam.hs:24:22:
Couldn't match expected type ‘Int’ with actual type ‘[Int -> Int]’
In the expression: f : (compose fs x)
In an equation for ‘compose’:
compose (f : fs) x
| fs == [] …Run Code Online (Sandbox Code Playgroud) haskell ×1