Haskell递归函数

发布于 2024-08-31 03:53:28 字数 229 浏览 6 评论 0原文

请帮助我编写一个函数,该函数接受两个参数:整数列表和索引(int),并返回表中指定索引位置上具有负值的整数列表。

该函数将具有此签名MyReverse :: [Int]->Int->[Int]

例如:myReverse [1,2,3,4,5] 3 = [1,2,-3,4,5]

如果索引大于列表的长度或小于0,则返回相同的列表。

Please help me writing a function which takes two arguments: a list of ints and an index (int) and returns a list of integers with negative values on specified index position in the table.

The function would have this signatureMyReverse :: [Int]->Int->[Int].

For example: myReverse [1,2,3,4,5] 3 = [1,2,-3,4,5].

If the index is bigger than the length of the list or smaller than 0, return the same list.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

甜味拾荒者 2024-09-07 03:53:28
myReverse :: [Int] -> Int -> [Int]
myReverse [] n = []
myReverse (x:xs) n
 | n < 0     = x:xs
 | n == 0    = (-x):xs
 | otherwise = x:(myReverse xs (n-1))

这是从 0 开始索引数组;您的示例从 1 建立索引,但对于 n == 0 的情况未定义。将其从 1 索引的修复应该相当明显:)

此外,您的大小写不一致; MyReversemyReverse 不同,只有后者作为函数才有效。

结果,在 GHCi 中:

*Main> myReverse [10,20,30,40,50] 0
[-10,20,30,40,50]
*Main> myReverse [10,20,30,40,50] 2
[10,20,-30,40,50]
*Main> myReverse [10,20,30,40,50] 3
[10,20,30,-40,50]
*Main> myReverse [10,20,30,40,50] 5
[10,20,30,40,50]
*Main> myReverse [10,20,30,40,50] (-1)
[10,20,30,40,50]

执行相同操作的更通用版本,使用 myReverse 的毫无意义的定义:

myGeneric :: (a -> a) -> [a] -> Int -> [a]
myGeneric f [] n = []
myGeneric f (x:xs) n
 | n < 0     = x:xs
 | n == 0    = (f x):xs
 | otherwise = x:(myGeneric f xs (n-1))

myReverse :: [Int] -> Int -> [Int]
myReverse = myGeneric negate
myReverse :: [Int] -> Int -> [Int]
myReverse [] n = []
myReverse (x:xs) n
 | n < 0     = x:xs
 | n == 0    = (-x):xs
 | otherwise = x:(myReverse xs (n-1))

That's indexing the array from 0; your example indexes from 1, but is undefined for the case n == 0. The fix to take it to index from 1 should be fairly obvious :)

Also, your capitalisation is inconsistent; MyReverse is different to myReverse, and only the latter is valid as a function.

Results, in GHCi:

*Main> myReverse [10,20,30,40,50] 0
[-10,20,30,40,50]
*Main> myReverse [10,20,30,40,50] 2
[10,20,-30,40,50]
*Main> myReverse [10,20,30,40,50] 3
[10,20,30,-40,50]
*Main> myReverse [10,20,30,40,50] 5
[10,20,30,40,50]
*Main> myReverse [10,20,30,40,50] (-1)
[10,20,30,40,50]

More generic version that does the same thing, using a pointless definition for myReverse:

myGeneric :: (a -> a) -> [a] -> Int -> [a]
myGeneric f [] n = []
myGeneric f (x:xs) n
 | n < 0     = x:xs
 | n == 0    = (f x):xs
 | otherwise = x:(myGeneric f xs (n-1))

myReverse :: [Int] -> Int -> [Int]
myReverse = myGeneric negate
梅倚清风 2024-09-07 03:53:28

myReverse :: [Int] ->整数-> [国际]
myReverse [] _ = []
myReverse 列表 n
|长度列表< n = 列表
myReverse (x:xs) n
|n == 0 = -x : myReverse xs (n-1)
|否则 = x : myReverse xs (n-1)

myReverse :: [Int] -> Int -> [Int]
myReverse [] _ = []
myReverse list n
|length list < n = list
myReverse (x:xs) n
|n == 0 = -x : myReverse xs (n-1)
|otherwise = x : myReverse xs (n-1)

烟织青萝梦 2024-09-07 03:53:28
myReverse :: [Int] -> Int -> [Int]
myReverse [] _ = []
myReverse list n
   |length list < n = list
myReverse (x:xs) n
   |n == 0 = -x : myReverse xs (n-1)
   |otherwise = x : myReverse xs (n-1)
   
myReverse :: [Int] -> Int -> [Int]
myReverse [] _ = []
myReverse list n
   |length list < n = list
myReverse (x:xs) n
   |n == 0 = -x : myReverse xs (n-1)
   |otherwise = x : myReverse xs (n-1)
   
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文