Question: Hi i need help correcting my code. I cant seem to do the right thing to produce what the check expects shows. It i in

Hi i need help correcting my code. I cant seem to do the right thing to produce what the check expects shows. It i in DrRacket. Thanks!

;; Design a function called longest-sequence that consumes a list of integers ;; and produces the longest sequence of consective integers that are each one ;; larger than the previous integer. All of the following are examples of ;; sequences of consecutive integers: ;; ;; (list 1 2 3 4) ;; (list 2) ;; empty ;; (list -5 -4) ;; ;; Your solution MUST BE TAIL-RECURSIVE. ;; ;; If there are multiple sequences of the same length in the input list, then ;; you must produce the first one (the left-most one) in the list. ;; ;; For example: ;; (longest-sequence (list 8 9 1 8 6 7)) must produce (list 8 9) ;; (longest-sequence (list 8 7 8 -3 -2 -1 5)) must produce (list -3 -2 -1) ;; ;; You MAY want to call length, and if so that is fine, but it is not required. ;; You MAY also want to handle the empty list case in the trampoline. ;; ;; You must include all relevant design recipe elements. ;; ;; As always, a file that does not run or that produces errors when run will ;; lose a significant number of marks. A file with failing tests is much ;; less bad. Run your work often, so that you can check and fix errors as ;; soon as they creep in. Also be sure to run every time before you submit. ;;

(@htdf longest-sequence) (@signature (listof Integer) -> (listof Integer)) (check-expect (longest-sequence empty) empty) (check-expect (longest-sequence (list 8 9 1 8 6 7)) (list 8 9)) (check-expect (longest-sequence (list 8 7 8 -3 -2 -1 5)) (list -3 -2 -1))

(@template (listof Integer) accumulator)

(define (longest-sequence loi) (local [(define (longest-sequence loi acc prev) (cond [(empty? loi) acc] [else (if (= (first loi) (+ prev 1)) (longest-sequence (rest loi) (cons (first loi) prev) (first loi)) (cons (first loi) acc))]))] ;(longest-sequence (rest loi)))]))] (longest-sequence loi '() (first loi))))

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!