1

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

1 Answer 1

4

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]]
Sign up to request clarification or add additional context in comments.

3 Comments

thanks i know it was something small but just didnt know it in haskell
Note that this is a map : addVal i = map (\x -> [i,x]). I don't know if I would even write a function for this...
im trying to convert my java code over to haskell for i can compare them so trying to do it as literal at possible but thanks for the help

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.