kivikakk.ee

Mer OCaml

This one was quite tricky, and my solution is inelegant, but I’m glad to have gotten it.

Inserting a new word into the trie (data structure as defined earlier):

let rec insert word =
let first = String.get word 0
and rest = String.sub word 1 (String.length word - 1)
in let insert_node = function
Letter(c,b,nl) as l -> if (first=c) then
if (String.length rest)=0 then Letter(c,true,nl)
else Letter(c,b,insert rest nl)
else l
in function
(Letter(c,_,_) as h)::t when (first=c) -> (insert_node h)::t
| h::t -> h::(insert word t)
| [] -> [insert_node (Letter(first,false,[]))];;

Some helpers for free:

let rec string_of_trie trie =
let string_of_node = function
Letter(c,b,nl) -> (String.make 1 c) ^ (if b then "*" else "") ^ " " ^ (string_of_trie nl)
in "[" ^ (String.concat "; " (List.map string_of_node trie)) ^ "]";;
let print_trie trie =
print_endline (string_of_trie trie);;

Next I think I have to establish the height of this thing or something, so that’ll let me do a prettier print_trie later.

Style comparison:

let rec construct = function
[] -> []
| h::t -> insert h (construct t);;
let construct wl =
List.fold_left (fun x y -> insert y x) [] wl;;

OCaml: sakta men säkert.

A trie:

type lex_node = Letter of char * bool * lex_tree
and lex_tree = lex_node list;;

A function to test whether a word belongs in the dictionary (answer to an exercise from the book “Deploying Applications With Objective Caml”):

let exists word trie =
let rec exists_aux acc nl =
let exists_inner = function
Letter(c,b,nl) -> let wc = (acc ^ (String.make 1 c))
in if b && (wc=word) then true
else exists_aux wc nl
in List.exists exists_inner nl
in exists_aux "" trie;;

Now working on “insert”.

Telstar:s farväl

Idag tog jag farväl av min ’86 Telstar, min alldra första bil. Det var en bra bil!

NativeApplicationUpdater (for AIR 2.0)

baby spinach leaves

tea clouds, er lang (stupid boy), re re re. RE. Religious education (Regular expression). Religion is a regular expression? /religion/g. Religion as a global religious expression. /religion/i; case-insensitive religion. Insensitive expression. Stunning prophecy on p.LXXI.

Don’t get started on labels again. I love jumping to labels. They drive me along on my little way, helping me avoid all actual inclination to do things, but just to say “oh yes I’m X and therefore I can have the opinion Y.”

I have applied the following terms to myself in the past:

girl. felon. agnostic. Australian. wife. solipsist. son. alone. pilot. husband. monk. nihilist. daughter. friendly. Christian. enemy. vegan. Buddhist. assured. right. Estonian. parent. vague. transwoman. lover. man. gay. heterosexual. polyamorist. boyfriend. queer. genderqueer. boy. atheist. girlfriend. unfriendly. wrong. transgender. father. Zen practiser. Sikh. antipatriot. vegetarian. feminist. universalist. woman. omnivore. friend. humanist. relativist. theist. programmer. homosexual. interpreter. Muslim. Mahayana practiser. mother. monogamist. Theravadin.

myself?

That’s one label I should keep.