对于作业,我正在处理一系列函数[Int -> Int](例如[(+3), (*4), (+1)]),我想Int对它们各应用一个,然后创建一个结果列表[Int]
我已经搜索了很多,但我无法找到办法进行这样的操作.使用map不如我所料.相关的错误是这样的:
ERROR - Cannot infer instance
*** Instance : Num ((Label -> Label) -> a)
Run Code Online (Sandbox Code Playgroud)
按要求代码:
data Tree = Node (Label -> Label) Label [Tree]
type Label = Int
testTree = Node (+1) 3 [ Node (+1) 5 [], Node (+1) 4 [Node (+1) 1 [], Node (+2) 7 []]]
listify :: Tree -> [(Label -> Label)]
listify t = [(getNodeFunction t)] ++ concat(map (listify) (getSubTrees t))
*Main> map (\f -> f 7) (listify testTree)
Run Code Online (Sandbox Code Playgroud)
这实际上有效.在文件中还有一段错误的代码,对不起大惊小怪.
Jak*_*old 32
您可以使用$代表功能应用的运算符.
> map ($ 3) [(+3), (*4), (+1)]
[6,12,4]
Run Code Online (Sandbox Code Playgroud)
这基本上扩展到了[(+3) $ 3, (*4) $ 3, (+1) $ 3],这只是功能应用.
基本上这是一个应用性的工作。你可能喜欢
?> [(+3), (*4), (+1)] <*> pure 3 -- or [3]
[6,12,4]
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
5245 次 |
| 最近记录: |