Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
419 views
in Technique[技术] by (71.8m points)

haskell - 如何从每个元素与另一个元素完全配对的列表中提取元组列表?(How to extract a list of tuple from a list with each element paired with exactly one other element?)

I am new to Haskell and I got a question.

(我是Haskell的新手,我有一个问题。)

How can I write a function that takes an even length list of values and returns a list of tuples?

(我该如何编写一个函数,该函数采用一个偶数长度的值列表并返回一个元组列表?)

Each element in the list should be paired with exactly one other element.

(列表中的每个元素都应与另一个元素完全配对。)

For example : [1, 2, 3, 4] would give [(1,2),(3,4)] or [(1,3),(2,4)]or[(1,4),(2,3)]

(例如:[1、2、3、4]将给出[(1,2 ,,(3,4)]或[(1,3),(2,4)]或[(1,4),( 2,3)])

  ask by lynn translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

StackOverflow is not a service where people will just write code for you.

(StackOverflow不是人们只为您编写代码的服务。)

You have to show your attemts or any effort that you applied to solve the task and demonstrate us problems that you've encountered on that way.

(您必须展示自己为解决该任务而做出的努力或付出的努力,并向我们展示您在此过程中遇到的问题。)

If you just ask people to solve your task, they'll get angry at you (-3 score of this question states exactly that), and your question will be closed.

(如果您只是要求别人解决您的任务,他们会生您的气(这个问题的分数为-3恰恰说明了这一点),您的问题将被关闭。)

So make sure you demonstrate enough effort in solving a task when asking about it here.

(因此,在此处询问任务时,请确保您表现出足够的精力来解决任务。)

BTW, I'll give you a hint:

(顺便说一句,我给你一个提示:)

pairUp :: [a] -> [(a, a)]
pairUp [] = [] -- obvious case
pairUp [x] = error "Odd-length list!"
pairUp (x:y:xs) = ...

You have to complete the last case.

(您必须完成最后一个案例。)

Here you have first two elements x and y , and also the tail list of all other elements.

(在这里,您有前两个元素xy ,还有所有其他元素的尾部列表。)

You have to combine the first two elements in a pair and also process the tail in the same way.

(您必须将成对的前两个元素组合在一起,并以相同的方式处理尾部。)

You will need an understanding recursion to accomplish the task.

(您将需要理解递归才能完成任务。)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...