Courses/CS 460/Fall 2005/Homework/Duc Trinh/Oct 15

From CSWiki

Jump to: navigation, search

Contents


[edit] Homework 3

[edit] MyZip

  • returns the list of all elements Zi computed by applying {P Xi Yi}, where Xi is the ith element of Xs and Yi the ith element of Ys. The two input lists must be of equal length, else an error exception is raised.
  • it works but I get an extra character '|' in between the element in the list.
local List1 List2
   proc {MyZip Xs Ys P ?Zs}
      Hx Tx Hy Ty in
      Xs = Hx | Tx
      Ys = Hy | Ty
      choice	 
	 Zs = {P Hx Hy} | {MyZip Tx Ty P}
      []
	 Xs = Hx | nil
	 Ys = Hy | nil
	 Zs = {P Hx Hy}
      end
   end
in
   List1 = [3 4 4 1]
   List2 = [5 1 3 2]
   {Browse {SearchAll proc {$ Y} {MyZip List1 List2 Max Y} end}}
end

[edit] MyLength

  • returns the length of Xs
local
   proc {MyLength Xs ?I}
      Head Tail in
      Xs = Head | Tail
      choice
	 Xs = Head | nil
	 I = 1
      []
	 I = 1 + {MyLength Tail}
      end
   end
in
   {Browse {SearchAll proc {$ Y} {MyLength [2 3 53 12 324] Y} end}}
end

[edit] MyNth

  • returns the Ith element of Xs (counting from 1).
local
   proc {MyNth Xs I ReturnElement}
      Head Tail in
      Xs = Head | Tail
      choice
	 I = 1
	 ReturnElement = Head
      []
	 {MyNth Tail I-1 ReturnElement}
      end
   end
in
   {Browse {SearchAll proc {$ Y} {MyNth [5 23 4321 43 532] 2  Y} end}}
end

[edit] MySubtract

  • binds Zs to Xs without the leftmost occurrence of Y if there is one.
local
   proc {MySubtract Xs Y ?Z}
      Head Tail in
      Xs = Head | Tail
      choice
	 Head = Y
	 Z = Tail
      []
	 Z = Head | {MySubtract Tail Y}
      end
   end
in
   {Browse {SearchAll proc {$ Y} {MySubtract [23 454 43 21] 454 Y} end}}
end

[edit] MyTake

  • returns the list that contains the first I elements of Xs, or Xs if it is shorter
  • it runs but can't return the list if I is larger than the length of the list.
local
   proc {MyTake Xs I ?Ys}
      Head Tail in
      Xs = Head | Tail
      choice
	 I = 1
	 Ys = Head
      []
	 Ys = Head | {MyTake Tail I-1}
      end
   end
in
   {Browse {SearchAll proc {$ Y} {MyTake [2 3 53 12 324] 3 Y} end}}
end

My user page.