[Haskell-cafe] A function using List and Tuple

Sara Kenedy sarakenedy at gmail.com
Mon Jun 19 01:13:36 EDT 2006


Hello,

I want to creat a function sumList as below examples:
sumList [("s1",2),("s2",4),("s3",3),("s4",2)] = ("#", 1/2 + 1/4 + 1/3 + 1/2)
sumList [("s1",2),("s2",4),("s3",3) =  ("#", 1/2 + 1/4 + 1/3)

I attempted it as following:

sumList :: (Fractional a) => [(String,a)] -> (String, a)
sumList [] = ???
sumList (x:xs) = ("#", 1/(snd x) + 1/snd(sumList xs))

I know this function cannot give the correct answer (even if I define
sumList []), but I did not find the right way.
If anyone can give me a suggestion, I really appereciate for that. Thanks.

S.


More information about the Haskell-Cafe mailing list