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]]

I would have guessed that the correct output should include [] as a segment.

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).

Thanks very much for your help cscgal.

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/Partition_function_%28number_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)

-}

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.