samedi 25 février 2017

How do you use foldl to find out the number of times a letter is repeated in a string?

I'm new to Erlang and am testing different functions around to get a grasp of them. I want this function to be able to return an integer that says the number of times a letter variable is repeated in a string using the foldl function but am kinda stuck with deciding the "fun" function. Any help?

Examples of what I want the function to return:

`test:count("Hello this is a test", $i)`

should return 2

test:count("Hello this is a test", $t)

should return 3

-spec count(String, Char) -> integer() when
  String::string(),
  Char::char().

count(String, Char) ->

F = ???,

lists:foldl(F, 0, String).

Aucun commentaire:

Enregistrer un commentaire