我是标准 ML 的新手,我有一个问题,对于知道如何使用这种语言进行编码的人来说,这可能是显而易见的。
我有一个原始函数如下:
fun getBlocked w =
case BlockingMgr.listBlockedSuccessors w
of nil => ""
| ws =>
concat (
List.map
(fn (r, w') => (
"v r1 w"
^
Int.toString (Node.getId w))
^ " w"
^ (Int.toString (Node.getId (w')))
^ "\n"
)
ws
)
这会返回一个非常适合在我的程序中显示的字符串。
不幸的是,我还需要这个字符串中 '\n' 的数量。因此,我考虑制作另一个返回整数的函数,而不是直接从字符串计数(这会很好):
fun getNbBlocked w =
let res = 0;
case BlockingMgr.listBlockedSuccessors w
of nil => res
| ws =>
List.map
(fn (r, w') => (
res = res+1
)
ws
但我的大脑过于专注于过程/对象思考,我不知道如何用函数式语言执行我想要的。
如果有人可以帮我调试这个函数,因为我真的不知道问题出在哪里:/(或者更好的是,通过告诉我如何编写一个函数来计算'\n'的数量来帮助我现有的字符串)
在此先感谢您的帮助!
最好的问候!
编辑
@molbdnilo 你的解决方案似乎很接近解决方案,但我没有设法使用它:/(不幸的是我是一个真正的初学者)。
我有这些功能(已经存在的和你的):
val res = 0
fun getBlocked w =
case BlockingMgr.listBlockedSuccessors w
of nil => ""
| ws =>
concat (
List.map
(fn (r, w') => (
"v r1 w"
^
Int.toString (Node.getId w))
^ " w"
^ (Int.toString (Node.getId (w')))
^ "\n"
)
ws
)
fun count c s = List.length (List.filter (fn x => x = c) (String.explode s));;
fun count_newlines s = count #"\n" s;;
fun af w = print(getBlocked w)
fun aff w = count_newlines(getBlocked w)
当我调用函数 getBlocked() 以显示创建的字符串时
我正在做如下:
app af (Nodestore.listNodes ())
因为是调用print,所以可以在任何地方调用。
但是对于你的函数,我需要使用它的返回值,但我没能做到:/
我尝试类似的东西:
res = (app aff Nodestore.listNodes())
(* We then, display the number of edges *)
print (Int.toString (res));
但不幸的是,它并不像我想的那么简单:/
我收到错误信息:
Error: src/output/modeloutput.sml 101.11.
Function applied to incorrect argument.
expects: _ -> [unit]
but got: _ -> [int]
in: app aff
Error: src/output/modeloutput.sml 101.11.
Function applied to incorrect argument.
expects: [Node.node list]
but got: [unit -> Node.node list]
in: (app aff) Nodestore.listNodes
Error: src/output/modeloutput.sml 101.11.
Function not of arrow type.
function: [unit]
in: ((app aff) Nodestore.listNodes) ()
pre codegen raised in 2.79 + 1.50 (35% GC)
pre codegen raised: Fail
Compile SML raised in 2.79 + 1.50 (35% GC)
Compile SML raised: Fail
最佳答案
您的函数的主要问题是 res = res + 1
是比较,而不是赋值。
你需要忘记assignment,甚至忘记这样的事情存在。
这是一种做你想做的事的方法:
- 将字符串转化为字符列表
- 过滤它,只保留你感兴趣的字符
- 取结果长度
看起来像这样:
fun count c s = List.length (List.filter (fn x => x = c) (String.explode s))
fun count_newlines s = count #"\n" s
关于algorithm - SML : Count the number of '\n' in a String,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34629215/