On 01/25/2012 09:22 PM, Serge D. Mechveliani wrote:
Consider the following program that counts by decreasing n by a string
length.
-- file ct.input -------------------------------------
all x == true
ct(n : Integer) : Integer ==
str := "abc"
i := n
while i> 0 repeat i := i - count(x +-> true, str)
return i
--------------------------------------------------------
Is count(x +-> true, str) the simplest way to find a string length?
Go to the hyperdoc window, click "Browse" and enter "String", click
"domains", click "operations", click "#".
(1) -> # "abc"
(1) 3
Type: PositiveInteger
(2) -> # "abcdefg"
(2) 7
Type: PositiveInteger
Is ct written in the Spad language?
Almost, but actually, no. You have written an .input file. All what is
in there, is basically the same as if you type it in the interpreter.
Any *real* programming should be done with .spad files. The .input files
should just be used if you have similar input commands and don't want to
*type* them in again and again.
If you want to *compile* something, you would have to put your function
into a .spad file. But that is not all. To my knowledge, you have to put
a wrapper around it.
I don't quite get the real sense of your ct function, but in the .spad
file it should look like this.
--- ct.spad
)abbrev package COUNT Count
Count(): with
ct: Integer -> Integer
== add
ct(n: Integer): Integer ==
str: String := "abc"
i := n
while i > 0 repeat
c := count((x: Character): Boolean +-> true, str)
i := i - (c::Integer)
return i
--- end ct.spad
Of course, you could forget about the count and write "i := i - #(str)"
instead.
Ralf
--
You received this message because you are subscribed to the Google Groups "FriCAS -
computer algebra system" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/fricas-devel?hl=en.