开发者

Sort two list with the elements in an increasing order

开发者 https://www.devze.com 2023-03-11 11:53 出处:网络
The question requires me to Complete the Scheme function merge, which consume开发者_JAVA百科s two lists of sorted numbers (in an increasing order) and produces a list of numbers which consists of all

The question requires me to Complete the Scheme function merge, which consume开发者_JAVA百科s two lists of sorted numbers (in an increasing order) and produces a list of numbers which consists of all the two consumed lists in sorted order.

For example,

(merge (list 1 4 5 9) (list -1 2 4)) => (list -1 1 2 4 4 5 9)
(merge (list 1 4 5 9) empty) => (list 1 4 5 9)
(merge empty (list 1 4 5 9)) => (list 1 4 5 9)
(merge empty empty) => empty

Thanks for helping out!!


Since this smells like homework, I won't write any code, but I will tell you that what are doing is part of the merge sort algorithm. Remember these two things:

  • In functional languages like Scheme, you are asking the question what value do I need to produce rather than what do I need to do
  • In Scheme, you often write more than one procedure to accomplish a single task

If you remember these two things and figure out which part of merge sort you need to implement, it should become fairly easy to figure out.

0

精彩评论

暂无评论...
验证码 换一张
取 消