Courses/CS 460/Fall 2005/Homework/Gufran Mohammed/Oct 15

From CSWiki

Jump to: navigation, search

Contents


[edit] IsList

% Checks to see if Xs is a list.

local
   proc {Islist Xs ?B}
       H T in
      choice
         Xs = H|nil
         B = true
      [] Xs = H|T
	 B = {Islist T}
      [] Xs = nil
	 B =false
      end
   end
in
   {Browse {SearchAll proc{$ Ans} {Islist  [2 3 4]   Ans} end}}
   {Browse {SearchAll proc{$ Ans} {Islist  [one two three four]  Ans} end}}
end

[edit] Length


% Returns the length of the list. 


local
   proc {Length Xs ?L}
      H T in
      Xs = H|T 
      choice
	 T = nil
	 L = 1
      [] L = {Length T} + 1
      [] Xs = nil
	 L = 0
      end
   end
in
   {Browse {SearchAll proc{$ Ans} {Length [ase ein di]  Ans} end}}
   {Browse {SearchAll proc{$ Ans} {Length [2 3 4 5]  Ans} end}}
end

[edit] Nth

Presented In the Class

% Gets the Nth element in the list Xs (continuing from 1).
local
   proc{Nth Xs N R}
      H T in
      Xs =H|T
      choice
	 N = 1
	 R = H
      [] R = {Nth T N-1}
% If the number exceeds the List length the result is nill.
      []N = 0
	 R = nil
      end
   end
in
{Browse {SearchAll proc {$ Ans} {Nth [4 5 7 d] 4 Ans} end }}
{Browse {SearchAll proc {$ Ans} {Nth [3 4 1] 6 Ans} end }}
end


[edit] Reverse

% Reverse the list .
% for some reason i am getting answer as 2 lists the first list is null and second list is the answer
local
   proc{Reverse In Out}
      H T in
      choice
	 In = H|nil
	 Out = H
      [] In = H|T
	 Out ={Reverse T}|H
      end
   end
in
   {Browse {SearchAll proc{$ Ans} {Reverse [1 2 3] Ans} end }}
   {Browse {SearchAll proc{$ Ans} {Reverse [one two three] Ans} end }}
end

[edit] Permute

%%%%%%%%%% Haven't Finished Yet %%%%%%%%%%%%
local
   proc{Permute In ?Out}
      H T Th Tt in
      choice
         In = H|nil
         Out = H
      [] In = H|T
	 T = Th|Tt
	 Out = {Permute Tt}|{Permute Th}
      end
    end
in
   {Browse {SearchAll proc {$ Perm} {Permute [a b c d] Perm} end}}
end

Personal tools