Re: Reading in floats from strings???
"Cierra DuBose [email protected] [ocaml_beginners]" <[email protected]> Thu, 28 Jan 2016 07:25:25 +0000 (UTC)
| Newsgroups | gmane.comp.lang.ocaml.beginners |
|---|---|
| Message-ID | <[email protected]> |
Hi Douglas, The Teaching OCaml section of the OCaml homepage has several schools that use OCaml in certain courses (here is a link to the page); looks like the closest one to you is University of Illinois at Urbana-Champaign, although that's still a bit of a drive from Chicago. However, many of the courses have lecture notes and other resources online. Paris Diderot University recently offered a MOOC freely available for anyone on OCaml, and they mentioned that they plan to run the course again in the near future, so if you're not too sold on using lecture notes for a traditional university course to further your learning, the Paris Diderot MOOC would be your next best bet, aside from reading an online textbook and doing Project Euler exercises in OCaml. Welcome to the OCaml Beginners List, Cierra [email protected]@DuboseIci From: "Douglas Lewit [email protected] [ocaml_beginners]" <[email protected]> To: [email protected] Sent: Wednesday, January 27, 2016 11:34 PM Subject: Re: "ocaml_beginners"::[] Reading in floats from strings??? Thanks guys. These were excellent suggestions, and none of them were that difficult to implement. Thanks again! Sorry about that other message. I didn't realize that the group doesn't allow messages with attachments. ( I guess that's for security reasons, but maybe I'm wrong. ) Just curious, but are there any colleges, universities, online schools, etc, that offer a program of study in Ocaml? Most of the CS programs in my city teach either C, C++ or Java. I don't know of any school around here that offers training in a functional programming language. On Wed, Jan 27, 2016 at 12:28 PM, Gabriel Scherer [email protected] [ocaml_beginners] <[email protected]> wrote: My quick&dirty way to parse numeric values in string format is Scanf, using formats of the kind " %d" (for integers) and " %f" (for floats), with the space having the meaning of "ignore any space before". It is easier to do on the standard input directly: let read_next_float () = Scanf.scanf " %f" (fun x -> x) let () = let x = read_next_float () in let y = read_next_float () in print_float (x +. y) if your input comes from a string, you have to convert it into a scanning buffer first: http://caml.inria.fr/pub/docs/manual-ocaml/libref/Scanf.html http://caml.inria.fr/pub/docs/manual-ocaml/libref/Scanf.Scanning.html let read_next_float input = Scanf.bscanf input " %f" (fun x -> x) let sum_of_string str = let input = Scanf.Scanning.from_string str in let x = read_next_float input in let y = read_next_float input in (x +. y) Please be aware, however, that Scanf is best used for programs where you control the input. It does not perform much input validation, and if you want to provide a convenient user interface to your user and be robust to formatting errors, you should use a more robust parsing strategy. I have never personally been in the situation where regexp parsing felt like the right compromise between expressivity and convenience, I tend to move to full lexer+parser stacks directly (which are easy to use in OCaml, see [sedlex] + [menhir]), but that's a question of habits I guess. sedlex: https://github.com/alainfrisch/sedlex menhir: http://gallium.inria.fr/~fpottier/menhir/ compiling a project using both: http://stackoverflow.com/questions/30853159/using-menhir-with-sedlex On Wed, Jan 27, 2016 at 10:29 AM, Douglas Lewit [email protected] [ocaml_beginners] <[email protected]> wrote: Hi everyone, I am new to this list, so I thought I would introduce myself. Also, I have a question about how to read in data of the form "11.3 12.9". Obviously a string, but I need to extract the floats contained in the string. I know how to do that in Java and also in Python, but for me Ocaml is still pretty much a foreign language. I appreciate any guidance you can offer. Anyhow, I've got a master's in math from a small state university in Chicago, Illinois. I'm currently working on a second master's in CS at the same university. My work experience is mostly in teaching, although I've also done some retail management. I used to be a high school math teacher ( mostly Catholic high schools, but also have some experience in the public school system ) and currently I teach part-time at Oakton Community College ( near Chicago ). I'm interested in leaving education within the next couple of years to find a new career in the software industry. My age could be a factor that works to my disadvantage, since most of the people that I personally know in various startup companies are quite young. ( I've been to some Python and Ruby meetups in the Chicago area, and I get the distinct impression that the vast majority of attendees are under the age of 35. ) Ocaml is appealing to me for a couple of reasons. Well first off, how can I resist a book that has a picture of a lovable camel on its front cover!!! ;-) But on a more serious note, I like Ocaml because it really forces the programmer to get over his fear or hatred of recursion. I think recursion is sort of optional in many other languages, and in fact some programmers really hate it because it does require a different way of thinking about problems. But in Ocaml recursion is not optional. So for me it's a really good exercise in thinking recursively about various problems. Although sometimes I think Ocaml goes a little too far with its emphasis on recursion. I mean.... there are times when a nice little for loop or while loop would do the job with far greater simplicity! From the reading I have done on the web it appears that Ocaml is a big hit with Jane Street, a very large financial trading company with offices in NYC, London and Hong Kong. Outside of Jane Street, where is Ocaml used? Take care and thanks for reading my intro message. Best, Douglas Lewit P.S. Is there any connection between Ocaml and Wolfram Mathematica? Both begin and end comments with (* and *), respectively, and both emphasize the functional approach to programming. I'm a big fan of Mathematica, and often incorporate it into math classes that I teach. I'm just wondering if there's a connection between the two software projects. Thanks. #yiv1007820498 #yiv1007820498 -- #yiv1007820498ygrp-mkp {border:1px solid #d8d8d8;font-family:Arial;margin:10px 0;padding:0 10px;}#yiv1007820498 #yiv1007820498ygrp-mkp hr {border:1px solid #d8d8d8;}#yiv1007820498 #yiv1007820498ygrp-mkp #yiv1007820498hd {color:#628c2a;font-size:85%;font-weight:700;line-height:122%;margin:10px 0;}#yiv1007820498 #yiv1007820498ygrp-mkp #yiv1007820498ads {margin-bottom:10px;}#yiv1007820498 #yiv1007820498ygrp-mkp .yiv1007820498ad {padding:0 0;}#yiv1007820498 #yiv1007820498ygrp-mkp .yiv1007820498ad p {margin:0;}#yiv1007820498 #yiv1007820498ygrp-mkp .yiv1007820498ad a {color:#0000ff;text-decoration:none;}#yiv1007820498 #yiv1007820498ygrp-sponsor #yiv1007820498ygrp-lc {font-family:Arial;}#yiv1007820498 #yiv1007820498ygrp-sponsor #yiv1007820498ygrp-lc #yiv1007820498hd {margin:10px 0px;font-weight:700;font-size:78%;line-height:122%;}#yiv1007820498 #yiv1007820498ygrp-sponsor #yiv1007820498ygrp-lc .yiv1007820498ad {margin-bottom:10px;padding:0 0;}#yiv1007820498 #yiv1007820498actions {font-family:Verdana;font-size:11px;padding:10px 0;}#yiv1007820498 #yiv1007820498activity {background-color:#e0ecee;float:left;font-family:Verdana;font-size:10px;padding:10px;}#yiv1007820498 #yiv1007820498activity span {font-weight:700;}#yiv1007820498 #yiv1007820498activity span:first-child {text-transform:uppercase;}#yiv1007820498 #yiv1007820498activity span a {color:#5085b6;text-decoration:none;}#yiv1007820498 #yiv1007820498activity span span {color:#ff7900;}#yiv1007820498 #yiv1007820498activity span .yiv1007820498underline {text-decoration:underline;}#yiv1007820498 .yiv1007820498attach {clear:both;display:table;font-family:Arial;font-size:12px;padding:10px 0;width:400px;}#yiv1007820498 .yiv1007820498attach div a {text-decoration:none;}#yiv1007820498 .yiv1007820498attach img {border:none;padding-right:5px;}#yiv1007820498 .yiv1007820498attach label {display:block;margin-bottom:5px;}#yiv1007820498 .yiv1007820498attach label a {text-decoration:none;}#yiv1007820498 blockquote {margin:0 0 0 4px;}#yiv1007820498 .yiv1007820498bold {font-family:Arial;font-size:13px;font-weight:700;}#yiv1007820498 .yiv1007820498bold a {text-decoration:none;}#yiv1007820498 dd.yiv1007820498last p a {font-family:Verdana;font-weight:700;}#yiv1007820498 dd.yiv1007820498last p span {margin-right:10px;font-family:Verdana;font-weight:700;}#yiv1007820498 dd.yiv1007820498last p span.yiv1007820498yshortcuts {margin-right:0;}#yiv1007820498 div.yiv1007820498attach-table div div a {text-decoration:none;}#yiv1007820498 div.yiv1007820498attach-table {width:400px;}#yiv1007820498 div.yiv1007820498file-title a, #yiv1007820498 div.yiv1007820498file-title a:active, #yiv1007820498 div.yiv1007820498file-title a:hover, #yiv1007820498 div.yiv1007820498file-title a:visited {text-decoration:none;}#yiv1007820498 div.yiv1007820498photo-title a, #yiv1007820498 div.yiv1007820498photo-title a:active, #yiv1007820498 div.yiv1007820498photo-title a:hover, #yiv1007820498 div.yiv1007820498photo-title a:visited {text-decoration:none;}#yiv1007820498 div#yiv1007820498ygrp-mlmsg #yiv1007820498ygrp-msg p a span.yiv1007820498yshortcuts {font-family:Verdana;font-size:10px;font-weight:normal;}#yiv1007820498 .yiv1007820498green {color:#628c2a;}#yiv1007820498 .yiv1007820498MsoNormal {margin:0 0 0 0;}#yiv1007820498 o {font-size:0;}#yiv1007820498 #yiv1007820498photos div {float:left;width:72px;}#yiv1007820498 #yiv1007820498photos div div {border:1px solid #666666;height:62px;overflow:hidden;width:62px;}#yiv1007820498 #yiv1007820498photos div label {color:#666666;font-size:10px;overflow:hidden;text-align:center;white-space:nowrap;width:64px;}#yiv1007820498 #yiv1007820498reco-category {font-size:77%;}#yiv1007820498 #yiv1007820498reco-desc {font-size:77%;}#yiv1007820498 .yiv1007820498replbq {margin:4px;}#yiv1007820498 #yiv1007820498ygrp-actbar div a:first-child {margin-right:2px;padding-right:5px;}#yiv1007820498 #yiv1007820498ygrp-mlmsg {font-size:13px;font-family:Arial, helvetica, clean, sans-serif;}#yiv1007820498 #yiv1007820498ygrp-mlmsg table {font-size:inherit;font:100%;}#yiv1007820498 #yiv1007820498ygrp-mlmsg select, #yiv1007820498 input, #yiv1007820498 textarea {font:99% Arial, Helvetica, clean, sans-serif;}#yiv1007820498 #yiv1007820498ygrp-mlmsg pre, #yiv1007820498 code {font:115% monospace;}#yiv1007820498 #yiv1007820498ygrp-mlmsg * {line-height:1.22em;}#yiv1007820498 #yiv1007820498ygrp-mlmsg #yiv1007820498logo {padding-bottom:10px;}#yiv1007820498 #yiv1007820498ygrp-msg p a {font-family:Verdana;}#yiv1007820498 #yiv1007820498ygrp-msg p#yiv1007820498attach-count span {color:#1E66AE;font-weight:700;}#yiv1007820498 #yiv1007820498ygrp-reco #yiv1007820498reco-head {color:#ff7900;font-weight:700;}#yiv1007820498 #yiv1007820498ygrp-reco {margin-bottom:20px;padding:0px;}#yiv1007820498 #yiv1007820498ygrp-sponsor #yiv1007820498ov li a {font-size:130%;text-decoration:none;}#yiv1007820498 #yiv1007820498ygrp-sponsor #yiv1007820498ov li {font-size:77%;list-style-type:square;padding:6px 0;}#yiv1007820498 #yiv1007820498ygrp-sponsor #yiv1007820498ov ul {margin:0;padding:0 0 0 8px;}#yiv1007820498 #yiv1007820498ygrp-text {font-family:Georgia;}#yiv1007820498 #yiv1007820498ygrp-text p {margin:0 0 1em 0;}#yiv1007820498 #yiv1007820498ygrp-text tt {font-size:120%;}#yiv1007820498 #yiv1007820498ygrp-vital ul li:last-child {border-right:none !important;}#yiv1007820498