Search code examples
smlsmlnjml

SML/NJ Asked to modify my code to use fldr, fldl and map


I'm asked to modify the code I wrote such that in most of the functions I have to use either fldr or fldl

Here is the description which includes the test cases.

I'm wondering it would be great if you can give me some hint where I should modify to use those functions.

Due to lack of documentations and examples in ML I had to ask this question here

Thank you.

fun is_member [] x = false |
is_member (h::t) x = if (h=x) then true else is_member t x;


fun splitter string = 
let
fun remove_c [] = [] |
remove_c (h::t) = (String.tokens (fn c => c = #":") h) @ (remove_c t)
fun remove_ex [] = [] |
remove_ex (h::t) = remove_c((String.tokens (fn c => c = #"!") h)) @ (remove_ex t)
fun remove_q [] = [] |
remove_q (h::t) = remove_ex((String.tokens (fn c => c = #"?") h)) @ (remove_q t)
fun remove_sc [] = [] |
remove_sc (h::t) = remove_q((String.tokens (fn c => c = #";") h)) @ (remove_sc t)
fun remove_dot [] = [] |
remove_dot (h::t) = remove_sc((String.tokens (fn c => c = #".") h)) @ (remove_dot t)
fun remove_nl [] = [] |
remove_nl (h::t) = remove_dot((String.tokens (fn c => c = #"\n") h)) @ (remove_nl t)
fun remove_tabs [] = [] |
remove_tabs (h::t) = remove_nl((String.tokens (fn c => c = #"\t") h)) @ (remove_tabs t)
fun remove_commas [] = [] |
remove_commas (h::t) = remove_tabs((String.tokens (fn c => c = #",") h)) @ (remove_commas t)
in
remove_commas((String.tokens (fn c => c = #" ") string))
end;

val stop_words = "a,able,about,across,after,all,almost,also,am,among,an,and,any,are,as,at,be,because,been,but,by,can,cannot,could,dear,did,do,does,either,else,ever,every,for,from,get,got,had,has,have,he,her,hers,him,his,how,however,i,if,in,into,is,it,its,just,least,let,like,likely,may,me,might,most,must,my,neither,no,nor,not,of,off,often,on,only,or,other,our,own,rather,said,say,says,she,should,since,so,some,than,that,the,their,them,then,there,these,they,this,tis,to,too,twas,us,wants,was,we,were,what,when,where,which,while,who,whom,why,will,with,would,yet,you,your";

val stop_word_list = splitter stop_words;

fun is_stop_word string = 
let
fun f L = map (fn x => (is_member stop_word_list x)) L
in
f (splitter string)
end;


fun get_stop_words string = 
let
fun get_stop [] x = x |
get_stop (h::t) x = if((is_member stop_word_list h = true) andalso ((is_member t h) = false)) then (get_stop t [h]@x) else (get_stop t x);
in
get_stop (splitter string) []
end;


fun remove_stop_words string = 
let
fun remove_stop [] = [] |
remove_stop (h::t) = if(is_member stop_word_list h = true) then (remove_stop t) else [h]@(remove_stop t)
in
remove_stop(splitter string)
end;

Solution

  • It is important to first understand how foldl and foldr work, once you have that you can apply the logic from your existing code to the functions.

    foldl(fn (a,b) => (*expression*)) *initial condition* *list*;

    So foldl(or foldr) takes a function as the first parameter. You can use some sml defined function like op > or op + but often these more simple functions don't do the job that you want them to do.

    The alternative is to write your own anonymous function and pass it as a parameter such as in the example above. In addition to the function, foldl takes two more arguments being the initial condition and the list.

    foldl then looks at the indexes of the list and using the initial condition, applies that index to the function. In the case of my example, a is the list index being passed to the function by foldl and b is the result of the expression for the first call (first index).

    foldl will continue to do this for the remainder of the list, passing each index to a and the result of each call to the function in b.

    So using a, you can really perform any computation you want and make any comparisons that you want using b.

    I don't really want to give you code but here is the setup for is_member.

    fun is_member list x = foldl(fn (a,b) => (if *expression* then *some result* else *some other result*)) *initial condition* list;
    

    You will need to use a,b, and x in your comparison and in your return. Since this is a boolean return, your initial condition should either be true or false. Good luck.