Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/facebook/9.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列表';s_Haskell - Fatal编程技术网

Haskell列表';s

Haskell列表';s,haskell,Haskell,我想将列表中的每个项目与其他元素进行比较,例如 [[1,2,3], [0,2,2], [1,4,5], [3,1,1]] 将[1,2,3]与[0,2,2]进行比较,并应用一个运算(例如,距离公式“sqrt((x2-x1)^2+(y2-y1)^2)”和该运算的结果,用一个保护程序对其进行评估),然后将[1,2,3]与[1,4,5]进行比较,从而结束列表,然后用[0,2.2]与[1,4,5]等 我正在考虑使用(headi)和tail(headi)进行比较,但不知道如何继续迭代比较 你们能告诉我怎

我想将列表中的每个项目与其他元素进行比较,例如

[[1,2,3], [0,2,2], [1,4,5], [3,1,1]] 
将[1,2,3]与[0,2,2]进行比较,并应用一个运算(例如,距离公式“sqrt((x2-x1)^2+(y2-y1)^2)”和该运算的结果,用一个保护程序对其进行评估),然后将[1,2,3]与[1,4,5]进行比较,从而结束列表,然后用[0,2.2]与[1,4,5]等

我正在考虑使用(headi)和tail(headi)进行比较,但不知道如何继续迭代比较

你们能告诉我怎么做吗?多谢各位

编辑

我需要的是,对于第一个列表,我需要根据距离公式制作另一个列表,并比较列表的第三个元素,例如

    [[1,2,3], [0,2,2], [1,4,5], [3,1,1]] 

     [x1,y1,z1], [x2,y2,z2]
    sqrt ((x2-x1)^2+(y2-y1)^2)) if result_of_sqrt < z1 then 1:[do the same thing with the other element]                    
else 0:[do the same thing with the other element]

sqrt ((0-1)^2+(2-2)^2) ) = 1, 1 < 3 => 1:(compare this two elements [1,2,3],[1,4,5]) and so...
[[1,2,3],[0,2,2],[1,4,5],[3,1,1]]
[x1,y1,z1],[x2,y2,z2]
sqrt((x2-x1)^2+(y2-y1)^2))如果_sqrt的结果_1:(比较这两个元素[1,2,3],[1,4,5]),然后。。。

这个问题确实不清楚,但从根本上说,您希望获取列表中的每个元素,并将其与列表中的所有其他元素进行比较。假设我们希望将
[1..3]
中顺序无关紧要的所有元素配对,即我们希望列表:

`[(1, 2), (1, 3), (2, 3)]`
我们可以直接这样做:

pairAll :: [a] -> [(a, a)]
pairAll [] = []
pairAll (x:xs) = map (\y -> (x, y)) xs ++ pairAll xs
现在,
pairAll[1..3]=[(1,2)、(1,3)、(2,3)]
根据需要。我们可以将配对函数分解为:

doStuffToAll :: (a -> a -> b) -> [a] -> [b]
doStuffToAll _ [] = []
doStuffToAll f (x:xs) = map (f x) xs ++ doStuffToAll f xs
然后
pairAll=dostuftoall(\x y->(x,y))


将lambda表达式替换为列表的比较函数(即,
dostufwithall compareLists
),如果我正确理解了您的问题,应该可以这样做。

我不确定我是否正确理解了您的意思,但这可能会帮助您解决一些问题:

  • 把所有的元组配对
  • 将“比较”函数应用于这些元组并输出真/假

  • 这似乎产生了您的最后一个示例结果:

    f xs = map (\x -> map (test x) xs) xs
      where test a@[x1,y1,z1] b@[x2,y2,z2] = 
              if a == b 
                 then 0 
                 else if sqrt ((x2 - x1) ^ 2 + (y2 - y1) ^ 2) < z1 
                         then 1 
                         else 0
    
    输出:

    *Main> f [[0,0,4], [2,4,2], [1,3,5], [3,1,1]]
    [[0,0,1,1],[0,0,1,0],[1,1,0,1],[0,0,0,0]]
    

    这一描述不清楚。您希望得到什么输出?在操作和保护之后,我将根据结果创建一个列表,其中包含0和1,如[1,0,1],[0,0,0],[1,1,0],[1,0],[1,0,1]问题陈述中的类型签名错误。可能是
    [[Int]]->[[Int]]
    。您好,谢谢您的回答,我刚刚编辑了解释问题的原始帖子!谢谢有没有一种方法可以用警卫来代替if-else?@user3806679当然可以,为什么不呢?请参阅已编辑的答案。您可以使用嵌套的
    where
    修饰表达式:
    …|m
    f xs = map (\x -> map (test x) xs) xs
      where test a@[x1,y1,z1] b@[x2,y2,z2] = 
              if a == b 
                 then 0 
                 else if sqrt ((x2 - x1) ^ 2 + (y2 - y1) ^ 2) < z1 
                         then 1 
                         else 0
    
    f xs = map (\x -> map (test x) xs) xs
      where test a@[x1,y1,z1] b@[x2,y2,z2] 
              | a == b    = 0 
              | m < z1    = 1 
              | otherwise = 0
        where m = sqrt ((x2 - x1) ^ 2 + (y2 - y1) ^ 2)
    
    *Main> f [[0,0,4], [2,4,2], [1,3,5], [3,1,1]]
    [[0,0,1,1],[0,0,1,0],[1,1,0,1],[0,0,0,0]]