Skip to content

Instantly share code, notes, and snippets.

@galek
Created June 27, 2017 07:01
Show Gist options
  • Save galek/8cd26216868ca11f9090b4c3129d199e to your computer and use it in GitHub Desktop.
Save galek/8cd26216868ca11f9090b4c3129d199e to your computer and use it in GitHub Desktop.
List to BST ftpl
Data List
{
List = c_nil ++ double * List .c_cons;
}
Data SearchTree
{
SearchTree = c_tnil ++ double * SearchTree * SearchTree .c_branch;
}
Scheme ToTree
{
Fun Construct
{
Fun Split
{
current = [2].~c_cons.[1];
rest = [2].~c_cons.[2];
next = ([1] * rest) .Split;
Split =
[1].~c_nil ->
(c_nil * c_nil),
[2].~c_nil ->
(c_nil * c_nil),
(current * [1]) .equal ->
next,
(current * [1]) .less ->
(current * next.[1]) .c_cons * next.[2] ,
next.[1] * (current * next.[2]) .c_cons;
}
pivot = [1].~c_cons.[1];
rest = [1].~c_cons.[2];
res = (pivot * rest) .Split;
@ = [1].~c_nil ->
c_tnil,
(res.[1].~c_nil ->
(res.[2].~c_nil ->
(pivot * c_tnil * c_tnil) .c_branch,
(pivot * c_tnil * (res.[2]).Construct) .c_branch)
,(res.[2].~c_nil ->
(pivot * (res.[1]).Construct * c_tnil) .c_branch,
(pivot * (res.[1]).Construct * (res.[2]).Construct) .c_branch));
}
Fun Contains
{
Fun or
{
or = [1] ->
true,
[2] ->
true,
false;
}
Contains = [1].~c_tnil ->
false,
(([1].~c_branch.[1]) * [2]) .equal ->
true,
((([1].~c_branch.[2] * [2]).Contains) * (([1].~c_branch.[3] * [2]).Contains)) .or;
}
Fun RandomList
{
@ = ([2] * 0).equal -> c_nil,
(([1] * (rand * ([2] * [1]).sub).mul).add * ([1] * ([2] * 1).sub).RandomList).c_cons;
}
// @ = ((
// ((3.0 *
// ((4.0 *
// ((5.0 * (c_nil))
// .c_cons))
// .c_cons))
// .c_cons)
// .Construct)
// * 4.0).Contains.print;
@ = ((1*20).RandomList).Construct;
}
Application
% ToTree
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment