Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/haskell/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
List 使用高阶函数和Lambda演算操作Haskell中的列表_List_Haskell_Lambda_Higher Order Functions - Fatal编程技术网

List 使用高阶函数和Lambda演算操作Haskell中的列表

List 使用高阶函数和Lambda演算操作Haskell中的列表,list,haskell,lambda,higher-order-functions,List,Haskell,Lambda,Higher Order Functions,我正在尝试编写一个非常简单的函数,它接受一个列表(例如:[1,2,3,1,5]),并返回直接位于特定元素后面的元素列表 到目前为止,我取得的成果是: function element list = filter (\x -> element:x) list 我的期望输出: 函数1[1,2,3,1,5] =>[2,5] 试试这个 map snd $ filter ((== x) . fst) $ zip theList (tail theList) 这对空列表不起作用,您仍然需要额外的代

我正在尝试编写一个非常简单的函数,它接受一个列表(例如:
[1,2,3,1,5]
),并返回直接位于特定元素后面的元素列表

到目前为止,我取得的成果是:

function element list = filter (\x -> element:x) list
我的期望输出:

函数1[1,2,3,1,5]

=>[2,5]

试试这个

map snd $ filter ((== x) . fst) $ zip theList (tail theList)
这对空列表不起作用,您仍然需要额外的代码来处理它


这是怎么回事

首先,请注意,这些值从右向左流动。
($)
操作符允许这种情况发生。因此,计算的第一部分是
zip
函数

zip theList (tail theList)
对于上面的例子,这将产生

zip [1,2,3,1,5] [2,3,1,5]
相等

[(1,2), (2, 3), (3, 1), (1,5)]
这是列表中并发对的集合

接下来,应用过滤器

filter ((== x) . fst) $ ....
在英语中,这意味着只过滤出第一个元素等于
x
的并发对。输出是

[(1,2), (1,5)]
现在我们有了从1开始的并发对列表

最后,我们应用映射

map snd $ ....
这只会拉出该对的第二个值

map snd [(1,2), (1,5)] = [2,5]
这是所需的值


请注意,我上面关于在空列表中失败的评论

这是因为
tail
在空列表上崩溃

tail [] --error
有很多方法可以修补这种行为(例如,请参阅
safe
包),但目前主要是簿记,所以我将其留给您解决


还请注意,由于我们使用的所有函数都是惰性的,因此这种方法也适用于无限长的列表。

试试这个

map snd $ filter ((== x) . fst) $ zip theList (tail theList)
这对空列表不起作用,您仍然需要额外的代码来处理它


这是怎么回事

首先,请注意,这些值从右向左流动。
($)
操作符允许这种情况发生。因此,计算的第一部分是
zip
函数

zip theList (tail theList)
对于上面的例子,这将产生

zip [1,2,3,1,5] [2,3,1,5]
相等

[(1,2), (2, 3), (3, 1), (1,5)]
这是列表中并发对的集合

接下来,应用过滤器

filter ((== x) . fst) $ ....
在英语中,这意味着只过滤出第一个元素等于
x
的并发对。输出是

[(1,2), (1,5)]
现在我们有了从1开始的并发对列表

最后,我们应用映射

map snd $ ....
这只会拉出该对的第二个值

map snd [(1,2), (1,5)] = [2,5]
这是所需的值


请注意,我上面关于在空列表中失败的评论

这是因为
tail
在空列表上崩溃

tail [] --error
有很多方法可以修补这种行为(例如,请参阅
safe
包),但目前主要是簿记,所以我将其留给您解决



另外请注意,由于我们使用的所有函数都是惰性的,因此这种方法也适用于无限长的列表。

您可以通过简单的列表理解轻松做到这一点,例如:

successors xs i = [y | (x,y) <- zip xs (drop 1 xs), x == i]

xs i=[y |(x,y)您可以通过简单的列表理解轻松完成此操作,例如:

successors xs i = [y | (x,y) <- zip xs (drop 1 xs), x == i]

xsi=[y |(x,y)这将符合您的规范

next x (i:y:ys) -- look at the first two items in the list
    | x == i = -- if the first item == x, 
        y : next x (y:ys) -- take the second, and continue minus the first element
    |otherwise = -- not equal, 
        next x (y:ys) -- so skip that element
next _ [_] = [] -- if there's no second element, then stop
next _ _ = [] -- if the list is empty, stop

这将符合您的规格

next x (i:y:ys) -- look at the first two items in the list
    | x == i = -- if the first item == x, 
        y : next x (y:ys) -- take the second, and continue minus the first element
    |otherwise = -- not equal, 
        next x (y:ys) -- so skip that element
next _ [_] = [] -- if there's no second element, then stop
next _ _ = [] -- if the list is empty, stop

通过“next to”,我假设您的意思是“after”,对吗?没错;指定的元素direclty位于所需元素w/列表理解之前这是
fooaxs=[y |(x:y:|)通过“next to”,我假设您的意思是“after”,对吗?没错;指定的元素direclty位于所需元素w/列表理解之前这是
fooaxs=[y |(x:y:u)只要用
drop 1
替换
tail
,它也适用于空列表。只要用
drop 1
替换
tail
,它也适用于空列表。