I'm new to Haskell, so bear with me.
我是Haskell的新手,所以请耐心等待。
Is it possible to remove the first element of a list within a tuple?
是否可以删除元组中列表的第一个元素?
So if we have something like:
所以,如果我们有类似的东西:
function:: [([x], y)] -> [([x], y)]
Could we edit the list so that the first element of the list gets deleted? So that tuples like:
我们可以编辑列表,以便删除列表的第一个元素吗?这样的元组就像:
[([1,2,3], 4), ([5,6,7], 8), ([9,10,11,12], 13), ([14,15], 16)]
become
[([2,3], 4), ([6,7], 8), ([10,11,12], 13), ([15], 16)]
?
Or is this not possible and am I wasting my time trying to figure this out?
或者这是不可能的,我是否在浪费时间试图解决这个问题?
1 个解决方案
#1
Start by writing the simpler function ([x],y) -> ([x],y)
. Something like this would work:
首先编写更简单的函数([x],y) - >([x],y)。像这样的东西会起作用:
f ((x:xs), y) = (xs, y)
You'll need to decide what to do when the list is empty. One option is to return the empty list again:
当列表为空时,您需要决定要做什么。一种选择是再次返回空列表:
f ([], y) = ([], y)
The other alternative is to produce an error as the tail
function does in which case you could just use that for your entire implementation.
另一种方法是产生一个错误,因为tail函数可以在这种情况下将它用于整个实现。
f (xs, y) = (tail xs, y)
Next you can use the higher order function called map
to get your final function. It's type is (a -> b) -> [a] -> [a]
. If you pass f
as the first argument then a
and b
are both ([x],y)
.
接下来,您可以使用名为map的高阶函数来获取最终函数。它的类型是(a - > b) - > [a] - > [a]。如果你传递f作为第一个参数,那么a和b都是([x],y)。
#1
Start by writing the simpler function ([x],y) -> ([x],y)
. Something like this would work:
首先编写更简单的函数([x],y) - >([x],y)。像这样的东西会起作用:
f ((x:xs), y) = (xs, y)
You'll need to decide what to do when the list is empty. One option is to return the empty list again:
当列表为空时,您需要决定要做什么。一种选择是再次返回空列表:
f ([], y) = ([], y)
The other alternative is to produce an error as the tail
function does in which case you could just use that for your entire implementation.
另一种方法是产生一个错误,因为tail函数可以在这种情况下将它用于整个实现。
f (xs, y) = (tail xs, y)
Next you can use the higher order function called map
to get your final function. It's type is (a -> b) -> [a] -> [a]
. If you pass f
as the first argument then a
and b
are both ([x],y)
.
接下来,您可以使用名为map的高阶函数来获取最终函数。它的类型是(a - > b) - > [a] - > [a]。如果你传递f作为第一个参数,那么a和b都是([x],y)。