Longest decreasing sequence in Lisp

后端 未结 3 913
情深已故
情深已故 2021-01-21 20:44

I\'m working on some problems for my upcoming exam and I need some help with this Lisp function. I\'m working in CLISP. I have to find the longest decreasing sequence comprised

3条回答
  •  迷失自我
    2021-01-21 21:04

    With contiguous subsequences, it's easy. Except I don't lisp, so I have to explain it in words.

    1. Call a recursive helper, with additional arguments a) longest found so far b) length of that c) current subsequence d) length thereof. Initially these are () 0 () 0.
    2. While head of list is even, recur on tail.
    3. Start current with the first odd encountered, recur on tail.
    4. If head is even, or head is not smaller than the previous element, the current sequence stops. Compare its length to the previously found longest sequence. If current is longer, it becomes the new longest, else forget current. Go to 2.

    When the end of the list is reached, the longer of the two remembered lists is the answer.

提交回复
热议问题