PasteRack.org
Paste # 18173
2022-07-20 01:52:51

Forked from paste # 50030.

Fork as a new paste.

Paste viewed 1402 times.


Embed:

  1. #lang racket
  2.  
  3. ; LON is one of:
  4. ;  empty
  5. ;  (cons Number LON)
  6. ; interpretation: a list of numbers
  7.  
  8. ; LON -> LON
  9. ; created a sorted version of one-lon
  10.  
  11. (check-expect (sort '(13 0 1 2 3 4 5 6 7 8 9 10 11 12 14 15))
  12.                     '(0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15))
  13.  
  14. (define (sort one-lon)
  15.    (cond
  16.      [(empty? one-lon) '()]
  17.      [(cons? one-lon)
  18.       (ryan (first one-lon) (sort (rest one-lon)))]))
  19.  
  20. ; Number LON -> LON
  21. ; puts n into one-lon according to ascending order
  22. ; WHAT DO WE KNOW: one-lon is sorted in ascending order
  23. ; because ryan is called inside of sort with (sort (rest ...))
  24.  
  25. (check-expect (ryan 13 (list 11 12 14 15)) (list 11 12 13 14 15))
  26. (check-expect (ryan 13 empty) (list 13))
  27.  
  28. (define (ryan n one-lon)
  29.    (cond
  30.      [(empty? one-lon) (list n)]
  31.      [(cons? one-lon)
  32.       (if (<= (first one-lon) n)
  33.           (cons (first one-lon) (ryan n (rest one-lon)))
  34.           (cons n one-lon))]))
  35.  
  36. (ryan 13 empty)
  37. (check-expect (ryan 13 empty) (list 12))

=>

check-expect: undefined;

 cannot reference an identifier before its definition

  in module: 'm

check-expect: undefined;

 cannot reference an identifier before its definition

  in module: 'm

check-expect: undefined;

 cannot reference an identifier before its definition

  in module: 'm

'(13)

check-expect: undefined;

 cannot reference an identifier before its definition

  in module: 'm