sml - ML : 匹配非穷举
问题描述
我想让函数命名为headcol
:
headcol[[1,2],[3,4]] = [1,3];
所以我做了这样的功能:
fun headcol [] = []
| headcol [x::xs',y::ys'] = [x,y]
但是当我调用它时,我会得到一个非穷尽的匹配。
解决方案
IIUC,headcol
将提取参数中的所有列表头,其类型为'a-list-list
. 你的数学只是[]
and [x::xs', y::ys']
,而不是其他任何东西。因此,如果您的论点有 2 个以上的子列表,则 execption 将引发:
- headcol[[1,2],[3,4], [4, 5, 9]];
uncaught exception Match [nonexhaustive match failure]
raised at: a.sml:9.34
-
如果你只想处理两个元素的列表,pair 是更好的选择。否则,您应该匹配更多案例:
fun headcol list =
case list of
[] => []
| x::xs' =>
case x of
[] => headcol xs'
| h::t => h::(headcol xs')
输出:
- headcol[[1,2],[3,4], [4, 5, 9]];
- val it = [1,3,4] : int list
推荐阅读
- amazon-web-services - aws 转录说话人识别不准确。如何改进它?如果无法改进,还有哪些其他最佳选择?
- nest - 使用 Nest 插入数据后的文档计数为 0
- c++ - 使用rocksdb Get() API 将值读入用户提供的预分配缓冲区
- php - Wordpress 自定义帖子类型永久链接在高级自定义字段帖子对象字段中不起作用
- javascript - 使用 axios/axios-auth-refresh 的 JWT 刷新令牌流问题
- oracle - Oracle DB - 从 19c 客户端连接 11gR2 DB - ORA-12650:没有通用加密或数据完整性算法
- javascript - JavaScript:如何根据第一个零出现在非零整数之后的位置将十进制数分成两部分
- ios - “Pods-Runner”目标具有传递依赖关系,包括静态链接的二进制文件
- python - 在 digitalocean 和 django 上避免 502
- module - 在 Windows 7 上找不到 File::Find Raku