I am trying to find out smlnj at the moment and I am having problems with the fold function.
What I'm trying to do is write a function, choose that uses a folding pattern and accepts the function and list. It will take a list of the list in the function to determine if it will add this element to the list. Here is an example of what I mean.
select (fn x => x mod 2 = 0) [1,2,3,4,5,6,7,8,9,10]; val it = [2,4,6,8,10] : int list
So here is what I still have ...
fun select fl = foldl (fn (x,y) => if (f(x)) then x else 0) 0 l;
This is obviously not working properly. It just returns 10. I'm sure I need to use op :: somehow to make this work, but I can't figure it out. I thought it should look something like this ...
fun select fl = foldl (fn (x,y) => if (f(x)) then op:: else []) [] l;
But that does not work. Any help would be greatly appreciated. Thanks!
source share