RE:[Haskell-cafe] A function using List and Tuple

19 Jun
2006
19 Jun
'06
10:40 a.m.
Hi
On 6/19/06, Sara Kenedy
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.
Yo can use the foldr function: sumList a = ("#",foldr suma 0 a ) where suma (s,x) y = 1/x + y Juan Jose
6912
Age (days ago)
6912
Last active (days ago)
0 comments
1 participants
participants (1)
-
jotajota