SML / NJ要求修改我的代码以使用fldr,fldl和map

时间:2014-02-27 01:58:57

标签: sml smlnj ml

我被要求修改我编写的代码,以便在大多数函数中我必须使用fldr or fldl

Here是包含测试用例的描述。

我想知道如果你能给我一些提示我应该修改以使用这些功能会很棒。

由于缺少ML中的文档和示例,我不得不在这里提出这个问题

谢谢。

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;

1 个答案:

答案 0 :(得分:1)

首先要了解foldl和foldr的工作方式非常重要,一旦完成,就可以将现有代码中的逻辑应用到函数中。

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

所以foldl(或foldr)将函数作为第一个参数。您可以使用某些sml定义的函数,如op >op +,但这些更简单的函数通常不会完成您希望它们执行的工作。

另一种方法是编写自己的匿名函数并将其作为参数传递,如上例所示。除了函数之外,foldl还有两个参数作为初始条件和列表。

foldl然后查看列表的索引并使用初始条件,将该索引应用于该函数。在我的示例中,a是由foldl传递给函数的列表索引,b是第一个调用(第一个索引)的表达式的结果。

foldl将继续为列表的其余部分执行此操作,将每个索引传递到a以及每次调用b中的函数的结果。

因此,使用a,您可以真正执行所需的任何计算,并使用b进行所需的任何比较。

我真的不想给你代码,但这里是is_member的设置。

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

您需要在比较和回报中使用a,b和x。由于这是一个布尔返回,因此您的初始条件应该为true或false。祝你好运。