RabbitFarm

2021-11-21

The Weekly Challenge 139 (Prolog Solutions)

The examples used here are from the weekly challenge problem statement and demonstrate the working solution.

Part 1

You are given a list of numbers. Write a script to implement JortSort. It should return true/false depending if the given list of numbers are already sorted.

Solution


:-initialization(main).

jort([]).
jort([H0, H1|[]]):-
    H1 >= H0.
jort([H0, H1|T]):-
    H1 >= H0,
    jort([H1|T]).

main:-
    (jort([1, 2, 3, 4, 5]), format("1~n", _); format("0~n", _)),
    (jort([1, 3, 2, 4, 5]), format("1~n", _); format("0~n", _)),
    (jort([1, 2, 3, 4, 5, 6]), format("1~n", _); format("0~n", _)),
    halt.

Sample Run


$ gplc prolog/ch-1.p 
$ prolog/ch-1   
1
0
1

Notes

I had never heard of a Jort Sort before this week. Once I understand what it was, a joke function which just returns true or false based on whether a given list is already sorted, I am still not sure I really get it. Or, at least, I don't really get the "joke". Apparently it started as a JavaScript thing so maybe there is something inherently funny about the JavaScript code for it.

Anyway, this is pretty easily done in Prolog especially in this case where we only are to be given a list of numbers. The code as written only goes in one direction in that it only verifies a list as requested. This could go in the other direction with a little use of between/3 and generate a sorted list too. But would that ruin the joke? Make it funnier? I found the whole exercise so unamusing I didn't bother!

Maybe the amusing part of this whole "joke" sort was, ironically, just how stupid I found the whole thing.

References

Jort Sort

Challenge 139

posted at: 16:33 by: Adam Russell | path: /prolog | permanent link to this entry