Loading ...
Sorry, an error occurred while loading the content.
 

Coding style question

Expand Messages
  • Richard Jones
    I want to produce a list of dates. The first date in the list will be today s date. The second element will be the same day one week previously. The third
    Message 1 of 38 , Sep 27, 2005
      I want to produce a list of dates. The first date in the list will be
      today's date. The second element will be the same day one week
      previously. The third element, two weeks previously, and so on. For
      a given number of weeks. So for example, if run today, the results
      might be:

      [ 2005/09/27, 2005/09/20, 2005/09/13, 2005/09/06 ]

      This is very easy to implement in an imperative style. This is my
      imperative implementation:

      let n_weeks = 4;;
      let date = Date.today ();;
      let dates = Array.make n_weeks date;;
      for i = 1 to n_weeks-1 do
      dates.(i) <- Date.prev dates.(i-1) `Week
      done;;
      let dates = Array.to_list dates;;

      # List.map (Printer.DatePrinter.to_string) dates;;
      - : string list = ["2005-09-27"; "2005-09-20"; "2005-09-13"; "2005-09-06"]

      However I'm struggling to find a good way to write this functionally
      using higher-order functions. It doesn't look like map or fold_* fit
      this iteration pattern well, and I'm wondering if there isn't a case
      for some other HOF to deal with this pattern.

      Can someone tell me what I'm missing?

      Rich.

      --
      Richard Jones, CTO Merjis Ltd.
      Merjis - web marketing and technology - http://merjis.com
      Team Notepad - intranets and extranets for business - http://team-notepad.com
    • Seth J. Fogarty
      ... I am not certain what the difference is between closures and CPS. CPS, in OCaml, is implemented using closures. I ll also note that CPS can be considered a
      Message 38 of 38 , Oct 2, 2005
        On 10/1/05, Oliver Bandel <oliver@...-berlin.de> wrote:
        > On Fri, Sep 30, 2005 at 05:03:08AM -0500, Seth J. Fogarty wrote:
        > > CPS gives you more explicit control over the control flow. Monads give
        > > you abstract explicit control over the control flow. They both also
        > > let you play with return types, which is very useful in multi-stage
        > > programming. These are the primary features for what I use them.
        > [...]
        >
        >
        > Which style best using for automatically closing e.g. filedescriptors
        > after read was completed?
        >
        > Using closures, CPS, Monads, .... ?
        >
        > Isn't CPS-style good suited for it, because the continuation
        > can automagically close the descriptor after the woprk is done?

        I am not certain what the difference is between closures and CPS. CPS,
        in OCaml, is implemented using closures.

        I'll also note that CPS can be considered a monad. (The Essence of
        Functional Programing paper covers this).

        I have never used CPS for this, but you very well could. OTOH CPS
        tends to delay execution, and since working with file descriptors
        tends to rely on order of execution, I'd be leary. Input and output
        tend to be pretty stateful in OCaml, and I generally use very explicit
        direct style with lots of let bindings to enforce order.

        >
        >
        > Ciao,
        > Oliver
        >
        >
        > Archives up to August 22, 2005 are also downloadable at http://www.connettivo.net/cntprojects/ocaml_beginners/
        > The archives of the very official ocaml list (the seniors' one) can be found at http://caml.inria.fr
        > Attachments are banned and you're asked to be polite, avoid flames etc.
        >
        >
        >
        >
        > SPONSORED LINKS
        > Basic programming language Computer programming languages Programming languages
        > Java programming language The history of computer programming language
        >
        > ________________________________
        YAHOO! GROUPS LINKS
        >
        >
        >
        > Visit your group "ocaml_beginners" on the web.
        >
        > To unsubscribe from this group, send an email to:
        > ocaml_beginners-unsubscribe@yahoogroups.com
        >
        > Your use of Yahoo! Groups is subject to the Yahoo! Terms of Service.
        >
        > ________________________________




        --
        Seth Fogarty sfogarty@[gmail.com|rice.edu|livejournal]
        Neep-neep at large AIM: Sorrath
        "I know there are people in this world who do not love their fellow
        human beings - and I hate people like that" --Tom Lehrer.
      Your message has been successfully submitted and would be delivered to recipients shortly.