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

From CSWiki

Jump to: navigation, search


Contents

[edit] 5 list procedures or functions (like last week) but use only choice

[edit] Last


local 
fun {Last L}
   H T in
   L=H|T
   choice
      T=nil H
   [] {Last T}
   end
end
in
{Browse {SearchAll fun {$} {Last [1 2 3 4 5]} end}}
end


local 
proc {Last L Out}
   H T in
   L=H|T
   choice
      T=nil Out=H
   [] {Last T Out}
   end
end
in
{Browse {SearchAll proc {$ X} {Last [1 2 3 4 5] X} end}}
end      



[edit] Length


local 
fun {Length L}
   H T X in
   L=H|T
   choice
      T=nil 1
   [] {Length T} +1
   end
end
in
{Browse {SearchAll fun {$} {Length [1 2 3 4 5]} end}}
end


[edit] Nth



local 
fun {Nth L Num}
   H T X in
   L=H|T
   choice
      Num=1 H
   [] {Nth T Num-1}
   end
end
in
{Browse {SearchAll fun {$} {Nth [1 2 3 4 5] 1} end}}
end



[edit] Reverse

local 
fun {Rev L}
   H T X in
   L=H|T
   choice
      T=nil [H]
   [] X={Rev T} {Append X [H]}
   end
end
in
{Browse {SearchAll fun {$} {Rev [1 2 3 4 5]} end}}
end