FastBatteryCharger
FastBatteryCharger

Reputation: 349

Haskell, two elements from one list followed by one element other list? (pattern matching)

i have a function that will take two elements from one list and one element from the second list, but there is a problem, if the given list has only one element:

example:

*Main> g2 [1] [5..10]
[1]

I want to have a output like this:

*Main> g2 [1] [5..10]
[1,5]

It works for list with more than 1 element:

*Main> g2 [1..5] [8..10]
[1,2,8,3,4,9,5]
*Main> g2 [1,2,3,4] [6,7,8,9]
[1,2,6,3,4,7]

My Code:

g2 (x1:x2:xs) (y:ys) = x1:x2:y:(g2 xs ys)
g2 (x1:[]) y = x1:[]
g2 [] ys = []

I think i forgot a pattern.

Upvotes: 0

Views: 1436

Answers (1)

Alistra
Alistra

Reputation: 5195

As confusing as your question is, this is the answer:

g2 (x1:x2:xs) (y:ys) = x1:x2:y:(g2 xs ys)
g2 (x1:[]) (y:ys) = x1:y:[]
g2 [] [] = []
-- Added missing patterns, don't know what the function is supposed to do here
g2 [] (y:ys) = []
g2 (x1:xs) [] = []

Upvotes: 3

Related Questions