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

SML/NJ要求修改我的代码以使用fldr、fldl和map,sml,smlnj,ml,Sml,Smlnj,Ml,我被要求修改我编写的代码,以便在大多数函数中我必须使用fldr或fldl 是包含测试用例的描述 我想知道,如果您能给我一些提示,我应该在哪里修改以使用这些函数,那将是非常棒的 由于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

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

是包含测试用例的描述

我想知道,如果您能给我一些提示,我应该在哪里修改以使用这些函数,那将是非常棒的

由于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;

首先了解foldl和foldr是如何工作的很重要,一旦了解了,就可以将现有代码中的逻辑应用到函数中

foldl(fn(a,b)=>(*表达式*)*初始条件**列表*

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

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

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

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

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

我真的不想给你代码,但这是is_成员的设置

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。祝你好运。

我必须补充一点,上面的代码工作得很好,但我需要修改它,以便它使用这些函数