solve: 88. Merge two sorted list in Racket

This commit is contained in:
Samuel Ortion 2025-10-31 23:09:37 +01:00
parent 2f30a4c037
commit 0af3f8403a
Signed by: sortion
GPG Key ID: 9B02406F8C4FB765
1 changed files with 20 additions and 0 deletions

View File

@ -0,0 +1,20 @@
; 88. Merge Sorted Array
;
; We consider that the function returns the sorted list
; instead of overwriting the nums1 list (as specified in the subject)
#lang racket
(define/contract (merge nums1 nums2)
(-> (listof number?) (listof number?) (listof number?))
"Merge recursively two sorted lists NUMS1 and NUMS2."
(cond
[(null? nums1) nums2]
[(null? nums2) nums1]
[(< (car nums1) (car nums2))
(cons (car nums1) (merge (if (list? nums1) (cdr nums1) '()) nums2))]
[else (cons (car nums2) (merge (if (list? nums2) (cdr nums2) '()) nums1))]))
(for/list ([item (merge '(1 2 3) '(1 3 5))])
(println item))