Xah Talk Show 2026-01-08 Ep740 Learning Fsharp. Write a Find Replace Script. Part 3.
Video Summary (Generated by AI, Edited by Human.)
- Write a find and replace script, similar to Unix's sed command, but with more features (0:42).
- Planned features include: Optional backup. backup with current datetime stamp. (1:06). Regular expression support (1:15). Ability to handle multiple find and replace pairs in one go (1:18).
- Emacs lisp version (2:07) and Golang, Python, Perl, Emacs Lisp, and Wolfram Language (2:32).
- Current Script Overview. (3:39).
- How to get the current datetime in fsharp. (6:40).
- Exploring different date and time formats, including ISO 8601 (11:02) and a programmer-friendly format (21:51).
- A digression on the complexity of date and time in programming due to calendars and astronomy (14:17).
- How to copy file.(32:35).
- Learning fsharp: how to count occurrence in replace string.
- Comparing old and new string content (42:48), ask grok AI (43:13) and encountering issues with AI answer (47:50, 55:08, 1:02:02).
- bad AI example of leaky recursive function. (54:22).
fsharp, date time
- how to get current date in fsharp
- in fsharp how to get date time in this format 2026-01-08_143718
- https://x.com/i/grok/share/XNIZ7Zfs86HtvCyfYqvzep2If
Fsharp, copy file
- how to copy a file in fsharp
- https://x.com/i/grok/share/VS7Bx7ttUTMY0UNLOtB9djb8o
// copy file example open System.IO File.Copy("c:/Users/xah/web/xahlee_info/talk_show/xah_talk_show_ep740.html", "c:/Users/xah/web/xahlee_info/talk_show/xah_talk_show_ep740.html~x~") // Does not overwrite if destination exists // Or, to allow overwriting an existing destination file: // File.Copy("source.txt", "destination.txt", overwrite = true)
fsharp, count occurance
- in fsharp, when you do xcontent.Replace(xfindString, xreplaceString), how to find if replace actually happened
- https://x.com/i/grok/share/yikWcC1PXoOA2oOGGvaHaXV81
// 2026-01-08 // this is a example of finding count of occurance // does not work open System // for StringComparison if needed let xinputText = "Alice was beginning to get very tired of sitting by her sister on the bank, and of having nothing to do: once or twice she had peeped into the book her sister was reading, but it had no pictures or conversations in it, “and what is the use of a book,” thought Alice “without pictures or conversation?”." let xfindStr = "Alice" let count = let rec floop zindex xcount = let pos = xinputText.IndexOf(xfindStr, zindex) if pos = -1 then xcount else floop (pos + xfindStr.Length) (xcount + 1) floop 0 0 let didReplaceHappen = count > 0 let result = xinputText.Replace(xfindStr, replaceWith) // printfn "didReplaceHappen is %s" didReplaceHappen
// 2026-01-08 // this is a example of finding count of occurance // does not work open System.Text.RegularExpressions // open System.Text.Regex // open System // for StringComparison if needed let xinputText = "Alice was beginning to get very tired of sitting by her sister on the bank, and of having nothing to do: once or twice she had peeped into the book her sister was reading, but it had no pictures or conversations in it, “and what is the use of a book,” thought Alice “without pictures or conversation?”." let xfindStr = "Alice" let xreplaceStr = "Queen" let regex = Regex(Regex.Escape(xfindStr)) // escape if needed // let matchResult = regex.Replace(xinputText, xreplaceStr, count = Int32.MaxValue) // the new string // In .NET 8+, you can get the count directly: let replaceResult = regex.Replace(xinputText, xreplaceStr, count = Int32.MaxValue, startingPosition = 0) let didReplaceHappen = replaceResult.ResultCount > 0
- how to write if in fsharp
- https://x.com/i/grok/share/n0h2hYt8iMzqq5Che3yggaYDv
Fsharp, find replace script
// find and replace, of all files in a dir // 2026-01-03 open System.IO // for getting dir content open System // for DateTime let xinputpath = "c:/Users/xah/web/xahlee_info/talk_show/xxelisp/" let xfilenameExtensionGlob = "A*.html" let xfindString = "Saving" let xreplaceString = "xSaving" // s------------------------------ let xdatetime = DateTime.Now.ToString("yyyy-MM-dd_HHmmss") // todo. make backup. with timestamp in the filename. // Or, to allow overwriting an existing destination file: // File.Copy("source.txt", "destination.txt", overwrite = true) // All files recursively let xfileList = Directory.EnumerateFiles(xinputpath, xfilenameExtensionGlob , SearchOption.AllDirectories) // process each file let fprocessFile (xfilePath: string) = let xcontent: string = File.ReadAllText(xfilePath) let xnewContent = xcontent.Replace(xfindString, xreplaceString) File.WriteAllText(xfilePath, xnewContent) // todo. modify it so it only print changed file // in fsharp, when you do xcontent.Replace(xfindString, xreplaceString), how to find if replace actually happened if (xcontent <> xnewContent) then printfn "changed %s" xfilePath else printfn "no change %s" xfilePath // walk the dir list xfileList |> Seq.iter fprocessFile printfn "done"