Skip to content

sorting a list #94

Answered by josd
giacomociti asked this question in Q&A
Sep 26, 2023 · 2 comments · 1 reply
Discussion options

You must be logged in to vote

There is a built-in list:sort in eye but it removes duplicates.
If you don't want the remove duplicates, here is a possible implementation

@prefix list: <http://www.w3.org/2000/10/swap/list#> .
@prefix math: <http://www.w3.org/2000/10/swap/math#> .
@prefix log: <http://www.w3.org/2000/10/swap/log#> .
@prefix : <hrttp://ex.org/> .

{ (3 5 8 3 4) :sort ?result} => { :result :is ?result } .

# sort
() :sort () .

{ ?a :sort ?b } <= {
    ?a list:firstRest (?c ?d) .
    (?c ?d) :split (?e ?f) .
    ?e :sort ?g .
    ?f :sort ?h .
    ?i list:firstRest (?c ?h) .
    (?g ?i) list:append ?b .
} .

# split
(?x ()) :split (() ()) .

{ (?a ?b) :split (?c ?d) } <= {
    ?b list:firstRest (?e ?f) .
 …

Replies: 2 comments 1 reply

Comment options

You must be logged in to vote
1 reply
@giacomociti
Comment options

Answer selected by giacomociti
Comment options

You must be logged in to vote
0 replies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants