Ralf Hemmecke wrote:
>
> Hi Waldek,
>
> are you working also on enabling non-pile mode for SPAD. For library
> code writing I somehow like braces more than all this indentation.
>
> Any chance that this gets a higer priority on your list?
This has relatively high priority. But I plan to do this together
with other changes to Spad parser and this is larger task. Non-pile
mode alone would be small task, but I dislike the idea of implementing
it only to to trow it out in near future due to incompatiblity with
other changes.
>
> Also I love Aldor's Generator... Any chance for having that in SPAD?
>
I just commited (easy) part of implementation of generators. It is
now possible to use generators for iteration, however creating
generators is a bit clumsy -- to create generator you need to
give two functions, one for end test (true at end, false otherwise)
and one for stepping (gives value and moves to next state).
This is still experimental, in particular I am not sure if it
is better to keep state in functions (current solution) or to
provide explicit state (should be more efficient, but not clear
about typing).
To try you need code below:
-- actual Generator domain
)abbrev domain GENERAT Generator
Generator(T : Type) : with
emptyFun : % -> (() -> Boolean)
stepFun : % -> (() -> T)
makeGenerator : ((() -> Boolean), (() -> T)) -> %
== add
Rep := Record(empty: (() -> Boolean), step : (() -> T))
emptyFun(x) == x.empty
stepFun(x) == x.step
makeGenerator(ef, sf) == [ef, sf]$Rep
-- helper to create stateful generators
)abbrev package MKGENER MakeGenerator
MakeGenerator(T : Type, S : Type) : with
makeGenerator: (S, S -> Boolean, S -> T) -> Generator(T)
== add
makeGenerator(state, ef, sf) ==
ef1() : Boolean == ef(state)
sf1 : () -> T := () +-> sf(state)
makeGenerator(ef1, sf1)$Generator(T)
-- code using generator
)abbrev package FFF FFF
FFF() : with
myfun : Generator(Integer) -> Integer
== add
myfun(x) ==
res : Integer := 0
for z in x repeat
print(z::OutputForm)$OutputForm
res := res + z
res
-- input file actually using the above
mS := Reference(Integer)
empf(i : mS) : Boolean == deref(i) <= 0
stepf(i : mS) : Integer ==
(pp := divide(deref(i), 3); setref(i, pp.quotient); pp.remainder)
mG := MakeGenerator(Integer, mS)
mygen := makeGenerator(ref(47), empf, stepf)$mG
--
Waldek Hebisch
[email protected]
--
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.