Tag Archives: sequences

Generating F# Sequences From Console Input

I got a very nice answer on SO to why a function was not executing.


open System
open System.Collections.Generic
open System.Text
open System.IO
#nowarn "40"

let rec readlines () =
     seq {
           let line = Console.ReadLine()
           if not (line.Equals("")) then
              yield line
              yield! readlines ()
}

[<EntryPoint>]
let main argv =
      let inSeq = readlines ()

      inSeq
      |> Seq.length
      |> printfn "%d lines read"

      // This will keep it alive enough to read your output
      Console.ReadKey() |> ignore
      0

Advertisements

Leave a comment

Filed under F#

Clojure: To Recurse or Not To Recurse

As I start week number 5 of my CodeLesson Clojure course and look at programming examples, it is clear to me that when to use recursion or not is a fuzzy science. Most Clojure text books talk about using sequences wherever possible and not even using loop/recur (Clojure’s tail recursion method)  let alone recursion without loop/recur. The Clojure Google Groups even called this method low-level while pushing using sequences.

One assignment in particular I found particularly tricky, because it involved how to make change for some number of dollar bills, and the official solution used recursion even without wrapping the sequence inside a lazy-seq function. So, I conclude individual preference and what makes sense will have to prevail concerning Clojure and recursion.

3 Comments

Filed under Clojure