🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: alt.lang.ml
Article #6

Re: can this be implemented in O(1) space?

#6
From: Scott Draves
Date: Mon, 06 Apr 1992 19:37
24 lines
1396 bytes
>>>>> On 6 Apr 92 02:51:26 GMT, jgore@Mines.Colorado.Edu (Jacob Gore) said:

Jacob> fun power(x,n) =
Jacob>   if n=0 then 1.0
Jacob>   else if n mod 2 = 1 then x * power(x,n-1)
Jacob>   else power(x*x, n div 2);

Jacob> What is the likelihood of an SML implementation doing it in O(1) space?

well, i don't know the internals of any SML implementations, but one
would assume that the answer is 0% due to the non-tail recursive call
in the second case.  you can easily rewrite the function so that most
compilers will take O(1) space: 

 fun power(x,n,r) : int =
   if n=0 then r
   else if n mod 2 = 1 then power(x,n-1,x*r)
   else power(x*x, n div 2,r);

call it with r=1
-- 
			conform
scott draves		deform
spot@cs.cmu.edu		explode
Technical Details
Message-ID: <SPOT.92Apr6143708@HOPELESS.MESS.CS.CMU.EDU>
Path: arc1-ca.pugleaf.net!sparky!uunet!haven.umd.edu!darwin.sura.net!udel!rochester!cantaloupe.srv.cs.cmu.edu!crabapple.srv.cs.cmu.edu!spot
References: <1992Apr06.025126.33552@slate.mines.colorado.edu>

Show Full Headers
Complete Article Headers
Newsgroups: alt.lang.ml
Path: arc1-ca.pugleaf.net!sparky!uunet!haven.umd.edu!darwin.sura.net!udel!rochester!cantaloupe.srv.cs.cmu.edu!crabapple.srv.cs.cmu.edu!spot
From: spot@CS.CMU.EDU (Scott Draves)
Subject: Re: can this be implemented in O(1) space?
Message-ID: <SPOT.92Apr6143708@HOPELESS.MESS.CS.CMU.EDU>
Date: Mon, 06 Apr 92 19:37:08 GMT
Organization: School of Computer Science, Carnegie Mellon University
Nntp-Posting-Host: hopeless.mess.cs.cmu.edu
In-Reply-To: jgore@Mines.Colorado.Edu's message of 6 Apr 92 02:51:26 GMT
References: <1992Apr06.025126.33552@slate.mines.colorado.edu>
Originator: spot@HOPELESS.MESS.CS.CMU.EDU
Lines: 24