Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/haskell/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Haskell上的广度优先搜索_Haskell_Breadth First Search - Fatal编程技术网

Haskell上的广度优先搜索

Haskell上的广度优先搜索,haskell,breadth-first-search,Haskell,Breadth First Search,图的定义如下:[(“1”,“2”,“3”),(“2”,“1”),(“3”,“1”)] 我想写一篇广度优先的搜索 这是我的密码: search_command graph start ""=(graph,"You didnt enter start vertex!\n") search_command graph "" end=(graph,"You didnt enter end vertex!\n") searc

图的定义如下:[(“1”,“2”,“3”),(“2”,“1”),(“3”,“1”)]

我想写一篇广度优先的搜索

这是我的密码:

search_command graph start ""=(graph,"You didnt enter start vertex!\n")
search_command graph ""  end=(graph,"You didnt enter end vertex!\n")
search_command graph start end=
    if (has_element graph start)&&(has_element graph end) then 
            (graph,unwords (search graph [start] end [] []))
    else if (has_element graph end)==False then 
            (graph,"Element with "++end++" name not found!")
    else 
            (graph,"Element with "++start++" name not found!")


search [] _ _ [] result=result 
search (item:graph) (x:start) end use result=
    if (fst item==x)&&(elem x use)==False then
            search graph (get_connect_vertices graph graph (fst item) []) end (use++[x]) (result++[x])
    else if (fst item==end)&&(fst item==x) then
            search [] [] "" [] (result++[x]++[end])
    else
            search graph [x] end use (result)
但当我运行它时,我得到了豁免: 异常:D:\Workspace\Labwork2\src\Main.hs:(190,1)-(197,49):函数搜索中的非穷举模式


我的错误是什么?如果像我一样给出图形,如何实现广度优先搜索?你没有理由这样做

search [item] [] _ _ _

很明显,你的程序正在解决这个问题。您是否有一个不变量说明,如果图形为空,那么开始符号将用尽?

正如例外情况所示,您的模式是非用尽的。具体来说,您可以使用以下模式进行
搜索

search []    _     _ [] _
search (_:_) (_:_) _ _  _
如果第一个参数为零,则第四个参数也必须为零;如果第一个参数不是零,则第二个参数也必须不是零。以下情况不包括在内:

search [] _ _ (_:_) _
search (_:_) [] _ _ _
您必须强制执行不变量以确保这些情况永远不会发生,或者您应该对它们采取合理的措施

通过
-Wall
运行您的程序应该有助于您捕获此类非总体性问题

(至于广度优先搜索:在Haskell中,如果您编写了一个正确的图形搜索,其中顶级结果不依赖于较低级别的结果,并且您不要求严格,那么结果是广度优先还是深度优先,或者介于两者之间,通常取决于结果的使用方式。)

(这是不相关的,但是有一个由空字符串标识的顶点真的是一个问题吗?如果你的图有一个由空字符串标识的顶点怎么办?是否有一个不变量表明这从来都不是问题?)