%% lists.pl %% %% Author(s): %% %% CS 60 Hw 4, part 1 %% %% %% Some "nice" prolog settings: %% See www.swi-prolog.org/pldoc/man?predicate=current_prolog_flag/2 %% This one prompts only when there are variables: :- set_prolog_flag( prompt_alternatives_on, groundness ). % This one sets some reasonable display defaults: :- set_prolog_flag(toplevel_print_options, [quoted(true), portray(true), attributes(portray), max_depth(999), priority(699)]). %% a couple of example predicates that might be helpful... % % member( E, L ) should be true if % E is a top-level member of the list L % member should work when E is a variable or bound to a value; % L should only be bound to a value % member( E, [E|_] ). member( E, [_|R] ) :- member( E, R ). % % reverse( L, Rev ) should be true if Rev is the reverse of L % reverse should work when Rev is a variable or bound; % L should only be bound to a value % reverse( [], [] ). % base case ~ though they don't always involve [] in Prolog! reverse( [F|R], Rev ) :- reverse( R, Tmp ), append( Tmp, [F], Rev ). % % has42( L ) should be true if L contains the element 42 at the top % level % has42( [42] ). has42( [_|R] ) :- has42( R ). %% Here are commented-out placeholders for this week's predicates... %% Be sure to comment each predicate you write! %% removeOne( E, L, NewL ) %% count( E, L, N ) %% find( Pattern, Target, Index ) %% depth( E, Tree, N ) % a helper predicate to define a tree tree1( [ 42, [ 20, [10, [5, [], []], [18, [], []]], [] ], [ 100, [67, [50, [], []], []], [150, [], [2009, [], []]] ] ] ). %% insert( E, Tree, NewTree ) %% path( A, B, Graph, Path ) % a helper predicate to define a graph graph1( [ [a,b], [b,c], [c,d], [b,w], [w,x], [x,z], [b,z] ] ).