我目前在学习Haskell时很困难。
我花了近6个小时来编写一个符合要求的函数。但不幸的是,我对它的外观并不满意。
请问有没有人能给我一些提示如何重写它?
get_connected_area :: Eq generic_type => [[generic_type]] -> (Int, Int) -> [(Int,Int)] -> generic_type -> [(Int,Int)]
get_connected_area habitat point area nullValue
| elem point area = area
| not ((fst point) >= 0) = area
| not ((snd point) >= 0) = area
| not ((fst point) < (length habitat)) = area
| not ((snd point) < (length (habitat!!0))) = area
| (((habitat!!(fst point))!!(snd point))) == nullValue = area
| otherwise =
let new_area = point : area
in
get_connected_area habitat (fst point+1, snd point) (
get_connected_area habitat (fst point-1, snd point) (
get_connected_area habitat (fst point, snd point+1) (
get_connected_area habitat (fst point, snd point-1) new_area nullValue
) nullValue
) nullValue
) nullValue
该函数接收一个通用类型的参数(表示一个地形图),并搜索与给定nullValue不相等的点周围的完全连接区域。
例如:
如果以以下方式调用该函数:
get_connected_area [[0,1,0],[1,1,1],[0,1,0],[1,0,0]] (1,1) [] 0
这实际上意味着
0 1 01 1 1
0 1 0
1 0 0
表示一个地图(类似于谷歌地图)。从坐标(1,1)开始,我想获取所有与给定点形成连接区域的元素的坐标。
因此,结果应该是:
0 1 0
1 1 1
0 1 0
1 0 0
以及相应的返回值(粗体1的坐标列表):
[(2,1),(0,1),(1,2),(1,0),(1,1)]