CS-460-1.Fall-2005:Homeworks:KellyBreedHomework
From CSWiki
Contents |
[edit] HW 1
[edit] Last Element of a List
It's been pointed out to me that there is a builtin for this, but this is my story and I'm sticking to it.
% Returns the last element of a list
declare
fun {Last S}
case S of H|T then
if T == nil then H
else {Last T}
end
end
end
local C N in
C=['Scarecrow' 'Tinman' 'Cowardly Lion']
{Browse {Last C}}
N=[4 6 3 4 8 9 7 6 54 21]
{Browse {Last N}}
end
[edit] Stripped Down Tree
%An extremely simple tree insertion procedure
local T T1 T2 T3 T4 Value TreeIn TreeOut Insert in
proc {Insert Value TreeIn TreeOut}
case TreeIn
of nil then TreeOut = tree(Value nil nil)
[] tree(V Ti To) then
if Value < V then Tmp in
TreeOut = tree(V Tmp To)
{Insert Value Ti Tmp}
else Tmp in
TreeOut = tree(V Ti Tmp)
{Insert Value Ti Tmp}
end
end
end
T = nil
{Insert 7 T T1}
{Insert 9 T1 T2}
{Browse T2}
end
[edit] Capitalize First Character of a String
% Capitalizes the first character of a string.
local S O CapFirst StringIn StringOut in
proc {CapFirst StringIn StringOut}
local H T O in
StringIn = H|T
{Char.toUpper H O}
StringOut = O|T
end
end
S = "this is an uncapitalized sentence."
{CapFirst S O}
{Browse O}
end
[edit] Average a List
% Gives average of a list of floats
local A B Average ListIn Out C L LF in
proc {Average ListIn Out}
L = {Length ListIn}
LF = {IntToFloat L}
{NewCell 0.0 C}
for X in ListIn do C:=@C+X end
Out = @C/LF
end
A = [3.0 2.0]
{Average A B}
{Browse B}
end

