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

From CSWiki

< Courses | CS 460 | Fall 2005 | Homework | Oscar Chen
Revision as of 08:55, 8 October 2005 by Oscar Chen (Talk | contribs)
(diff) ←Older revision | Current revision (diff) | Newer revision→ (diff)
Jump to: navigation, search

[edit] CS460

[edit] Oct 8 Homework, Oscar Chen

local Last ListIn LastIn Out in
   ListIn = [1 2 3 4]
   LastIn = nil
   
   proc {Last ListIn LastIn Z?}
      if ListIn==nil then Z=LastIn
      else case ListIn of H|T then {Last T H Z?} end
      end
   end

   {Last ListIn LastIn Out} {Browse Out}
end
local Drop ListIn NumDrop Out in

   ListIn = [1 2 3 4]

   proc {Drop ListIn NumDrop Z?}
      if ListIn==nil then Z=nil
      elseif
	 NumDrop==0 then Z=ListIn
      else
	 case ListIn of H|T then {Drop T NumDrop-1 Z?}
	 end
      end
   end

   {Drop ListIn 3 Out} {Browse Out}
end
local Member ListIn
   
   ListIn = [1 2 3 4]

   fun {Member ListIn MemOf}
      local Head in
	 case ListIn of H|T then Head=H
	    if Head==MemOf then true
	    else {Member T MemOf} end
	 else false end
      end
   end

in
   {Browse {Member ListIn 3}}
   {Browse {Member ListIn 5}}
end
local IsPrefix ListIn

   ListIn = [1 2 3 4 5 6]
   
   fun {IsPrefix ListIn PrefixIn}
      if ListIn==nil then false
      elseif PrefixIn==nil then true
      else
	 local Compare1 Compare2 in
	    case ListIn of H|T then Compare2=H
	       case PrefixIn of H2|T2 then Compare2=H2
		  if Compare1==Compare2 then {IsPrefix T T2}
		  else false end
	       end
	    end
	 end
      end
   end
   
in
   {Browse {IsPrefix ListIn [1 2 3 4]}}
   {Browse {IsPrefix ListIn [1 4 2]}}
end

No Browse?

local Length ListIn Count Out in

   ListIn = [a b b o t t]
   Count = 0
   
   proc {Length ListIn Count Z?}
      if ListIn==nil then Z=Count
      else
	 case ListIn of H|T then {Length T Count+1 Z?}
	 end
      end
   end

   {Length ListIn Count Out} {Browse Out}
end