sml - 如何返回一些列表而不是普通列表?
问题描述
我正在尝试实现一个程序,该程序接受一个字符串和一个列表,NONE
如果找不到任何匹配项则返回,如果找到则返回没有元素的列表。
fun all_except_option ("string",["he","she","string"]) = SOME["he","she"]
我设法让它工作,但没有选项类型,我不知道如何让它返回SOME list
一个普通列表。
fun all_except_option(str,lst)=
case lst of
[] => []
| x::lst' => if same_string(x,str) = false
then let fun append (word, list) = word::list
in append(x,[]) :: all_except_option(str,lst')
end
else all_except_option(str,lst')
解决方案
谢谢你。我设法让它工作,但我仍然不明白“其他情况”以及我的程序如何处理它。这是工作代码。如果你能解释一下“else case all_except_option(str,list') of”,我会很高兴。
fun all_except_option(str,list)=
case list of
[] => NONE
| x::list' => if same_string(x,str) then
SOME( list')
else case all_except_option(str,list') of
NONE=>NONE
| SOME list'=>SOME(x::list')
推荐阅读
- angular - ng test 和 ng serve 失败并显示“发生未处理的异常:无效或意外的令牌”
- sql - SQL - 13 个数字列的组合如何等于 1 列
- javascript - 如何使用ajax get rest api从aws s3获取对象
- python - 如何使用 Selenium 和 Python 从通过 xpath 找到的 webdriver 元素中提取文本
- javascript - 如何仅从第一页和最后一页删除按钮?
- javascript - 从 html 模板字符串中删除额外的元素
- model-view-controller - MVC 文件夹结构和用例中操作的最佳实践
- sql-server - 用 ssis 中的用户变量覆盖表达式中的 zip 文件
- php - 防止在转换时在 XML 文件中添加 LF
- c# - 如何在 OpenID-Connect OWIN MVC 中重定向之前执行操作