Courses/CS 460/Fall 2005/Homework/Kelly Breed/Oct 15
From CSWiki
< Courses | CS 460 | Fall 2005 | Homework | Kelly Breed
Contents |
[edit] HW 3
[edit] Member
This returns true on finding a match against a list.
local
proc {Member L M ?O}
H T in
choice
L=nil fail
[] L=H|T H=M O=true
[] L=H|T {Member T M O}
end
end
in
{Browse {SearchAll fun {$} {Member ['one' 'two' 'three' 'four'] 'three'} end }}
end
[edit] Last
Returns the last element of a list.
local
proc {Last I ?Out}
H T in
choice
I = nil fail
[] I=H|nil Out=H
[] I=_|T {Last T Out}
end
end
in
{Browse {SearchAll fun {$} {Last [2 4 5 8 6]} end}}
end
[edit] Nth
Returns the Nth element of a list.
local
proc {Nth L I S ?Out}
H T in
choice
L=nil fail
[] L=H|T S=I Out=H
[] L=_|T {Nth T I S+1 Out}
end
end
in
{Browse {SearchAll fun {$} {Nth [7 5 3 8 9] 4 1} end }}
end
[edit] Length
Returns the length of a list.
local
proc {Length I S ?Out}
H T in
choice
I=nil fail
[] I=H|nil Out=S
[] I=_|T {Length T S+1 Out}
end
end
in
{Browse {SearchAll fun {$} {Length [a b c d e] 1} end}}
end
[edit] Permute
local
proc {Delete L M ?Out}
H T in
L=H|T
choice
M=H
Out=T
[] Out=H|{Delete T M}
end
end
proc {Permute L ?Out}
Element Rest in
choice
L=nil
Out=nil
[]
Rest = {Delete L Element}
Out = Element|{Permute Rest}
end
end
in
{Browse {SearchAll fun {$} {Permute [a b c d]} end}}
end

