3

I have this Haskell question as homework, and the details are a little complicated. I'd just like to clarify what I am trying to do in this question:

Define the function select which takes a list of integers and returns the list whose head is the smallest element in the list and whose tail is the result of recursively sorting the list obtained by deleting the smallest element of the list from the list.

Does this mean something like "the first element of the list is the smallest, and the last element of the list is the int just bigger than the smallest and smaller than everything else"?

For example if I have this List:

[ 2, 3, 4, 6, 8, 7]

,should the answer be

[2, 4, 6, 8, 7, 3]

or

[2, 4, 6, 7, 8, 3]

?

2
  • 1
    Just to know, which school or university give Haskell homework? Commented Mar 31, 2011 at 21:46
  • University of Alberta teaches Haskell and scheme in it's 3rd year functional programming course Commented Mar 31, 2011 at 21:59

4 Answers 4

2

What you want is: Given a list [5 3 9 8 1], you want the following:

  • A list where the head is the smallest (1), and the tail is the result of sorting the rest of the list ([5 3 9 8]).
  • The tail of the original list has the head as the smallest (3), and the tail is the result of sorting [5 9 8]
  • And so on.
Sign up to request clarification or add additional context in comments.

2 Comments

so it is like [1, 3, 5, 8, 9] at the end just like Iarsmans said below right?
Yes, but the assignment is to get [1, 3, 5, 8, 9] in a certain way, using recursion.
2

It means that you should get the sorted list [2, 3, 4, 6, 7, 8]. The expected answer is not the list, but the implementation. The question tells you how the implementation should work, namely by extracting the smallest element, then calling itself for sorting the remainder, and finally glueing things together.

Comments

1

"the list whose head is the smallest element in the list"

Ok, that's easy

and whose tail is the result of recursively sorting the list obtained by deleting the smallest element of the list from the list.

So the result should be sorted: [2, 3, 4, 6, 7, 8].

(Your professor wants you to implement selection sort.)

Comments

0

It seems that the question is asking you to sort an array in ascending order (e.g. sort([5,4,3]) ==> [3,4,5]).

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.