我是方案语法的新手。这是我一直在做的项目的最后一部分。我能够从给定的 Collatz 序列中找到最大值,但是项目的这一部分需要从多个 Collatz 序列列表中找到最大长度。因此,例如给出这个列表:'((1 10)(10 200)(201 210)(900 1000),输出应该是这样的:'(20 125 89 174)我需要找到数字之间的最大长度1 到 10 然后从 10 到 200 ets 这是我的代码:
#lang racket
; Part I
(define (sequence n)
(cond [(= n 1)
(list n)]
[(even? n)
( cons n(sequence( / n 2)))]
[(odd? n)
( cons n(sequence (+(* n 3) 1))) ] ))
(sequence 10)
; Part II
(define (find-length items)
(if (null? items)
(list )
(cons
(length (sequence(car items)))
(find-length (rest items))))
)
(find-length (list 10 16 22 90 123 169))
;Part III
(define max-in-list (lambda (ls)
(let ( (head (car ls)) (tail (cdr ls)))
(if (null? tail)
; list contains only one item, return it
head
; else find largest item in tail
(let ((max-in-tail (max-in-list tail)))
; return the larger of 'head' and 'max-in-tail'
(if (> head max-in-tail)
head
max-in-tail
)
)
)
)
))
(define (find-max i j)
( if (= i j)
(list)
(cons
(max-in-list (find-length(sequence i)))
(find-max (+ 1 i ) j)
))
)
(max-in-list(find-max 1 10))
(define (max-length-list items )
(if (null? items)
(list)
(cons
(find-max ? ?) ) ; how i can call this function ?
(max-length-list (?) ) ; how i can call this function ?
)))
(max-length-list '((1 10) (10 200) (201 210) (900 1000) ))