(FunctionClauseError) no function clause matching in List.foldl/3

I get an error when calling Bob.heyfunction


     def foldl(list, acc, fun) when is_list(list) and is_function(fun)

 code: assert Bob.hey("Tom-ay-to, tom-aaaah-to.") == "Whatever."
 stacktrace:
   (elixir) lib/list.ex:188: List.foldl/3
   bob.exs:11: Bob.hey/1
   bob_test.exs:12: (test)```

The two functions related to the error are:

```def remove_misplaced_chars(str) do
  str
  |> List.foldl("", fn(ch, acc) -> if valid_char?(ch), do: acc <> ch, else: acc end)
end

def valid_char?(ch) do
  ch_up = String.upcase ch
  if List.first(to_charlist(ch_up)) in 65..90 or ch == ",", do: true, else: false
end

What is wrong?

List.foldl accepts a list and you’re giving it a string, which is not a list. I believe you could first split it with String.codepoints before feeding it to foldl.

1 Like