* [Caml-list] small exercise @ 2002-11-29 15:19 altavillasalvatore 2002-11-29 15:29 ` Xavier Leroy ` (2 more replies) 0 siblings, 3 replies; 6+ messages in thread From: altavillasalvatore @ 2002-11-29 15:19 UTC (permalink / raw) To: caml-list Hi all, I do not succeed to develop the following exercise: I have need of your aid. input: string "1.15.120.13.43.35.340.190.0" output: int array= [| 1; 15; 120; 13; 43; 35; 340; 190; 0; |] : string -> int array Regards. ------------------- To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/ Beginner's list: http://groups.yahoo.com/group/ocaml_beginners ^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: [Caml-list] small exercise 2002-11-29 15:19 [Caml-list] small exercise altavillasalvatore @ 2002-11-29 15:29 ` Xavier Leroy 2002-11-29 18:29 ` Pierre Weis 2002-11-29 15:30 ` Christophe Raffalli 2002-11-29 15:32 ` Jean-Christophe Filliatre 2 siblings, 1 reply; 6+ messages in thread From: Xavier Leroy @ 2002-11-29 15:29 UTC (permalink / raw) To: altavillasalvatore; +Cc: caml-list > input: string "1.15.120.13.43.35.340.190.0" > output: int array= [| 1; 15; 120; 13; 43; 35; 340; 190; 0; |] That's easy: let answer_to_the_exercise = function "1.15.120.13.43.35.340.190.0" -> [| 1; 15; 120; 13; 43; 35; 340; 190; 0; |] I'm sure your professor will love this solution, as it is not only correct w.r.t. the specification, but also quite concise. - Xavier Leroy P.S.1: this list isn't for doing student's homework. P.S.2: the beginner's list http://groups.yahoo.com/group/ocaml_beginners isn't either, but you might find more sympathy there. ------------------- To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/ Beginner's list: http://groups.yahoo.com/group/ocaml_beginners ^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: [Caml-list] small exercise 2002-11-29 15:29 ` Xavier Leroy @ 2002-11-29 18:29 ` Pierre Weis 0 siblings, 0 replies; 6+ messages in thread From: Pierre Weis @ 2002-11-29 18:29 UTC (permalink / raw) To: Xavier Leroy; +Cc: altavillasalvatore, caml-list > > input: string "1.15.120.13.43.35.340.190.0" > > output: int array= [| 1; 15; 120; 13; 43; 35; 340; 190; 0; |] > > That's easy: > > let answer_to_the_exercise = function > "1.15.120.13.43.35.340.190.0" -> > [| 1; 15; 120; 13; 43; 35; 340; 190; 0; |] > > I'm sure your professor will love this solution, as it is not only > correct w.r.t. the specification, but also quite concise. As a long time Caml teacher, I would prefer this one :) let answer_to_the_exercise = function | "1.15.120.13.43.35.340.190.0" -> | 1; 15; 120; 13; 43; 35; 340; 190; 0; |] | _ -> assert false;; Never show a student a non exhaustive pattern matching :) Pierre Weis INRIA, Projet Cristal, Pierre.Weis@inria.fr, http://pauillac.inria.fr/~weis/ ------------------- To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/ Beginner's list: http://groups.yahoo.com/group/ocaml_beginners ^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: [Caml-list] small exercise 2002-11-29 15:19 [Caml-list] small exercise altavillasalvatore 2002-11-29 15:29 ` Xavier Leroy @ 2002-11-29 15:30 ` Christophe Raffalli 2002-11-29 18:48 ` Pierre Weis 2002-11-29 15:32 ` Jean-Christophe Filliatre 2 siblings, 1 reply; 6+ messages in thread From: Christophe Raffalli @ 2002-11-29 15:30 UTC (permalink / raw) To: altavillasalvatore; +Cc: caml-list altavillasalvatore@libero.it wrote: > Hi all, > > I do not succeed to develop the following exercise: > I have need of your aid. > > > > input: string "1.15.120.13.43.35.340.190.0" > > output: int array= [| 1; 15; 120; 13; 43; 35; 340; 190; 0; |] > > : string -> int array > > > Regards. I hope you are not a student trying to have his home work done by this mailing list ? Just a hint look at the documentation of the following functions: module | function ------------------------------ str | split list | map pervasives | int_of_string array | of_list The answer fits on one (long) line. -- Christophe Raffalli Université de Savoie Batiment Le Chablais, bureau 21 73376 Le Bourget-du-Lac Cedex tél: (33) 4 79 75 81 03 fax: (33) 4 79 75 87 42 mail: Christophe.Raffalli@univ-savoie.fr www: http://www.lama.univ-savoie.fr/~RAFFALLI ------------------- To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/ Beginner's list: http://groups.yahoo.com/group/ocaml_beginners ^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: [Caml-list] small exercise 2002-11-29 15:30 ` Christophe Raffalli @ 2002-11-29 18:48 ` Pierre Weis 0 siblings, 0 replies; 6+ messages in thread From: Pierre Weis @ 2002-11-29 18:48 UTC (permalink / raw) To: Christophe Raffalli; +Cc: altavillasalvatore, caml-list > altavillasalvatore@libero.it wrote: > > Hi all, > > > > I do not succeed to develop the following exercise: > > I have need of your aid. > > > > > > > > input: string "1.15.120.13.43.35.340.190.0" > > > > output: int array= [| 1; 15; 120; 13; 43; 35; 340; 190; 0; |] > > > > : string -> int array > > > > > > Regards. [...] > Just a hint look at the documentation of the following functions: > > module | function > ------------------------------ > str | split > list | map > pervasives | int_of_string > array | of_list > > The answer fits on one (long) line. > -- > Christophe Raffalli I guess, you forgot the most appropriate module for the exercicise: Scanf. Indeed, scanning the given input string to obtain the expected vector only using Scanf.scanf is interesting and not completely trivial. An additional question could be: write a general polymorphic reader that can input vectors with elements of any simple basic type (say bool, int, string, char, float). Define a string reader via partial application of the polymorphic reader. Last but not least could be: generalize (if necessary) the polymorphic reader to read vectors of any given type. Apply this function to read matrices of floating point numbers. Discuss the use of Scanf in place of a combination of modules Str, List, Pervasives, Array, ... Mmm. A trivial exercicise could show up to be interesting afterwards :) Pierre Weis INRIA, Projet Cristal, Pierre.Weis@inria.fr, http://pauillac.inria.fr/~weis/ ------------------- To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/ Beginner's list: http://groups.yahoo.com/group/ocaml_beginners ^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: [Caml-list] small exercise 2002-11-29 15:19 [Caml-list] small exercise altavillasalvatore 2002-11-29 15:29 ` Xavier Leroy 2002-11-29 15:30 ` Christophe Raffalli @ 2002-11-29 15:32 ` Jean-Christophe Filliatre 2 siblings, 0 replies; 6+ messages in thread From: Jean-Christophe Filliatre @ 2002-11-29 15:32 UTC (permalink / raw) To: altavillasalvatore; +Cc: caml-list > input: string "1.15.120.13.43.35.340.190.0" > > output: int array= [| 1; 15; 120; 13; 43; 35; 340; 190; 0; |] Objective Caml version 3.06 # #load "str.cma";; # List.map int_of_string (Str.split (Str.regexp "\\.") "1.15.120.13.43.35.340.190.0");; - : int list = [1; 15; 120; 13; 43; 35; 340; 190; 0] ^ permalink raw reply [flat|nested] 6+ messages in thread
end of thread, other threads:[~2002-11-29 18:48 UTC | newest] Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed) -- links below jump to the message on this page -- 2002-11-29 15:19 [Caml-list] small exercise altavillasalvatore 2002-11-29 15:29 ` Xavier Leroy 2002-11-29 18:29 ` Pierre Weis 2002-11-29 15:30 ` Christophe Raffalli 2002-11-29 18:48 ` Pierre Weis 2002-11-29 15:32 ` Jean-Christophe Filliatre
This is a public inbox, see mirroring instructions for how to clone and mirror all data and code used for this inbox