Reputation: 716
I need some help :/
I have the fowlling list:
xs = [("a",1,[(11,"aa","aa"),(22,"bb","bb")]),("b",2,[(33,"cc","cc"),(44,"dd","dd")])]
And I need to make a function that allows me to change the values of "a" and 1 (the first 2 values from the first 3-tuple on the list).
I tried to solve this by spliting my list:
xs1 = ("a",1,[(11,"aa","aa"),(22,"bb","bb")])
xs2 = ("b",2,[(33,"cc","cc"),(44,"dd","dd")])
then:
changeXs (_, _, z) x y = (x,y,z)
newXs x y = [changeXs xs1 x y]
this works for change xs1. I thought that now it would be easy concatenate xs2 to my newXs function, using ++ ... But it wont work (I believe I cant use ++ whit pattern matching, am I right?)
Any ideas about how can I concatenate xs2 to my newXs function ? Or any other approach to solve the problem?
Thanks in advance.
Upvotes: 0
Views: 778
Reputation: 5411
You cannot concatenate newXs x y
, that is a list
, with xs2
, that is a tuple
.
Upvotes: 0
Reputation: 8448
Your problem is with ++
.
++
has the type [a] -> [a] -> [a]
In other words, it takes two lists and "puts them together"
What you want is to add some a
to a [a]
Try
(:) :: a -> [a] -> [a]
Upvotes: 0
Reputation: 130004
insert_new_xy x y ((_, _, z) : rest) = (x, y, z) : rest
Test:
*Main> insert_new_xy "foo" 42 xs
[("foo",42,[(11,"aa","aa"),(22,"bb","bb")]),("b",2,[(33,"cc","cc"),(44,"dd","dd")])]
The ((_, _, z) : rest)
pattern matches a list, whose head is a three-element tuple and binds last element in that tuple and the tail of the list. Then it's just a matter of recombining the list, by creating a new tuple with given x
, y
and extracted z
, and prepending it to the tail.
Upvotes: 4