简体   繁体   English

Haskell列表到元组

[英]Haskell List to Tuple

I am writing a function, parseArgs , which will take in command line arguments in the form of a list. 我正在编写一个函数parseArgs ,它将以列表的形式接受命令行参数。 If the size of the list is two, the function is to parse the contents of the list and convert them to a tuple, otherwise returning Nothing . 如果列表的大小是2,则该函数将解析列表的内容并将其转换为元组,否则返回Nothing I am unsure of quite how to go about this; 我不确定如何解决这个问题; my code thus far is below: 到目前为止,我的代码如下:

parseArgs :: [String] -> Maybe (String, Maybe String)
parseArgs [x, y]
  | length [x, y] < 2 = Nothing
  | length [x, y] > 2 = Nothing
  | otherwise = Just (x, Just y)

In your code, parseArgs [x, y] means it only accepts exactly a list of two elements. 在您的代码中, parseArgs [x, y]表示它只接受两个元素的列表。 So length [x, y] will always be 2, and those (>2) (<2) conditions will never be met. 因此length [x, y]将始终为2,并且永远不会满足那些(> 2)(<2)条件。

otherwise will always be a list of two elements. otherwise将始终是两个元素的列表。 So when the input is a list of two elements, you can get x and y, and make them a Maybe tuple for sure. 所以当输入是两个元素的列表时,你可以得到x和y,并确保它们是一个Maybe元组。

But other than that, if you parseArgs [] parseArgs ["a"] parseArgs ["a","b","c"] , you get an exception "Non-exhaustive patterns in function parseArgs". parseArgs [] ,如果你parseArgs [] parseArgs ["a"] parseArgs ["a","b","c"] ,你会得到一个异常“函数parseArgs中的非详尽模式”。 It is because the code didn't cover all the patterns in [String] 这是因为代码没有涵盖[String]所有模式

I use Maybe (String, String) for output here. 我在这里使用Maybe(String,String)作为输出。 It means parseArg will either produce Just (String, String) or Nothing. 这意味着parseArg将生成Just(String,String)或Nothing。 Perhaps it's closer to what you want. 也许它更接近你想要的东西。

So try this: 试试这个:

parseArgs :: [String] -> Maybe (String, String)
parseArgs x:y:[] = Just (x,y)
parseArgs xs = Nothing

It means if the input [String] happens to be x:y:[] (a list of exact two strings), produce Just (x,y). 这意味着如果输入[String]恰好是x:y:[](精确两个字符串的列表),则生成Just(x,y)。 Other than that, produce Nothing. 除此之外,什么都不做。 In this way, it covers all patterns in [String]. 这样,它涵盖了[String]中的所有模式。 And you then can get Nothing when it is not a list of two elements. 然后,当它不是两个元素的列表时,你就可以得到Nothing

Edit: And second to @pdoherty926's parseArgs _ = Nothing , the wildcard _ is a better way to express "everything else". 编辑:第二个是@ pdoherty926的parseArgs _ = Nothing ,通配符_是表达“其他一切”的更好方式。

@Johhny Liao beat me to it, but here's my similar answer: @Johhny Liao打败了我,但这是我的回答:

Based on your requirements, I'm unclear as to why the second tuple element would be Maybe String . 根据您的要求,我不清楚为什么第二个元组元素是Maybe String So, I'm going to proceed as though the type of your function is: parseArgs :: [String] -> Maybe (String, String) 所以,我将继续进行, parseArgs :: [String] -> Maybe (String, String)你的函数的类型是: parseArgs :: [String] -> Maybe (String, String)

parseArgs :: [String] -> Maybe (String, String)
parseArgs [x, xx] = Just (x, xx)  -- pattern match on the list of two elements
parseArgs _ = Nothing               -- discard _everything_ else

print $ parseArgs ["hi", "bye"]    -- Just ("hi", "bye")
print $ parseArgs ["hi"]               -- Nothing

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

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