Thursday, April 26, 2012

Seven Languages in Seven Weeks - Prolog Day 2 Self-Study

More fun with prolog! This self-study section consists in implementing some list operations.

Reverse the elements of a list
rev([], []).
rev([Head|Tail], List) :- append(X, [Head], List), rev(Tail, X).

Find the smallest element of a list.
min([X], X).
min([Head1|[Head2|Tail]], X) :- Head1 =< Head2, min([Head1|Tail], X).
min([Head1|[Head2|Tail]], X) :- Head1 > Head2, min([Head2|Tail], X).

Sort the elements of a list.
insert(X, [], [X]).
insert(X, [H|T], [X,H|T]) :- X =< H.
insert(X, [H|T], [H|Rest]) :- X > H, insert(X, T, Rest).

isort([], Acc, Acc).
isort([H|T], Acc, Sorted) :- insert(H, Acc, Acc2), isort(T, Acc2, Sorted).

Wednesday, April 25, 2012

Seven Languages in Seven Weeks - Prolog Day 1 Self-Study

For this book's section about prolog, I'll be using SWI-Prolog, as I'm currently getting an error installing the gnu-prolog macport. Here's the link to the SWI-Prolog online reference.

Make a simple knowledge base. Represent some of your favorite books and authors.
wrote(odersky, programmingInScala).
wrote(tate, sevenLanguagesInSevenWeeks).
wrote(martin, cleanCode).
wrote(martin, cleanCoder).

Find all books in your knowledge base written by one author.
?- ['books'].
% books compiled 0.00 sec, 13 clauses

?- wrote(martin, X).
X = cleanCode ;
X = cleanCoder.

Make a knowledge base representing musicians and instruments. Also represent musicians and their genre of music.
instrument(flea, bass).
instrument(hendrix, guitar).
instrument(hancock, piano).
instrument(cash, guitar).
genre(flea, funk).
genre(hendrix, blues).
genre(hancock, jazz).
genre(cash, country).

Find all musicians who play the guitar.
?- ['music'].
% music compiled 0.00 sec, 10 clauses

?- instrument(X, guitar).
X = hendrix ;
X = cash.

Wednesday, April 4, 2012

How to get a List of Installed JVMs on a Mac

I got this tip from a fellow worker:
$ /usr/libexec/java_home -V
This command will get you the list of installed Java Virtual Machines installed on your Mac OS X system.

Sunday, April 1, 2012

Inspecting the Process Environment Variables with GDB

While trying to solve the 4th level of the vortex wargame, I found it was necessary to learn how to inspect the location and content of the environment variables within the process memory.

GDB has built-in commands to inspect the process environment, see the GDB manual. You can either list all environment variables or a specific one (e.g. FOOBAR) using the following commands, which will output their values:
(gdb) show environment
(gdb) show environment FOOBAR
In order to locate the environment variables within the process memory, you can query the variable char** environ (see the libc reference and this entry on stack overflow):
(gdb) x/s *((char **)environ)
This will print the location of the first environment variable and its representation as string. To print the next variables, simply add an offset to the variable:
(gdb) x/s *((char **)environ + 1)

I also found these links to be useful: