Haskell 1级问题,感谢您的答案
我是Haskell的新手,因此需要一些帮助,我想对你们很容易。 我正在尝试制作一个与使用“!”相同的函数。对于索引,但索引应从列表的长度从1变为。 这是我到目前为止的函数“ numberSlist”和MAIN函数“ Ele”
numberList :: [a] -> [(a, Int)]
numberList x = zip x [1..10]
ele :: Int -> [a] -> a
最终结果应该看起来像:
ELE 1 [1,2,3] = 1
ELE 2 [3,4,5,6] = 4
I I使用模式匹配尝试了一些不同的事情,但似乎不起作用。 我从这里去哪里?
I am new to Haskell and therefore needs some help which i guess is easy for you guys.
I am trying to make a function that does the same as using "!!" for indexes, but the indexes should go from 1 to the length of the list.
This is what i have so far, help function "numberList" and main function "ele"
numberList :: [a] -> [(a, Int)]
numberList x = zip x [1..10]
ele :: Int -> [a] -> a
End result should look like:
ele 1 [1,2,3] = 1
ele 2 [3,4,5,6] = 4
I have tried some different things with pattern matching, but it doesn't seem to work.
Where do i go from here?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
考虑您的两个示例:
在第一种情况下,由于数字为
1
,因此您返回了列表的头部。 简单。,因此我们需要将
2
1
获得。非常简单,从中减去1
。但是,您还需要将
4
转到列表的头部。ELE 1 [4,5,6]
将为您提供所需的结果。此递归可能看起来如下,但是为了不为您做作业,我会让您填写空白。Consider your two examples:
In the first case, because the number was
1
, you returned the head of the list. Simple.So we need to get that
2
to1
. Pretty easy, subtract1
from it.But you also need to get
4
to the head of the list.ele 1 [4, 5, 6]
would handily give you the result you're looking for. This recursion might look like the following, but in the interest of not doing your homework for you, I'll let you fill in the blanks.