4 Replies - 2620 Views - Last Post: 01 December 2011 - 07:20 PM

#1 Paticia  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 19
  • Joined: 01-December 11

Haskell 101 ... Tuples :S

Posted 01 December 2011 - 03:50 PM

Heeeeeeeeeeeelp plssssss.

Well, I have a beginner problem (for me, atm, is a very advanced problem :P ).

I have a list of tuples, like:
x = [("a","b","c"),("d","e","f")] 
Ö and I need to change, letís say, the "e" for "xyz". Can it be done?

Iíve tried to apply pattern matching, but with no success.
With tuples of 2 elements itís easier (by using fst and snd)Ö but Iím having lots of problems with tuples of 3 elements.
Is pattern matching the right way to solve this?

Any help will be welcome.
Thanks in advanced.

Is This A Good Question/Topic? 0
  • +

Replies To: Haskell 101 ... Tuples :S

#2 sepp2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2018
  • View blog
  • Posts: 3,046
  • Joined: 21-June 11

Re: Haskell 101 ... Tuples :S

Posted 01 December 2011 - 04:26 PM

I'm not quite sure what you're trying to do here. Can you show us how you would do it with 2-tuples using first and second? That might make it clearer what you're trying to accomplish.
Was This Post Helpful? 0
  • +
  • -

#3 Paticia  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 19
  • Joined: 01-December 11

Re: Haskell 101 ... Tuples :S

Posted 01 December 2011 - 04:58 PM

If I have, for example,
tuplelist1 = ("a","b")

and I wanna change "a" for "xyz":
tuplelist2 y = (fst tuplelist1, y)


then:
ghci> tuplelist2 "xyz"
("a","xyz")



... but I need it for 3-tuple ... in a list, like: x = [("a","b","c"),("d","e","f")]
Was This Post Helpful? 0
  • +
  • -

#4 sepp2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2018
  • View blog
  • Posts: 3,046
  • Joined: 21-June 11

Re: Haskell 101 ... Tuples :S

Posted 01 December 2011 - 06:45 PM

Okay, the 3-tuple equivalent of your code is:

tuplelist1 = ("a", "b", "c")
change (x, _, _) y z = (a,b,c)
tuplelist2 y z = change tuplelist1 y z



I.e. you do indeed use pattern matching to replace fst and snd.
Was This Post Helpful? 1
  • +
  • -

#5 Paticia  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 19
  • Joined: 01-December 11

Re: Haskell 101 ... Tuples :S

Posted 01 December 2011 - 07:20 PM

That's a great help.

It seams so easy... :S

thank you very much!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1