Ordered words: Difference between revisions
Content deleted Content added
→{{header|Common Lisp}}: grammar |
|||
Line 1,189: | Line 1,189: | ||
let rec aux = function |
let rec aux = function |
||
| c1::c2::tl -> |
| c1::c2::tl -> |
||
if |
if c1 <= c2 |
||
then aux (c2::tl) |
then aux (c2::tl) |
||
else false |
else false |
||
Line 1,202: | Line 1,202: | ||
(fun (lst, n) word -> |
(fun (lst, n) word -> |
||
let len = String.length word in |
let len = String.length word in |
||
let comp = |
let comp = compare len n in |
||
match lst, comp with |
match lst, comp with |
||
| lst, 0 -> ((word::lst), n) |
| lst, 0 -> ((word::lst), n) (* len = n *) |
||
| lst, -1 -> (lst, n) (* len < n *) |
| lst, -1 -> (lst, n) (* len < n *) |
||
| _, 1 -> ( |
| _, 1 -> ([word], len) (* len > n *) |
||
| _ -> assert false |
| _ -> assert false |
||
) |
) |
||
Line 1,216: | Line 1,216: | ||
let ic = open_in "unixdict.txt" in |
let ic = open_in "unixdict.txt" in |
||
let words = read_lines ic in |
let words = read_lines ic in |
||
let lower_words = List. |
let lower_words = List.map String.lowercase words in |
||
let ordered_words = |
let ordered_words = List.filter is_ordered lower_words in |
||
List.fold_left |
|||
(fun acc w -> |
|||
if is_ordered w |
|||
then w::acc |
|||
else acc) |
|||
[] lower_words in |
|||
let longest_ordered_words = longest_words ordered_words in |
let longest_ordered_words = longest_words ordered_words in |
||
List.iter print_endline longest_ordered_words</lang> |
List.iter print_endline longest_ordered_words</lang> |