HopefulSplash
HopefulSplash

Reputation: 31

Making List of Lists Haskell

i am trying to get pairs of coords and i have got this function that outputs: [9,0,9,1]....... etc

addVal :: Int -> [Int] -> [Int]
 addVal i [] = []
 addVal i (x:xs) =  i:x : addVal i xs

but i want the output to be a list of lists : [[9,0],[9,1]]

addVal :: Int -> [Int] -> [[Int]]
addVal i [] = [[]]

how do i get it so the it will make each pair a list so i i can use it with my other functions to get the smallest of the pairs

Upvotes: 1

Views: 10097

Answers (1)

Sibi
Sibi

Reputation: 48766

You are almost there, instead of i:x you have to use [i,x]. Note that you want the elements in the new list, so you create [i,x] and pass it up.

addVal :: Int -> [Int] -> [[Int]]
addVal i [] = []
addVal i (x:xs) =  [i,x] : addVal i xs

Demo in ghci

λ> addVal 9 [1,2]
[[9,1],[9,2]]

Upvotes: 4

Related Questions