Please support our Computer Science advertiser: Learn about neural networks and artificial intelligence.
Thread Solved

Join Date: Nov 2005
Posts: 4
Reputation: optimak is an unknown quantity at this point 
Solved Threads: 0
optimak optimak is offline Offline
Newbie Poster

Haskell

 
0
  #1
Nov 18th, 2005
Hello everyone, I'm new here and have browsed through some discussions and found it useful. I was wondering if anyone could help me with a functional programming problem in Haskell.

Im trying to define a function seg, which is supposed to take a finite list xs as its argument and returns the list of all the segments xs. What is meant by a segment, is a list of adjacent elements i.e.
seg [1,2,3] = [[1,2,3], [1,2], [2,3], [1], [2], [3]]

Ive given it a shot, but my program is partially working with wrong output, i get
seg [1,2,3] = [[1,2,3],[1,3],[2,3],[3]]

Here is my code,...

cons :: Int -> [Int] -> [Int]
cons x l = (x:l)
seg :: [Int] -> [[Int]]
seg [] = []
seg [x] = [[x]]
seg (x:xs) = (map (cons x) (seg xs)) ++ seg xs


I'll be grateful if anyone can help me, because it is one of the last few questions that i have to do before next week. Will be mega :lol: when its over.

Can you please email optimak@gmail.com to let me know, or do i just regularly check the forums.

Thanks very much
Reply With Quote Quick reply to this message  
Join Date: Feb 2002
Posts: 12,047
Reputation: cscgal is a glorious beacon of light cscgal is a glorious beacon of light cscgal is a glorious beacon of light cscgal is a glorious beacon of light cscgal is a glorious beacon of light cscgal is a glorious beacon of light 
Solved Threads: 130
Administrator
Staff Writer
cscgal's Avatar
cscgal cscgal is offline Offline
The Queen of DaniWeb

Re: Haskell

 
0
  #2
Nov 25th, 2005
Please post your question in the appropriate forum, which I believe would be the Computer Science forum ... http://www.daniweb.com/techtalkforums/forum14.html
Dani the Computer Science Gal
Follow my Twitter feed! twitter.com/DaniWeb
And if you're interested in Internet marketing there is twitter.com/DaniWebAds
Reply With Quote Quick reply to this message  
Join Date: Jun 2005
Posts: 2,052
Reputation: Rashakil Fol is just really nice Rashakil Fol is just really nice Rashakil Fol is just really nice Rashakil Fol is just really nice 
Solved Threads: 139
Team Colleague
Rashakil Fol's Avatar
Rashakil Fol Rashakil Fol is offline Offline
Super Senior Demiposter

Re: Haskell

 
2
  #3
Nov 25th, 2005
Originally Posted by optimak
Im trying to define a function seg, which is supposed to take a finite list xs as its argument and returns the list of all the segments xs. What is meant by a segment, is a list of adjacent elements i.e.
seg [1,2,3] = [[1,2,3], [1,2], [2,3], [1], [2], [3]]
I would have guessed that the correct output should include [] as a segment.

Originally Posted by optimak
cons :: Int -> [Int] -> [Int]
cons x l = (x:l)
seg :: [Int] -> [[Int]]
seg [] = []
seg [x] = [[x]]
seg (x:xs) = (map (cons x) (seg xs)) ++ seg xs
This code looks more like the code that produces all the subsets of a list. The code that produces all the subsets of a list is
subsets :: [a] -> [[a]]
subsets [] = [[]]
subsets (x:xs) = map (x:) (subsets xs) ++ subsets xs
after all. Look closely at your last line. Does mapping the function (cons x) to the results of (seg xs) produce continuous segments? I'd think you'd only want to prepend x to the segments that begin at the beginning of the list xs. I recommend using a helper function for this.

As you have it, seg [1,2,3] results in (map (cons 1) (seg [2,3])) being called, and since [3] is a segment of [2,3], this results in [1,3] being given as a segment.

Can you please email optimak@gmail.com to let me know, or do i just regularly check the forums.
Answers should be on the forums so that others can see them (and then correct idiots like me).
All my posts may be redistributed under the GNU Free Documentation License.
Reply With Quote Quick reply to this message  
Join Date: Feb 2002
Posts: 12,047
Reputation: cscgal is a glorious beacon of light cscgal is a glorious beacon of light cscgal is a glorious beacon of light cscgal is a glorious beacon of light cscgal is a glorious beacon of light cscgal is a glorious beacon of light 
Solved Threads: 130
Administrator
Staff Writer
cscgal's Avatar
cscgal cscgal is offline Offline
The Queen of DaniWeb

Re: Haskell

 
0
  #4
Nov 25th, 2005
Originally Posted by cscgal
Please post your question in the appropriate forum, which I believe would be the Computer Science forum ... http://www.daniweb.com/techtalkforums/forum14.html
Okay okay. This thread moved. Hopefully Comp Sci is the right forum?
Dani the Computer Science Gal
Follow my Twitter feed! twitter.com/DaniWeb
And if you're interested in Internet marketing there is twitter.com/DaniWebAds
Reply With Quote Quick reply to this message  
Join Date: Nov 2005
Posts: 4
Reputation: optimak is an unknown quantity at this point 
Solved Threads: 0
optimak optimak is offline Offline
Newbie Poster

Re: Haskell

 
0
  #5
Nov 26th, 2005
Thanks very much for your help cscgal.
Reply With Quote Quick reply to this message  
Join Date: Nov 2005
Posts: 4
Reputation: optimak is an unknown quantity at this point 
Solved Threads: 0
optimak optimak is offline Offline
Newbie Poster

Re: Haskell

 
0
  #6
Nov 26th, 2005
Hi people, this is the last Haskell problem that i have which is in for Monday morning. Thanks cscgal for helping me on my penultimate question.

I am trying to define a function 'partition' that takes in an integer as an argument and returns a list of all possible positive integers that would sum to the input, ie
partition 4 = [[1, 1, 1, 1], [1, 1, 2], [1, 3], [2, 2], [4]]

The function (rather formally defined) can be found here
http://en.wikipedia.org/wiki/Partiti...mber_theory%29

From the outset it looks like i'll need to increment and decrement different variables on each recursive call, but have no idea where to start. I have included the code from attemps below. Thanks for your time....



{- START

parts n
| n==0 = []
| otherwise = auxPart 1 n

cons :: Int -> [Int] -> [Int]
cons k l = k:l

auxPart :: Int -> [[Int]]
auxPart k n
| k == n = [[]]
| k < n = part1 k n ++ auxPart k+1 n

part1 k n = map (cons k) auxPart k(n-k)

-}
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the Computer Science Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC