Jump to content

Boustrophedon transform: Difference between revisions

→‎F#: Remove mutable data probably more idiotmatic
(New post.)
(→‎F#: Remove mutable data probably more idiotmatic)
Line 251:
<syntaxhighlight lang="fsharp">
// Boustrophedon transform. Nigel Galloway:October 26th., 2023
let fG n g=let rec fG n g=[match gn with h::[]->yield ng+h |h::t->yield ng+h; yield! fG t (ng+h) t] in [yield ng; yield! fG n g]|>List.rev
let rec Boustrophedon n g=seq{yield ListSeq.headscan(fun n g;->fG yield!n Boustrophedong)[Seq.head n] (fGSeq.tail (n()|>Seq.map(List.head) g)}
 
</syntaxhighlight>
 
===The Task===
<syntaxhighlight lang="fsharp">
printfn "zeroes"; Boustrophedon Seq.unfold(fun() n->Some(n,0I) [)1I]|>Seq.take 15|>Boustrophedon|>Seq.iter(printf "%A "); printfn ""
let n=sprintf $"%A{Boustrophedon Seq.unfold(fun() n->Some(n,0I) [)1I]|>Boustrophedon|>Seq.item 999}" in printfn "%s ... %s (%d digits)" n[0..19] n[n.Length-20..n.Length] (n.Length)
printfn "ones"; Boustrophedon Seq.unfold(fun() n->1ISome(n,n)) [1I]|>Seq.take 15|>Boustrophedon|>Seq.iter(printf "%A "); printfn ""
let n=sprintf $"%A{Boustrophedon Seq.unfold(fun() n->1ISome(n,n)) [1I]|>Boustrophedon|>Seq.item 999}" in printfn "%s ... %s (%d digits)" n[0..19] n[n.Length-20..n.Length] (n.Length)
printfn "1,-1,1,-1....."; Boustrophedon Seq.unfold(let mutablefun n=1I in (fun()->Some(n<- ,-n; n)) [1I]|>Boustrophedon|>Seq.take 15|>Seq.iter(printf "%A "); printfn ""
let n=sprintf $"%A{Boustrophedon Seq.unfold(let mutablefun n=1I in (fun()->Some(n<- ,-n; n)) [1I]|>Boustrophedon|>Seq.item 999}" in printfn "%s ... %s (%d digits)" n[0..19] n[n.Length-20..n.Length] (n.Length)
printfn "factorials"; Boustrophedon Seq.unfold(fun(let mutable n,g=1I,2I in (fun()->let r=Some(n in n<-,(n*g; g<-,g+1I; r)) [)(1I],1I)|>Boustrophedon|>Seq.take 15|>Seq.iter(printf "%A "); printfn ""
let n=sprintf $"%A{Boustrophedon Seq.unfold(fun(let mutable n,g=1I,2I in (fun()->let r=Some(n in n<-,(n*g; g<-,g+1I; r)) [)(1I],1I)|>Boustrophedon|>Seq.item 999}" in printfn "%s ... %s (%d digits)" n[0..19] n[n.Length-20..n.Length] (n.Length)
printfn "primes"; let p=primesIprimes() in Boustrophedon Seq.initInfinite(fun() _->Seq.head bigint(p() [Seq.head p]))|>Boustrophedon|>Seq.take 15|>Seq.iter(printf "%A "); printfn ""
let p=primesI() in let n=sprintf $"%A{Boustrophedonlet (funp=primes()->Seq.head p)in [Seq.headinitInfinite(fun _->bigint(p]()))|>Boustrophedon|>Seq.item 999}" in printfn "%s ... %s (%d digits)" n[0..19] n[n.Length-20..n.Length] (n.Length)
 
</syntaxhighlight>
{{out}}
2,172

edits

Cookies help us deliver our services. By using our services, you agree to our use of cookies.