I am trying a modification of sort code on http://home.adelphi.edu/~siegfried/cs270/270rl10.html where I am using let for the insert function:
(define (mysort alon )
(let insert ((n n) (alon alon))
(cond
[(empty? alon) (cons n empty)]
[else (cond
[(< n (first alon)) (cons n alon)]
[else (cons (first alon)
(insert n (rest alon))])])
(cond
[(empty? alon) empty]
[(cons? alon) (insert (first alon)
(mysort (rest alon)))])))
(mysort (list 1 2 3 4 5 6 2 3 1 4 5 2 10))
However, it is not working at level of 'let' variable declaration :
n: unbound identifier in module in: n
I see here (https://docs.racket-lang.org/reference/let.html) that 'let' needs to have initial values of variables. Can we use 'let' without initializing the variables? How can above code be corrected?
Edit: I tried to use lambda but it does not work:
(define (mysort4 alon )
(let ([insert4
(lambda (n alon)
(cond
[(empty? alon) (cons n empty)]
[(< n (first alon)) (cons n alon)]
[else (cons (first alon)
(insert4 n (rest alon) ))]))])
(cond
[(empty? alon) empty]
[(cons? alon) (insert4 (first alon)
(mysort4 (rest alon) ) )])))
(mysort4 (list 1 2 3 4 5 6 2 3 1 4 5 2 10))
The error is:
insert4: unbound identifier in module in: insert4