趣味编程:静夜思

C#版本摘自 老赵点滴 - 追求编程之美
 

C#代码

using System; using System.Linq; using System.Text; namespace ConsoleApplication1 { class Program { static void Print(string text, int offset) { text .Select((c, i) => new { Char = c, Index = i }) .GroupBy(p => p.Index % offset, p => p.Char.ToString()) .ToList() .ForEach(g => Console.WriteLine(string.Join("|", g.Reverse().ToArray()))); } static void Main(string[] args) { Print("床前明月光疑是地上霜举头望明月低头思故乡", 5); } } } //低|举|疑|床 //头|头|是|前 //思|望|地|明 //故|明|上|月 //乡|月|霜|光

F#代码

open System let print (text : string) offset = text.ToCharArray() |> Array.mapi (fun i c -> (i, c)) |> Seq.groupBy (fun (i, _) -> i % offset) |> Seq.map (fun (_, s) -> s |> Seq.map (fun (_, c) -> c.ToString()) |> Seq.reduce (fun c1 c2 -> c2 + "|" + c1)) |> Seq.iter (fun str -> printfn "%s" str) print "床前明月光疑是地上霜举头望明月低头思故乡" 5 //低|举|疑|床 //头|头|是|前 //思|望|地|明 //故|明|上|月 //乡|月|霜|光

你可能感兴趣的:(编程,c,String,C#,F#,fun)