Jump to content

Scheme Newbie - Interpreting Scheme for "INSERT" function

- - - - -

  • Please log in to reply
No replies to this topic

#1
rheb2k

rheb2k

    Newbie

  • Members
  • Pip
  • 1 posts
Hey everyone,

I apologize, I know this is a Non-language specific forum, but I didn't know where else to post.

I was wondering if someone could help me to translate the following Scheme into an alternative pseudo-code...(like java or c)

I know that the following will insert a number, x, into a list, and the resulting list returned is a sorted list (in ascending order) with x included.

I have tried looking up each individual expression (like cond, cons, car, etc.) and while I get an idea of what it's supposed to do, I'm not getting the whole picture. If someone could help explain it to me, I'd appreciate it greatly!


(define insert (lambda (x myList)

  (cond

     ((null? myList) (cons x '())) ;base case

  (else

     (if (<= x (car myList))

        (cons x myList)

        (cons (car myList) (insert x (cdr myList))))))))


//Example
> (insert 6 '(1 5 7))
(list 1 5 6 7)




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users