You are not logged in.

#1 2010-05-11 01:15:48

cordedconch
Member
Registered: 2010-05-11
Posts: 1

Haskell triples and lists

Hi everyone– I'm a newbie to Haskell and looking for help. PLEASE !!!!

Say I have a list of triples with the type:
[(Int, [Int], Int)]

so for example the user may input:

[(1, [1,2,3,4], 8)]
[(4, [2,4], 19)]
[(3, [1,5,9,10], 10)]
[(23, [9,9,2,27,1], 1)]
[(5, [1,4,2,6,8,4], 7)]

I am trying to write a program in Haskell that goes through all of the user inputted triples and returns a single integer. I want this integer to be the length of the longest list out of all the triples. In the example above, the longest list is in the 5th line, and it is 6 integers long. So the integer should be 6.

The program should be able to cope with as many triples as the user chooses to input.

I have gotten thus far:

Here is the type definition for my main function:
biggest :: [(Int, [Int], Int)] -> a

Here is my function that singles out the middle element in the triple (this is the only part of the triple that we're concerned about):
center :: (a, [Int], c) -> [Int]
center (_, [x] ,_) = [x]

Here is my function that determines the length of the middle element:
length' :: [a] -> Int
length' [] = 0
length' (_:l) = 1 + len l

I'm trying to work out a way to stitch it all together, but am having some difficulty. Any suggestions?

Thank you !

Offline

#2 2010-05-11 01:44:26

jxy
Member
Registered: 2008-12-03
Posts: 133

Re: Haskell triples and lists

I guess you might want to read A Tour of the Haskell Prelude to familiarize yourself with the language.  And for the problem at hand, you just need 3 Prelude functions and pattern matching.

f = maximum . map (\(_, l, _) -> length l)
f $ [(1, [1,2,3,4], 8)] ++ [(4, [2,4], 19)] ++ [(3, [1,5,9,10], 10)] ++ [(23, [9,9,2,27,1], 1)] ++ [(5, [1,4,2,6,8,4], 7)]
==> 6

Offline

#3 2010-05-11 01:49:00

diegonc
Member
Registered: 2008-12-13
Posts: 42

Re: Haskell triples and lists

You may want to use map ,

map :: (a -> b) -> [a] -> [b]

to get a [ [Int] ] out of a [ (Int, [Int], Int) ]

And then again to get a [Int] out of a [ [Int] ]

Offline

Board footer

Powered by FluxBB