mirror of
https://github.com/ProgramSnail/Lama.git
synced 2025-12-05 22:38:44 +00:00
Massive renaming
This commit is contained in:
parent
241ab0a9ae
commit
61296c51e7
152 changed files with 45 additions and 29 deletions
116
stdlib/List.lama
Normal file
116
stdlib/List.lama
Normal file
|
|
@ -0,0 +1,116 @@
|
|||
public fun singleton (x) {
|
||||
x : {}
|
||||
}
|
||||
|
||||
public fun size (l) {
|
||||
case l of
|
||||
{} -> 0
|
||||
| _ : t -> 1 + size (t)
|
||||
esac
|
||||
}
|
||||
|
||||
public fun foldl (f, acc, l) {
|
||||
case l of
|
||||
{} -> acc
|
||||
| x : xs -> foldl (f, f (acc, x), xs)
|
||||
esac
|
||||
}
|
||||
|
||||
public fun foldr (f, acc, l) {
|
||||
case l of
|
||||
{} -> acc
|
||||
| x : xs -> f (foldr (f, acc, xs), x)
|
||||
esac
|
||||
}
|
||||
|
||||
public fun iter (f, l) {
|
||||
case l of
|
||||
{} -> skip
|
||||
| x : xs -> f (x); iter (f, xs)
|
||||
esac
|
||||
}
|
||||
|
||||
public fun map (f, l) {
|
||||
case l of
|
||||
{} -> {}
|
||||
| x : xs -> f (x) : map (f, xs)
|
||||
esac
|
||||
}
|
||||
|
||||
public infix +++ at + (x, y) {
|
||||
case x of
|
||||
{} -> y
|
||||
| x : xs -> x : xs +++ y
|
||||
esac
|
||||
}
|
||||
|
||||
public fun reverse (l) {
|
||||
fun inner (acc, l) {
|
||||
case l of
|
||||
{} -> acc
|
||||
| x : xs -> inner (x : acc, xs)
|
||||
esac
|
||||
}
|
||||
|
||||
inner ({}, l)
|
||||
}
|
||||
|
||||
public fun assoc (l, x) {
|
||||
case l of
|
||||
{} -> None
|
||||
| [y, z] : ys -> if compare (x, y) == 0 then Some (z) else assoc (ys, x) fi
|
||||
esac
|
||||
}
|
||||
|
||||
public fun find (f, l) {
|
||||
case l of
|
||||
{} -> None
|
||||
| x : xs -> if f (x) then Some (x) else find (f, xs) fi
|
||||
esac
|
||||
}
|
||||
|
||||
public fun flatten (l) {
|
||||
local res = [0, {}], curr = [res];
|
||||
|
||||
fun append (x) {
|
||||
local new = x : {};
|
||||
|
||||
curr [0][1] := new;
|
||||
curr [0] := new
|
||||
}
|
||||
|
||||
iter (fun (x) {iter (append, x)}, l);
|
||||
|
||||
res [1]
|
||||
}
|
||||
|
||||
public fun zip (a, b) {
|
||||
case [a, b] of
|
||||
[{}, {}] -> {}
|
||||
| [a : aa, b : bb] -> [a, b] : zip (aa, bb)
|
||||
esac
|
||||
}
|
||||
|
||||
public fun unzip (a) {
|
||||
case a of
|
||||
{} -> [{}, {}]
|
||||
| [a, b] : aa ->
|
||||
case unzip (aa) of
|
||||
[aa, bb] -> [a : aa, b : bb]
|
||||
esac
|
||||
esac
|
||||
}
|
||||
|
||||
public fun remove (f, l) {
|
||||
case l of
|
||||
{} -> {}
|
||||
| h : t -> if f (h) then t else h : remove (f, t) fi
|
||||
esac
|
||||
}
|
||||
|
||||
public fun filter (f, l) {
|
||||
case l of
|
||||
{} -> {}
|
||||
| h : t -> if f (h) then h : filter (f, t) else filter (f, t) fi
|
||||
esac
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue