3 Replies - 2879 Views - Last Post: 03 December 2011 - 08:14 PM

#1 Paticia  Icon User is offline

  • New D.I.C Head

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

Haskell … inside a list! - problem

Posted 03 December 2011 - 05:01 PM

Well … once again… help!!

It’s like this:
I have a list

list1 = [(11.0,"11","11"),(22.0,"22","22"),(33.0,"33","33"),(44.0,"44","44"),(55.0,"55","55"),(66.0,"66","66")]

And I need to change the order of 2 of the elements inside the list. For example, change (22.0,"22","22") for (55.0,"55","55"). That will make the list look like this:
[(11.0,"11","11"),(55.0,"55","55"),(33.0,"33","33"),(44.0,"44","44"),(22.0,"22","22"),(66.0,"66","66")]

So far, I’m trying drop and take:
change posA posB = take (posA-1) list1 ++ [list1!!(posB-1)] ++ [list1!!(posA-1)] ++ drop posB list1

But the middle elements missing (in this example "),(33.0,"33","33"),(44.0,"44","44") is missing). :/

Any ideas how to found those middle elements and add them to my function?
I really appreciate any kind of help.

Thanks.

Is This A Good Question/Topic? 0
  • +

Replies To: Haskell … inside a list! - problem

#2 Raynes  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 611
  • View blog
  • Posts: 2,815
  • Joined: 05-January 09

Re: Haskell … inside a list! - problem

Posted 03 December 2011 - 05:30 PM

I'm pretty sure that this does what you want:

import List
swap i j xs | i == j    = xs
swap i j xs | otherwise = initial ++ (xs !! B)/> : middle ++ (xs !! a) : end 
    where [a,b] = sort [i,j]
          initial = take a xs
          middle  = take (b-a-1) (drop (a+1) xs)
          end     = drop (b+1) xs



Code taken from here: http://www.haskell.o...May/009859.html

Go ahead and read that post. It makes a very good case for using a different data structure for this.
Was This Post Helpful? 1
  • +
  • -

#3 Paticia  Icon User is offline

  • New D.I.C Head

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

Re: Haskell … inside a list! - problem

Posted 03 December 2011 - 06:59 PM

Thanks Raynes
That really works :P

But let me give you the full assignment:
I have:
fulllist = [("abc",111,[(11.0,"11","11"),(22.0,"22","22"),(33.0,"33","33"),(44.0,"44","44"),(55.0,"55","55"),(66.0,"66","66")]),("def",222,[(1.1,"aa","aa"),(2.0,"bb","bb"),(3.0,"cc","cc"),(4.0,"dd","dd"),(5.0,"ee","ee"),(6.0,"ff","ff")])]

I need to make a function that change the order of 2 tuples from the list belonging to the first 3-tuple, [(11.0,"11","11"),(22.0,"22","22"),(33.0,"33","33"),(44.0,"44","44"),(55.0,"55","55"),(66.0,"66","66")]
or the second 3-tuple, [(1.1,"aa","aa"),(2.0,"bb","bb"),(3.0,"cc","cc"),(4.0,"dd","dd"),(5.0,"ee","ee"),(6.0,"ff","ff")]

For example, change (22.0,"22","22") for (55.0,"55","55"). That will make the list look like this:
[("abc",111,[(11.0,"11","11"),(55.0,"55","55"),(33.0,"33","33"),(44.0,"44","44"),(22.0,"22","22"),(66.0,"66","66")],("def",222,[(1.1,"aa","aa"),(2.0,"bb","bb"),(3.0,"cc","cc"),(4.0,"dd","dd"),(5.0,"ee","ee"),(6.0,"ff","ff")])]

I started to solve this splitting my “problems” into smaller ones:
sublist1 = [(11.0,"11","11"),(22.0,"22","22"),(33.0,"33","33"),(44.0,"44","44"),(55.0,"55","55"),(66.0,"66","66")]
sublist2 = [(01.1,"aa","aa"),(02.0,"bb","bb"),(03.0,"cc","cc"),(04.0,"dd","dd"),(05.0,"ee","ee"),(06.0,"ff","ff")]
list1 = [("abc",111,sublist1)]
list2 = [("def",222,sublist2)]
fulllist = list1 ++ list2


Now, I was rdy (I thought) to try to implement my function.
I'm trying this:
swap1 list posA posB = case list of 1 -> [("abc",111, take (posA-1) sublist1 ++ [sublist1!!(posB-1)] ++ MISSING THE MIDDLE ELEMENTS ++ [sublist1!!(posA-1)] ++ drop posB sublist1)] ++ list2
                                    2 -> list1 ++ [("def",222,  take (posA-1) sublist2 ++ [sublist2!!(posB-1)] ++ MISSING THE MIDDLE ELEMENTS ++ [sublist2!!(posA-1)] ++ drop posB sublist2)]
                                    otherwise -> error "no way!!"

We have not talked in class about Haskell modules yet(to use sort, I must import a module). So, there should be another way to handle this… And… I’m just a beginner in Haskell! :P

Maybe I need to make a different approach the problem… I dunno :S
Was This Post Helpful? 0
  • +
  • -

#4 Paticia  Icon User is offline

  • New D.I.C Head

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

Re: Haskell … inside a list! - problem

Posted 03 December 2011 - 08:14 PM

!done
\o/
Thanks to Raynes I was able to perform the changes in my function!
Raynes _o_
You rock \o/

Now I can go to sleep!
lol
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1