The Geeks Shall Inherit the Earth

Personal Ramblings of the Techhouse Admin

Skip to: Content | Sidebar | Footer

Lisp Practice Updates

21 April, 2010 (16:56) | Erlang | By: Brian

After reading some more documentation and tinkering, I’ve updated the three practice functions I wrote in Erlang a little while ago. Here they are:

palinedromep(List) when is_list(List) ->
 List == lists:reverse(List);
palinedromep(_) ->
 false.

presentp(Atom, Tuple) when is_atom(Atom), is_tuple(Tuple) ->
 presentp(Atom,tuple_to_list(Tuple));
presentp(Atom, [Head | Rest]) when is_atom(Atom), is_list(Head) ->
 presentp(Atom,Head) orelse presentp(Atom, Rest);
presentp(Atom, [Head | Rest]) when is_atom(Atom) ->
 Atom == Head orelse presentp(Atom, Rest);
presentp(_, _) -> false.

duplicate_entries(Thing) when not is_list(Thing) -> false;
duplicate_entries([]) -> false;
duplicate_entries([_]) -> false;
duplicate_entries([Head | Rest] ) ->
 lists:member(Head, Rest) orelse duplicate_entries(Rest).

In addition, I wrote a simple unit test method that calls each method with a bunch of different inputs and tries to match the output with an expected true or false. If the test fails it throws a mismatch error. If it succeeds, it returns an atom saying the tests passed. Simple, but effective, and I only have to call lispprac1:unit_test() to make sure I haven’t broken anything.

unit_test() ->
 true = palinedromep([]),
 true = palinedromep([a]),
 true = palinedromep([a, a]),
 true = palinedromep([a, b, a]),
 true = palinedromep([a, b, b, a]),
 false = palinedromep([a, b]),
 false = palinedromep([a, b, b]),
 false = palinedromep(a),
 false = palinedromep({a, b}),
 true = presentp(a, [a]),
 true = presentp(a, [b, a]),
 true = presentp(a, [a, b, b, c]),
 true = presentp(a, {a}),
 true = presentp(a, {b, a}),
 true = presentp(a, [b, [a]]),
 true = presentp(a, [b, [b, [b, a]]]),
 false = presentp(a, [b]),
 false = presentp(a, b),
 false = presentp(a, [b, [b, c]]),
 false = presentp(a, {b, b, c}),
 false = presentp(a, {b, {b, c}}),
 true = duplicate_entries([a, a]),
 true = duplicate_entries([a, b, a]),
 true = duplicate_entries([a, b, c, b]),
 true = duplicate_entries([[a], b, [a]]),
 true = duplicate_entries([[a, b], b, b, [a]]),
 true = duplicate_entries([[a, b], b, c, [a, b]]),
 false = duplicate_entries([a, b]),
 false = duplicate_entries([[a], b]),
 false = duplicate_entries(a),
 false = duplicate_entries({a}),
 false = duplicate_entries([a, b, c]),
 false = duplicate_entries([a]),
 tests_passed.

Next I plan to use a record data structure on the train yard problem so that the yard data is easier to work with.

Comments

Pingback from Tweets that mention The Geeks Shall Inherit the Earth » Lisp Practice Updates — Topsy.com
Time April 22, 2010 at 5:38 pm

[...] This post was mentioned on Twitter by Brian E. Williams. Brian E. Williams said: Some random Erlang musings on my blog: http://techhouse.us/wordpress-mu/brianw/2010/04/21/lisp-practice-updates/ [...]

Write a comment

You need to login to post comments!