diff options
| author | mryouse | 2022-07-16 02:41:29 +0000 |
|---|---|---|
| committer | mryouse | 2022-07-16 02:41:29 +0000 |
| commit | 5ece218b4e1461a8ce9c1770aba0e47c829e8362 (patch) | |
| tree | e98bdbe4754cc24f891e114e8201d3a8b724009f /libs/sort.neb | |
| parent | 51c9b6c3773d713f9771f60cac4e84429dbfc181 (diff) | |
implement 'ord', and start integrating string sorting
Diffstat (limited to 'libs/sort.neb')
| -rw-r--r-- | libs/sort.neb | 36 |
1 files changed, 29 insertions, 7 deletions
diff --git a/libs/sort.neb b/libs/sort.neb index b8f324b..081a678 100644 --- a/libs/sort.neb +++ b/libs/sort.neb @@ -36,19 +36,29 @@ (redef ret (append ret _item_))) ret) -(func insert (lst item) +(func .insert-num (lst item) (extend-reduce - (append (take-while lst (< _item_ item)) item) - (drop-while lst (< _item_ item)))) + (append (take-while lst (<= _item_ item)) item) + (drop-while lst (<= _item_ item)))) -(func insertion-reduce (lst) - (reduce insert lst (list))) +(func .insert-string (lst item) + (extend-reduce + (append (take-while lst (strcmp _item_ item)) item) + (drop-while lst (strcmp _item_ item)))) + +;(func insertion-reduce (lst) +; (reduce insert lst (list))) + +(func insertion-strings (lst) + (reduce .insert-string lst (list))) +(func insertion-nums (lst) + (reduce .insert-num lst (list))) -(func insertion (lst) +(func insertion-iter (lst) (def ret (list)) (for-each lst - (redef ret (insert ret _item_)) + (redef ret (.insert-num ret _item_)) (print (->string ret))) ret) @@ -119,3 +129,15 @@ (print (concat "after: " (->string after))) (print (concat "consider: " (->string consider)))) (extend (append (quick before) pivot) (quick after)))))) + +(func strcmp (left :string right :string) + (branch + ((or + (eq? 0 (length left)) + (eq? 0 (length right))) + (<= (length left) (length right))) + ((eq? (first-char left) (first-char right)) + (strcmp (rest-char left) (rest-char right))) + (#true + (<= (ord (first-char left)) (ord (first-char right)))))) + |
