Courses/CS 460/Fall 2005/Homework/Cynthia York/Oct 15

From CSWiki

Jump to: navigation, search

[edit] FindElement

  • was presented in class
  • uses SearchAll & choice statements to find the locations of an element in a list
% FindElement - lists the location(s) Loc of the requested element X in the 
%                provided list Zs
local List1 List2 List3 SearchValue1 SearchValue2 SearchValue3 Cnt = 1
   proc {FindElement ?Loc Ctr X Zs}
      Zh Zt in
      Zs = Zh|Zt
      choice
	 X = Zh Loc = Ctr 
      [] 
	 {FindElement Loc Ctr+1 X Zt}
      end
      
   end

   % used by SearchAll or GetList to produce the list of digits
   proc {Digit Z}
      Z = choice 0 [] 1 [] 2 [] 3 [] 4 [] 5 [] 6 [] 7 [] 8 [] 9 end
   end

   % used by SearchAll or GetList to produce a list of lower case alpha characters
   proc {LetterLower Z}
      Z = choice a [] b [] c [] d [] e [] f [] g [] h [] i [] j []
	     k [] l [] m [] n [] o [] p [] q [] r [] s [] t [] u []
	     v [] w [] x [] y [] z [] c end
   end

   % used by SearchAll or GetList to product a list of upper case alpha characters
   proc {LetterUpper Z}
      Z= choice 'A' [] 'B' [] 'C' [] 'D' [] 'E' [] 'F' [] 'G' []
	    'H' [] 'I' [] 'J' [] 'K' [] 'L' [] 'M' [] 'N' [] 'O' []
	    'P' [] 'Q' [] 'R' [] 'S' [] 'T' [] 'U' [] 'V' [] 'W' []
	    'X' [] 'Y' [] 'Z' end
   end
   
   % used by SearchAll to produce a list of alphanumeric characters
   proc {GetList Z}
      choice Z = {Digit} [] Z = {LetterLower} [] Z = {LetterUpper} end
   end
   
in
   SearchValue1 = 6
   List1 = {SearchAll proc {$ Lst} {Digit Lst} end}
   {Browse List1}
   {Browse SearchValue1#'is in position(s)'#{SearchAll proc {$ Pos} {FindElement Pos Cnt SearchValue1 List1} end}}

   SearchValue2 = c
   List2 = {SearchAll proc {$ Lst} {LetterLower Lst} end}
   {Browse List2}
   {Browse SearchValue2#'is in position(s)'#{SearchAll proc {$ Pos} {FindElement Pos Cnt SearchValue2 List2} end}}

   SearchValue3 = 'Z'
   List3 = {SearchAll proc {$ Lst} {GetList Lst} end}
   {Browse List3}
   {Browse SearchValue3#'is in position(s)'#{SearchAll proc {$ Pos} {FindElement Pos Cnt SearchValue3 List3} end}}

end

[edit] Permute

  • uses SearchAll & choice to find produce the permutations of a list ListIn
local 
   proc {Permute ListIn PermOut}
      H T in
      {Browse ['Permute: ListIn=' ListIn ', PermOut=' PermOut]}
      choice
	 ListIn=nil PermOut=nil
      []
	 ListIn=H|nil PermOut=H
      []
	 ListIn=H|T PermOut=H#{Permute T}
      end
   end
in
   {Browse {SearchAll proc {$ Perm} {Permute [a b c d]  Perm} end}}
end


[edit] Delete

  • uses SearchAll & choice to create a sub-list XsLeft off list Xs
% DeleteUpToX returns a sub-list XsLeft containing no elements preceding X from list Xs
local
   proc {DeleteUpToX X Xs XsLeft}
      H T in
      Xs = H|T
      choice
	 H=X
	 XsLeft=Xs
      [] 
	 XsLeft={DeleteUpToX X T}
      end
   end
in
   {Browse 'deleting up to '#a#'leaves this'#{SearchAll proc {$ Lst} {DeleteUpToX a [a b c d] Lst} end}}
   {Browse 'deleting up to '#b#'leaves this'#{SearchAll proc {$ Lst} {DeleteUpToX b [a b c d] Lst} end}}
   {Browse 'deleting up to '#c#'leaves this'#{SearchAll proc {$ Lst} {DeleteUpToX c [a b c d] Lst} end}}
   {Browse 'deleting up to '#d#'leaves this'#{SearchAll proc {$ Lst} {DeleteUpToX d [a b c d] Lst} end}}
end
Personal tools