/* Ordered words in Picat. http://rosettacode.org/wiki/Ordered_words """ Define an ordered word as a word in which the letters of the word appear in alphabetic order. Examples include 'abbey' and 'dirt'. The task is to find and display all the ordered words in this dictionary that have the longest word length. (Examples that access the dictionary file locally assume that you have downloaded this [unixdict.txt] file yourself.) The display needs to be shown on this page. Model created by Hakan Kjellerstrand, hakank@gmail.com See also my Picat page: http://www.hakank.org/picat/ */ main => go. go => FD = open("unixdict.txt"), % FD = open("words_lower.txt"), Dict = new_map(), while (not at_end_of_stream(FD)) Line = read_line(FD), if Line == Line.sort() then Len = Line.length, Dict.put(Len, cond(Dict.has_key(Len), Dict.get(Len), "") ++ [Line]) end end, close(FD), println(Dict.get(max(Dict.keys()))), nl. go2 => % Dict = "unixdict.txt", Dict = "words_lower.txt", Words = new_map([Word=Word.length : Word in read_file_lines(Dict), Word == Word.sort()]), MaxLen = max([Len : _Word=Len in Words]), println(maxLen=MaxLen), println([Word : Word=Len in Words, Len=MaxLen].sort()), nl.