diff options
Diffstat (limited to 'std/List.bruijn')
-rw-r--r-- | std/List.bruijn | 54 |
1 files changed, 54 insertions, 0 deletions
diff --git a/std/List.bruijn b/std/List.bruijn new file mode 100644 index 0000000..a4fa40a --- /dev/null +++ b/std/List.bruijn @@ -0,0 +1,54 @@ +# MIT License, Copyright (c) 2022 Marvin Borner +# Copyright (c) 2017-2019 Sandro Lovnički +# TODO: Tests. + +:import std/Combinator . + +:import std/Logic . + +# empty list element +empty [[1]] + +# appends an element to a list +append [[[[0 3 2]]]] + +# returns the head of a list or empty +head [0 F T] + +# returns the tail of a list or empty +tail [0 F F] + +# returns whether a list is empty +empty? [0 T [[F]]] + +# returns the last element of a list or empty +last Z [[empty? 0 [empty] [empty? (tail 1) (head 1) (2 (tail 1))] I]] + +# swaps both sides of a list +swap [[[2 0 1]]] + +# TODO: Fix ternary inc +# finds index of number in list or empty +# find [[Y rec 1 0 +0]] +# rec [[[[(empty? 1) empty ((eq? 2 (head 1)) 0 (3 2 (tail 1) (S 0)))]]]] + +# TODO: Fix ternary inc +# gets element of list by index or empty +# get [[Y rec 1 0 +0]] +# rec [[[[(empty? 1) empty ((eq? 2 0) (head 1) (3 2 (tail 1) (S 0)))]]]] + +# merges two lists to one +merge Y rec + rec [[[1 0 [[append 1 (4 0 2)]]]]] + +# reverses a list +reverse [Y rec 0 empty] + rec [[[(empty? 1) 0 (2 (tail 1) (append (head 1) 0))]]] + +# removes elements of a list that satisfy a condition +remove Y rec + rec [[[0 empty [[((3 1) I (append 1)) (4 3 0)]]]]] + +# sorts a list using quick sort (leq? and gre? as argument) +sort [[Y (rec 1 0)]] + rec [[[[0 empty [[merge (3 (remove (swap 4 1) 0)) (append 1 (3 (remove (swap 5 1) 0)))]]]]]] |