简体   繁体   中英

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. 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?


Then you can use the info in this answer to convert that tuple to params for your constructor

Uncurry for n-ary functions

It works, and is sort of intellectually clean, but it is a lot of work to avoid one line of pattern matching.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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