简体   繁体   English

Haskell-将列表解压缩为自定义数据类型

[英]Haskell - unpacking a list into a custom data type

Is it possible to unpack a list into a data type, without specific pattern matching? 是否可以在没有特定模式匹配的情况下将列表解压缩为数据类型?

for example 例如

data MyType = MyType Int Int Int deriving Show
let l = [1, 2, 3]

func :: [Int] -> MyType

usage would be 用法将是

λ: func l
λ: Mytype 1 2 3 

What I am trying to achieve.* 我正在努力实现的目标。*

I have a list of lists which I pulled in from a CSV file. 我有一个列表列表,这些列表是我从CSV文件中提取的。 Each of the lists is a list of type variables. 每个列表都是类型变量的列表。 I was hoping there would be a fast way of passing those type variables into the type without having to pattern match ten or more variables. 我希望有一种快速的方法可以将那些类型变量传递给类型,而不必模式匹配十个或更多变量。

I personally think you should use the pattern matching.... 我个人认为您应该使用模式匹配。

However, you can use the info in this answer to convert a list to a tuple.... 但是,您可以使用此答案中的信息将列表转换为元组。

How do I convert a list to a tuple in Haskell? 如何在Haskell中将列表转换为元组?


Then you can use the info in this answer to convert that tuple to params for your constructor 然后您可以使用此答案中的信息将元组转换为构造函数的参数

Uncurry for n-ary functions n元函数的无毛

It works, and is sort of intellectually clean, but it is a lot of work to avoid one line of pattern matching. 它可以工作,并且从理论上讲是干净的,但是要避免一行模式匹配是很多工作。

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

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