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

Re: Writing a list to a file

Expand Messages
  • Matt Armstrong
    ... So, List.map returns a new list. One way to get rid of a value you re not interested in is to do this: let skryflys l lys = let _ = List.map
    Message 1 of 4 , May 14, 2002
    • 0 Attachment
      Johann Spies <jspies@...> writes:

      > How do I get rid of the warning in the following function:
      > -------------------------------
      > # let skryflys l lys =
      > List.map (Printf.fprintf l "%s\n") lys;
      > close_out l;;
      > Toplevel input:
      > # let skryflys l lys =
      > List.map (Printf.fprintf l "%s\n") lys;
      > ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
      > close_out l;;
      > Warning: this expression should have type unit.
      > val skryflys : out_channel -> string list -> unit = <fun>>

      So, List.map returns a new list. One way to get rid of a value you're
      not interested in is to do this:

      let skryflys l lys =
      let _ = List.map (Printf.fprintf l "%s\n") lys in
      close_out l;;

      That is, you're assigning the result to _, which effectively throws
      the value away.

      But I think using List.iter is closer to what you want, since you
      don't want to create a new list you just want to iterate over the one
      you have:

      let skryflys l lys =
      List.iter (Printf.fprintf l "%s\n") lys;
      close_out l;;


      --
      Don't send mail to Del.Hayes@...
      The address is there for spammers to harvest.
    • Johann Spies
      ... Thanks. I have never used List.iter. This is a good opportunity to start using it. Regards. Johann -- Johann Spies Telefoon: 021-808 4036
      Message 2 of 4 , May 14, 2002
      • 0 Attachment
        On Tue, May 14, 2002 at 04:52:14PM +0200, Remi VANICAT wrote:
        >
        > List.iter is what you need here.

        Thanks. I have never used List.iter. This is a good opportunity to
        start using it.

        Regards.
        Johann
        --
        Johann Spies Telefoon: 021-808 4036
        Informasietegnologie, Universiteit van Stellenbosch

        "Yea, though I walk through the valley of the shadow of
        death, I will fear no evil, for thou art with me;
        thy rod and thy staff they comfort me." Psalms 23:4
      Your message has been successfully submitted and would be delivered to recipients shortly.