简体   繁体   English

一行中的多个函数调用的语法-Haskell

[英]Syntax for multiple function calls in one line - Haskell

This may be really straightforward but I'm new to Haskell and have no idea what I'm doing. 这可能很简单,但是我是Haskell的新手,也不知道我在做什么。

I have three functions: 我有三个功能:

first :: (Int, [Char], [[Char]], [((Int,Int),(Int,Int))]) -> (Int, [Char], [[Char]])
first (a, arr, stringlist, []) = (a, arr, stringlist)
first (a, arr, stringlist, (x:xs))
    | snd(snd(x)) == 1 = (first $ (second (a, arr, stringlist)) xs)
    | otherwise = (first $ (third (a arr stringlist x)) xs)

second :: (Int, [Char], [[Char]]) -> (Int, [Char], [[Char]])

third :: (Int, [Char], [[Char]], ((Int,Int),(Int,Int))) -> (Int, [Char], [[Char]])

Second and third both work so the details of those don't matter. 第二和第三个都起作用,因此这些细节无关紧要。 I'm sure there are multiple errors in my implementation of first but the part that says 我敢肯定我的第一个实现中存在多个错误,但部分内容是

(first $ (second (a, arr, stringlist)) xs)

is my main issue. 是我的主要问题。 What is the proper way to call second and then pass the result and the tail of the list to first? 什么是调用第二个然后将结果和列表尾部传递给第一个的正确方法?

You should definitely consider some type aliases, or even better use record syntax to simplify your type signatures. 您绝对应该考虑一些类型别名,甚至最好使用记录语法来简化类型签名。 You can also get rid of a lot of those parentheses: 您也可以删除许多括号:

first :: (Int, [Char], [[Char]], [((Int,Int),(Int,Int))]) -> (Int, [Char], [[Char]])
first (a, arr, stringlist, []) = (a, arr, stringlist)
first (a, arr, stringlist, (x:xs))
    | snd (snd x) == 1 = first $ (second (a, arr, stringlist)) xs
    | otherwise = first $ (third $ a arr stringlist x) xs

And I think this makes it easier to see that your syntax is a bit wrong. 而且我认为这样可以更轻松地看到您的语法有点错误。 What you have is passing the argument xs to second (a, arr, stringlist) , which does not return a function so it will result in an error, and similarly with the second case. 您所拥有的是将参数xs传递给second (a, arr, stringlist) ,后者不返回函数,因此将导致错误,与第二种情况类似。 If you changed it to this, it would be closer to correct. 如果将其更改为此,它将更接近于更正。

first :: (Int, [Char], [[Char]], [((Int,Int),(Int,Int))]) -> (Int, [Char], [[Char]])
first (a, arr, stringlist, []) = (a, arr, stringlist)
first (a, arr, stringlist, (x:xs))
    | snd (snd x) == 1 = first (second (a, arr, stringlist)) xs
    | otherwise = first (third $ a arr stringlist x) xs

I also see that you're passing 4 arguments to third , when it should be taking a 4-tuple, so maybe something like 我还看到您正在将4个参数传递给third ,当它应该取一个4元组时,所以也许像

first :: (Int, [Char], [[Char]], [((Int,Int),(Int,Int))]) -> (Int, [Char], [[Char]])
first (a, arr, stringlist, []) = (a, arr, stringlist)
first (a, arr, stringlist, (x:xs))
    | snd (snd x) == 1 = first (second (a, arr, stringlist)) xs
    | otherwise = first (third (a, arr, stringlist, x)) xs

Now we get to the problem that you're passing two arguments to first when you only want one argument. 现在我们要解决的问题是,当您只想要一个参数时,您first传递两个参数。 You have something of type (Int, [Char], [[Char]]) and something else of type [((Int, Int), (Int, Int))] , and you want to combine them into a 4-tuple. 您有某种类型的(Int, [Char], [[Char]])和其他一些类型的[((Int, Int), (Int, Int))] ,并且想要将它们组合成一个4元组。 I would suggest making an auxiliary function to do just that: 我建议做一个辅助功能来做到这一点:

mk4from3 :: (a, b, c) -> d -> (a, b, c, d)
mk4from3 (a, b, c) d = (a, b, c, d)

Then you can simply write 然后你可以简单地写

first :: (Int, [Char], [[Char]], [((Int,Int),(Int,Int))]) -> (Int, [Char], [[Char]])
first (a, arr, stringlist, []) = (a, arr, stringlist)
first (a, arr, stringlist, (x:xs))
    | snd (snd x) == 1 = first $ mk4from3 (second (a, arr, stringlist)) xs
    | otherwise = first $ mk4from3 (third (a, arr, stringlist, x)) xs

The general rule of thumb I use when deciding if I should convert a tuple into its own data type is if I use a 3-tuple or larger in more than one type signature, I should make it a data type, even if it isn't using record syntax and instead just uses ADT features. 在决定是否应将元组转换为其自己的数据类型时,通常使用的经验法则是,如果我在一个以上的类型签名中使用3个元组或更大的元组,即使它不是,我也应将其设为数据类型。 t使用记录语法,而仅使用ADT功能。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM