Courses/CS 460/Fall 2005/Homework/Andre Liv/Oct 15

From CSWiki

Jump to: navigation, search

[edit] Homework 3

%Append the elements of two lists
local
   proc {Append Xs Ys Zs}      
       choice        Xs=nil  Zs=Ys
       [] X Xr Zr in Xs=X|Xr Zs=X|Zr {Append Xr Ys Zr}
       end
   end
in
   {Browse {SearchAll proc {$ Zs} {Append [2 4 6 7] [1 3] Zs} end}}   
end


%Calculate the sum of elements in list
local
   proc {Sum List L L1}
       choice  List=nil L1=L
       [] Head Tail in
	  List=Head|Tail {Sum Tail L+Head L1}
       end
   end
in
   {Browse {SearchAll proc {$ L} {Sum [3 4 9 0 2] 0 L} end}}
end

%the sum is : 18


%Delete an element in List
local Delete in
    proc {Delete List D ?Out} Head Tail in
       choice
	    List=nil  Out=nil
	[]
            List = Head|Tail
            (D==Head) = false
            Out = Head|{Delete Tail D}
       []
	    List = Head|Tail
            (D==Head) = true
            Out = {Delete Tail D}
        end
    end
       {Browse {SearchAll proc {$ Out} {Delete [1 2 5 4 5 21 2 3 2 6 8] 2 Out} end}}
end

Personal tools