53 lines
1.2 KiB
Plaintext
53 lines
1.2 KiB
Plaintext
; algorithms.flan, written by hand in the indented syntax. test_syntax reads
|
|
; both and wants the same forms.
|
|
|
|
import agent "vendor:agent"
|
|
|
|
fn find-match(str: str, pattern: str) -> i32
|
|
for i in range(length(str))
|
|
let matched = true
|
|
for j in range(length(pattern))
|
|
if str[i + j] != pattern[j]
|
|
matched = false
|
|
if matched
|
|
return i
|
|
-1
|
|
|
|
fn selection-sort(coll: [$t]) -> () where is-ordered($t)
|
|
let len = length(coll)
|
|
for i in range(len)
|
|
let min-val = i
|
|
for j in range(i + 1, len)
|
|
if coll[j] < coll[min-val]
|
|
min-val = j
|
|
let tmp = coll[min-val]
|
|
coll[min-val] = coll[i]
|
|
coll[i] = tmp
|
|
|
|
fn insertion-sort(coll: [$t]) -> () where is-ordered($t)
|
|
let i = 1
|
|
let length = length(coll)
|
|
while and(i < length)
|
|
let j = i
|
|
while j > 0
|
|
and coll[j] < coll[dec(j)]
|
|
let temp = coll[j]
|
|
coll[j] = coll[dec(j)]
|
|
coll[dec(j)] = temp
|
|
--(j)
|
|
++(i)
|
|
|
|
fn main() -> i32 = 0
|
|
|
|
comment():
|
|
insertion-sort([\I \N \S \E \R \T \I \O \N \S \O \R \T])
|
|
insertion-sort(slice([6 2 4 9 1 9 4 5], 0, 8))
|
|
let str = bytes("INSERTIONSORT")
|
|
insertion-sort(str)
|
|
println(str)
|
|
let str = bytes("SELECTIONSORT")
|
|
selection-sort(str)
|
|
println(str)
|
|
find-match("aababba", "abba")
|
|
:-
|