| | |
Haskell
Please support our Computer Science advertiser: Learn about neural networks and artificial intelligence.
Thread Solved |
•
•
Join Date: Nov 2005
Posts: 4
Reputation:
Solved Threads: 0
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
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
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

Follow my Twitter feed! twitter.com/DaniWeb
And if you're interested in Internet marketing there is twitter.com/DaniWebAds
•
•
•
•
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]]
•
•
•
•
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
subsets :: [a] -> [[a]] subsets [] = [[]] subsets (x:xs) = map (x:) (subsets xs) ++ subsets xs
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.
All my posts may be redistributed under the GNU Free Documentation License.
•
•
•
•
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
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

Follow my Twitter feed! twitter.com/DaniWeb
And if you're interested in Internet marketing there is twitter.com/DaniWebAds
•
•
Join Date: Nov 2005
Posts: 4
Reputation:
Solved Threads: 0
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)
-}
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)
-}
![]() |
Similar Threads
- Haskell (DaniWeb Community Feedback)
- Perl 6 and Haskell (Perl)
- Haskell (Computer Science)
Other Threads in the Computer Science Forum
- Previous Thread: Real-time OS
- Next Thread: If Blu-Ray and HDVD interests you. Check this out.
| Thread Tools | Search this Thread |
ai algorithm algorithms amazon assignmenthelp automata battery bigbrother binary bittorrent bizarre bletchleypark blogging bomb business cern codebreaker compiler computer computers computerscience computertrackingsoftware connect csc data dataanalysis dataintepretation development dfa dissertation dissertations dissertationthesis dissertationtopic ebook employment energy extensions floatingpoint foreclosure foreclosuresoftware fuel gadgets givemetehcodez government graphics hardware history homeowners homeworkassignment homeworkhelp humor ibm idea ideas internet iphone ipod itcontracts jobs kindle laser laws linkbait lsmeans mainframes marketing mining mobileapplication msaccess nano netbeans networking news os piracy piratebay principles programming rasterizer research sam-being-cute sas science sex software spying sql stephenfry study supercomputer supercomputing sweden technology textfield turing turingtest two'scompliment uk warehouse ww2






