-1
목록이 주어지면 같은 요소를 그룹화하는 목록으로 목록을 분할해야합니다.구성표의 목록에 그룹 번호가 있습니까?
예: 수 ((37 37) (39 39) (38 38 38) (40 40))
'(37 37 39 38 38 39 38 40 40 38)에 발생하는' 누구든지 이걸 도와 줘?
목록이 주어지면 같은 요소를 그룹화하는 목록으로 목록을 분할해야합니다.구성표의 목록에 그룹 번호가 있습니까?
예: 수 ((37 37) (39 39) (38 38 38) (40 40))
'(37 37 39 38 38 39 38 40 40 38)에 발생하는' 누구든지 이걸 도와 줘?
여기를 수행하는 한 방법이다 :
(define (group lst)
(let iter ((lst lst) (found null) (res null))
(if (null? lst) ; all done ...
(reverse res) ; ... return result (reversed)
(let ((c (car lst))) ; isolate first element
(if (member c found) ; already processed that
(iter (cdr lst) found res) ; just go on with the next
(iter (cdr lst) ; otherwise add this to found and create a list of as many elements ...
(cons c found) ; ... as found in the list to the current result ...
(cons (make-list (count (curry equal? c) lst) c) res))))))) ; ... and go on with the next
테스팅
> (group '(37 37 39 38 38 39 38 40 40 37))
'((37 37 37) (39 39) (38 38 38) (40 40))
> (group '(37 37 39 38 38 39 38 40 40 38))
'((37 37) (39 39) (38 38 38 38) (40 40))
결과이어야 '((37 37) (39 39) (38 38 38) (40 40)) – uselpa
[group-by] (https://docs.racket-lang.org/reference/pairs.html?q=group#%28def._%28%28lib._racket%2Flist..rkt%29)를 사용할 수 있습니다. _group-by % 29 % 29) :'(그룹 별 ID '(37 37 39 38 38 39 38 40 40 38))'. – assefamaru