[英]Is it possible to write any function without explicit parameters in Haskell?
For example I have a table tab = [(1,11), (2,22), (3,33)]
and two functions. 例如,我有一个表
tab = [(1,11), (2,22), (3,33)]
和两个函数。 The first one will take as parameter the table and an Integer and will replace the Integer with the value from the table. 第一个将表和一个Integer用作参数,并将Integer替换为表中的值。 Let's assume the Integer will be on the table.
假设Integer将出现在桌子上。
replace :: [(Int, Int)] -> Int -> Int
replace t i = snd $ head [x | x <- t, fst x == i]
The second function will replace all the occurrences of 1, 2, 3
from a list with the values from the table. 第二个函数将用列表中的值替换列表中出现的所有
1, 2, 3
。
replaceAll :: [(Int, Int)] -> [Int] -> [Int]
First I got: 首先我得到:
replaceAll = \tab lst -> foldl(\acc x -> acc ++ [replace tab x]) [] lst
then, 然后,
replaceAll = \tab -> foldl(\acc x -> acc ++ [replace tab x]) []
Is there a way to write this without the lambda function? 没有lambda函数,有没有办法写这个? EDIT: Not necessary using the fold function.
编辑:不需要使用折叠功能。
This works for me 这对我有用
replaceAll = map . replace
Although I also like removing params, I think this is a bit easier to understand if you fill them the params for this definition.... 尽管我也喜欢删除参数,但是我认为如果为这些定义填充参数,这会更容易理解。
replaceAll index vals = map (replace index) vals
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.