我是Haskeller的开始.这是一个我认为需要几分钟才能构建的脚本,但这给我带来了相当大的困难.
假设我们有一个由节点和边组成的图.数据结构是节点到节点对的列表,如下所示:
[(1,6),(1,8),(6,9),(6,10),(6,13),(8,13)]
Run Code Online (Sandbox Code Playgroud)
我想构建一个遍历图形的函数,并显示从起始节点到底部所有可到达节点的所有可能路径.
因此,函数的几个理想执行可能如下所示:
> allPaths 1 [(1,6),(1,8),(6,9),(6,10),(6,13),(8,13)]
[[1,6,9],[1,6,10],[1,6,13],[1,8,13]]
> allPaths 8 [(1,6),(1,8),(6,9),(6,10),(6,13),(8,13)]
[[8,13]]
Run Code Online (Sandbox Code Playgroud)
这是我刚开始构建路径列表的初始尝试:
allPaths start graph = [start : [snd edge] | edge <- graph, fst edge == start]
> allPaths 8 [(1,6),(1,8),(6,9),(6,10),(6,13),(8,13)]
[[1,6],[1,8]]
Run Code Online (Sandbox Code Playgroud)
问题是我不知道如何使这个解决方案使用递归来完成路径.这是几个没有通过类型检查的蹩脚尝试之一:
allPaths start graph = [start : (snd edge : allPaths (snd edge) graph) | edge <- graph, fst edge == start]
Occurs check: cannot construct the infinite type: a ~ [a]
Expected type: [a]
Actual type: [[a]]
Relevant bindings include
edge :: …Run Code Online (Sandbox Code Playgroud) 我有一个医疗报告项目,需要从诊断日期列表中计算"开始"日期.
患者第一次出现某种诊断,即发病日期为#1.在接下来的30天内,任何后续诊断日期都计入该发病次数的一部分,因此可以删除后续日期.
30天过后,下一个诊断日期计为起始#2.
等等..
示例输入
2015-09-10 (this is onset #1)
2015-09-19 (within 30 days from onset #1, so drop)
2015-09-29 (within 30 days from onset #1, so drop)
2015-10-17 (>= 30 days from onset #1, so this is onset #2)
2015-10-19 (within 30 days from onset #2, so drop)
2015-11-13 (within 30 days from onset #2, so drop)
2015-11-29 (>= 30 days from onset #2, so this is onset #3)
Run Code Online (Sandbox Code Playgroud)
示例输出
2015-09-10 (onset #1)
2015-10-17 (onset #2)
2015-11-29 (onset #3) …Run Code Online (Sandbox Code Playgroud)