Ich glaube nicht, dass es sinnvoll ist, dass ein faktorieller Stream irgendwelche Eingaben nimmt, da faktorielle Daten eine spezifische Sequenz bilden.
Stattdessen müssen Sie eine Hilfefunktion, die den internen Zustand verwaltet, und dann der Strom selbst ist nur ein Wert (n und n!):
local
(* returns a stream consisting of
nFac,
nFac * nextN,
nFac * nextN * (nextN + 1),
nFac * nextN * (nextN + 1) * (nextN + 2),
...
*)
fun facHelper (nFac, nextN) = Cons (nFac, fn() => facHelper (nFac * nextN, nextN + 1))
in
(* a stream consisting of 0!, 1!, 2!, 3!, ... *)
val factorials = facHelper (1, 1)
end
Alternativ können Sie eine erstellen Strom von den Zahlen 1, 2, 3, & hellip ;, und einem Strom-Reduzierer, dass bei einer Strömung s, liefert einen Strom von 1, s , ss , sss , & hellip ;:
local
(* returns a stream consisting of n, n + 1, n + 2, n + 3, ... *)
fun positiveIntegersHelper n = Cons (n, fn() => positiveIntegersHelper (n + 1))
in
(* a stream consisting of 1, 2, 3, 4, ... *)
val positiveIntegers = positiveIntegersHelper 1
end
(* Note: the above could also have been written using your 'from' function, as
val positiveIntegers = from 1 (fn n => n + 1)
*)
(* given a function f, an initial value seed, and a stream consisting of [s1, s2, s3, ...],
returns a stream consisting of
[seed, f(s1, seed), f(s2, f(s1, seed)), f(s3, f(s2, f(s1, seed))), ...]
-- so, somewhat analogous to List.foldl, except that it returns a stream of all partial
results, instead of just the final result.
*)
fun reduceStream _ seed Nil = Cons (seed, fn() => Nil)
| reduceStream f seed (Cons (h, tf)) =
Cons (seed, fn() => reduceStream f (f (h, seed)) (tf()))
(* a stream consisting of 0!, 1!, 2!, 3!, ... *)
val factorials = reduceStream op* 1 positiveIntegers
SML hat keine eingebaute in Begriff Strom. Was ist ein unendlicher Faktorstrom und was bedeutet es zu sagen, dass es eine Funktion von zwei Strömen ist? Was ist "Nachteile"? Bitte klären Sie Ihre Frage. –
Was gibt es gegen was Sie erwarten? Und hast du versucht es zu debuggen? – Carcigenicate