Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Haskell List of tuples to list?

Tags:

haskell

Is it possible to convert a list of tuples [(Int,Int)] as a generic way which valid to any input size ? .. i saw in various questions thats its not possible generically

example :

type X = [(Int,Int)]


func :: X -> [Int]
like image 688
Sudantha Avatar asked Jun 07 '11 18:06

Sudantha


1 Answers

You could also use a fold and avoid explicit recursion:

tupleToList = foldr (\(f,s) a -> f : s : a) []

Or:

tupleToList = foldl (\a (f,s) -> a ++ [f,s]) []

(For elements of the same type)

like image 104
danlei Avatar answered Nov 16 '22 00:11

danlei