Advertisement
matthammy

depth-replicate

Feb 26th, 2023
863
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Racket 0.60 KB | None | 0 0
  1. #lang racket
  2.  
  3. (define (clone a n)
  4.   (if (eq? n 0) '() (cons a (clone a (- n 1)))
  5.       )
  6.   )
  7.  
  8. (define (get-depth lst n)
  9.   (cond
  10.     ((null? lst) '())
  11.     ((list? (car lst)) (cons (get-depth (car lst) (+ n 1)) (get-depth (cdr lst) n)))
  12.     (else (cons n (get-depth (cdr lst) n)))
  13.     )
  14.   )
  15.  
  16. (define (traverse lst1 lst2)
  17.   (cond
  18.     ((null? lst1) '())
  19.     (else (cons (clone (car lst2) (car lst1)) (traverse (cdr lst1) (cdr lst2)))
  20.   )
  21.   )
  22.  )
  23.  
  24. (define (depth-replicate lst)
  25.   (flatten (traverse (flatten (get-depth lst 1)) (flatten lst)))
  26.   )
  27.  
  28. (display (depth-replicate '((((3 2))) ((1)) (1))))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement