diff options
| -rw-r--r-- | libs/sort.neb | 36 | ||||
| -rw-r--r-- | neb/std/strings.py | 7 |
2 files changed, 36 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)))))) + diff --git a/neb/std/strings.py b/neb/std/strings.py index 4a7166e..00d1fc3 100644 --- a/neb/std/strings.py +++ b/neb/std/strings.py @@ -57,3 +57,10 @@ def interpretRaw(symbol, args, env, ns): return String(str(args[0])) STRINGS.register("raw", Builtin("raw", interpretRaw, [Arg("string", TypeEnum.STRING)], return_type=Type(":string"))) + +def interpretOrd(symbol, args, env, ns): + if len(args[0].value) != 1: + raise InterpretPanic(symbol, "requires a :string of length 1", args[0]) + return Int(ord(args[0].value)) + +STRINGS.register("ord", Builtin("ord", interpretOrd, [Arg("char", TypeEnum.STRING)], return_type=Type(":int"))) |
