The question might sound confusing but basically I want to make a function that takes in a list and another function. Now going through that list I want to get the count of elements that return true when inputted into this parameter function. Im new to sml so my mistake was that I intended to add a count to a helper function and increment that alongside If statements but I learned soon enough that you can't really increment values in ml. Can someone please explain the more sml way of doing this? I'm just looking to understand what I'm missing.
fun helper f nil counter = false
| helper f xs 1 = true
| helper f (x::xs) counter =
if (f x) = true then
counter = counter + 1
else
counter = counter + 0
fun numberExisting f alist = (helper f alist 0);
The function should not return a boolean, so neither should your helper.
"Incrementing" is also a concept that doesn't make much sense in a language without mutation.
(counter = counter + 1
is a comparison that will never be true.)
You don't need the helper, but you need to think recursively:
In ML:
fun numberExisting _ [] = 0
| numberExisting f (x::xs) = if f x
then 1 + numberExisting f xs
else numberExisting f xs