Wednesday, 15 February 2012

functional programming - Haskell: Sum of the occurrences of each char from a string A in a string B -



functional programming - Haskell: Sum of the occurrences of each char from a string A in a string B -

say have 2 strings, how can sum of occurrences of each char in string in string b?

for exemple, how can know, in string somestring: numbers of + numbers of b ... + number of f?

sumoccurrencesofcharsinstring :: string -> string -> int sumoccurrencesofcharsinstring str1 str2 = ? -- evaluating sumoccurrencesofcharsinstring somestring notes should homecoming 3+0+3+0+5+0+1= 12 somestring = "evaluating sumoccurrencesofcharsinstring somestring notes homecoming 12" notes = "abcdefg"

you can using list comprehension:

length [ s | s <- somestring, s `elem` notes]

haskell functional-programming fold

No comments:

Post a Comment