mirror of
https://codeberg.org/ProgramSnail/lang_2023.git
synced 2025-12-06 06:58:45 +00:00
debug
This commit is contained in:
parent
582ad5668e
commit
0d62ae0814
29 changed files with 99479 additions and 1166 deletions
70
tests/functions.lang
Normal file
70
tests/functions.lang
Normal file
|
|
@ -0,0 +1,70 @@
|
|||
// "decl" is not required, but useful in module interface
|
||||
|
||||
decl sum ('A : #Add) : 'A -> 'A -> 'A
|
||||
def sum : a b = a + b
|
||||
|
||||
decl fib : Int -> Int
|
||||
def fib : n =
|
||||
match n with
|
||||
| 0 | 1 -> 1
|
||||
| _ -> fib (n - 1) + fib n
|
||||
|
||||
decl fact : Int -> Int
|
||||
def fact : n =
|
||||
match n with
|
||||
| 0 -> 1
|
||||
| n -> n * fact (n - 1)
|
||||
|
||||
decl find_prefix_hashes ('H : (#AccHash Char)) : String -> Array 'H
|
||||
def find_prefix_hashes ('H : (#AccHash Char)) : str = {
|
||||
var hashes = (Array 'H).new (str.size + 1)
|
||||
|
||||
; hashes.0 = 'H.of str.0
|
||||
for i in 1..hashes.size do {
|
||||
; hashes.i = hashes.(i - 1).clone
|
||||
; hashes.i.append str.i
|
||||
}
|
||||
|
||||
return hashes
|
||||
}
|
||||
|
||||
// ?? other default constructor symbol (instead of placeholder _), etc. ??
|
||||
|
||||
// seporate first and last iteration of loop ?
|
||||
// previous and next iterations ?
|
||||
|
||||
decl find_substring : String -> String -> Array Index
|
||||
def find_substring : str substr = {
|
||||
alias Hash = AccHash Char
|
||||
|
||||
var result = (Array Index).empty
|
||||
|
||||
const str_hashes = find_prefix_hashes Hash str
|
||||
const substr_hash = Hash.of substr
|
||||
|
||||
for i in 0..(str_hashes.size - substr.size) do {
|
||||
const part_hash = Hash.diff str_hashes.(i + substr.size) str_hashes.i
|
||||
|
||||
if part_hash == substr_hash then {
|
||||
; result.push i
|
||||
}
|
||||
}
|
||||
|
||||
return result
|
||||
}
|
||||
|
||||
decl is_empty : Unit -> Bool
|
||||
def is_empty =
|
||||
return 0
|
||||
|
||||
decl do_something : Unit -> Unit
|
||||
def do_something =
|
||||
IO.print "Hello World!"
|
||||
|
||||
decl mul : Int -> Int -> Int
|
||||
def mul : x y = x * y
|
||||
|
||||
decl mul_10 : Int -> Int
|
||||
def mul_10 = mul 10 // or argument can be used
|
||||
|
||||
// ?? is partial application feature needed ??
|
||||
Loading…
Add table
Add a link
Reference in a new issue