简体   繁体   English

是否可以在Haskell中编写任何没有显式参数的函数?

[英]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.

相关问题 是否可以在Haskell中编写此函数? - Is it possible to write this function in Haskell? Haskell function 计算 f 0 + f 1 + ... + fn 而没有显式递归 - Haskell function that evaluates f 0 + f 1 + … + f n without explicit recursion 是否可以编写类型为Int - &gt; Float的Haskell函数 - Is it possible to write a Haskell function with type Int -> Float 是否可以在haskell中编写一个函数:(r - &gt; [a]) - &gt; [r - &gt; a])? - Is it possible to write a function in haskell: (r -> [a]) -> [r -> a])? 是否有可能在Rust中编写Haskell的翻转函数? - Is it possible to write Haskell's flip function in Rust? haskell函数中不带参数的where子句 - Where clause in haskell function without parameters Haskell:如何编写带有单调上下文的参数的单调变参函数 - Haskell: how to write a monadic variadic function, with parameters using the monadic context 如何在Haskell中编写参数和结果均为多态的函数 - How to write a function whose both parameters and results are polymorphic in Haskell 是否有任何工具可以帮助在Haskell中显式导入模块? - Is there any tool for aiding with explicit import of modules in Haskell? 如何将任何类型的列表作为函数参数 Haskell - How to Take Lists of Any Types as Function Parameters Haskell
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM