Courses/CS 460/Fall 2005/Homework/Joey Leung/Oct 1

From CSWiki

Jump to: navigation, search

Contents


[edit] Homework 1 (10/1/05)


[edit] Programs on binary trees and lists


% just playing around with list

    local L1 L2 L3 L4 in 
       L1 = 1|nil   
       L2 = 2|nil
       L3 = L1|L2
       L4 = [[1]2]
       {Browse L3==L4}
    end



% got my book and tried to make up the HW, however still having a hard time with syntax errors

local Countlist X in
   proc {Countlist L}
      if L==nil then X=0
      else L of H|T then {Countlist T} +1 end
   end
   {Browse X}
end 
{Countlist [1 2 3]}
   

local Countlist X in
   proc {Countlist L}
      case L of H|T
      [] L==nil then X=0
      else 1+ {Countlist T} end
      end
   end
   {Browse X}
end 
{Countlist [1 2 3]}


declare X in
   fun {Countlist L}
      if L==nil then x=0
      else L of H|T then {Countlist T} +1 end
   end
end
{Browse {Countlist [1 2 3]}}


declare
   fun {Countlist L}
      case L of H|T then skip
	 [] L==nil then 0
      else {Countlist T} + 1  end
   end
end
{Browse {Countlist [1 2 3]}}



% Finally, some that work

declare
   fun {Countlist L}
      if L==nil then 0
      else case L of H|T then {Countlist T} +1 end end
   end
{Browse {Countlist [1 2 3 0]}} --> 4


declare Con X Y Z in
   proc {Con X Y}
      Z = X|Y
      {Browse Z}
   end   
{Con 3 [4]} --> [3 4]


declare First Y in
proc {First X}
   case X of H|T then Y=H end
   {Browse Y}
end
{First [a b c]} --> a


declare Last in
local Y in
   proc {Last X}
      case X of H|T then
	 if T==nil then Y=H
	 else {Last T}
	 end
      end
   end
   {Browse Y}
end
{Last [a b c d e]} --> e