Courses/CS 460/Fall 2005/Homework/Oscar Chen/Oct 15

From CSWiki

Jump to: navigation, search

[edit] Oct 15 Homework, Oscar Chen

local Last
   proc {Last ListIn Out}
      H T in
      ListIn = H|T
      choice
		 T = nil
		 Out = H
      []
		{Last T Out}
      end
   end
in
   {Browse {SearchAll proc {$ Ans} {Last [1 2 3 4] Ans} end}}
end
local Drop
   proc {Drop ListIn NumDrop Out}
      T in
      ListIn = _|T
      choice
	 NumDrop = 0
	 Out = ListIn
      []
	 {Drop T NumDrop-1 Out}
      end
   end
in
   {Browse {SearchAll proc {$ Ans} {Drop [1 2 3 4] 3 Ans} end}}
end
local Member
   proc {Member ListIn MemOf Out}
      H T in
      ListIn = H|T
      choice
	 H = MemOf
	 Out = true
      []
	 {Member T MemOf Out}
      end
   end
in
{Browse {SearchAll proc {$ Ans} {Member [1 2 3 4] 3 Ans} end}}
   {Browse {SearchAll proc {$ Ans} {Member [1 2 3 4] 6 Ans} end}}
end

How to return false instead of nil?

local IsPrefix
   proc {IsPrefix ListIn PrefixIn Out}
      H1 T1 H2 T2 in
      ListIn = H1|T1
      PrefixIn = H2|T2
      choice
	 H1 = H2
	 {IsPrefix T1 T2 Out}
      []
	 T2 = nil
	 Out = true
      end
   end
in
   {Browse {SearchAll proc {$ Ans} {IsPrefix [1 2 3 4] [1 2 3] Ans} end}}
   {Browse {SearchAll proc {$ Ans} {IsPrefix [1 2 3 4] [4 2] Ans} end}}
end
local Length
   proc {Length ListIn C Out}
      T in
      ListIn = _|T
      choice
	 T = nil
	 Out = C+1
      []
	 {Length T C+1 Out}
      end
   end
in
   {Browse {SearchAll proc {$ Ans} {Length [1 2 3 4] 0 Ans} end}}
   {Browse {SearchAll proc {$ Ans} {Length [1 2 3 4 5 6] 0 Ans} end}}
end

Way to pass internal variable?

local Permute
   proc {Permute ListIn Out}
      H T Temp in
      ListIn = H|T
      choice
	 ListIn = nil
	 Out = nil
      []
	 Temp = H
	 {Permute T Out}
	 Out = H#T
      end
   end
in
   {Browse {SearchAll proc {$ Ans} {Permute [1 2 3 4] Ans} end}}
   {Browse {SearchAll proc {$ Ans} {Permute [1 2 3 4 5 6] Ans} end}}
end

(not working yet..)