Version française
Home     About     Download     Resources     Contact us    

This site is updated infrequently. For up-to-date information, please visit the new OCaml website at ocaml.org.

Browse thread
Writing a list to a file
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: 2002-05-14 (06:22)
From: Warp <warplayer@f...>
Subject: Re: "ocaml_beginners"::[] Re: Writing a list to a file
> Johann Spies <jspies@s...> 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.
>

Actualy I would prefer the "ignore" function :

let skryflys l lys =
ignore (List.map (...) lys);
close_out l;;

Which is "almost" the same as "let _ = " but the compiler prints a Warning
if you're doing partial application - which really would be stupid : but
sometimes you can simply forgot a parameter and then debugging that kind of
thing is REALY hard since your function call is not made.

Nicolas Cannasse