Re: Optional arguments in functions???
"Douglas Lewit [email protected] [ocaml_beginners]" <[email protected]> Wed, 6 Apr 2016 16:42:55 -0500
| Newsgroups | gmane.comp.lang.ocaml.beginners |
|---|---|
| Message-ID | <CAM0XMJSRJninN83wC1YJW8ps-Yr_Z44E+frYcyhUWEtrnKgd1A@mail.gmail.com> |
NOBODY gets my Python magazines!!! ;-) Lately I've actually been spending more time with Java than Python, and I've been doing some Ocaml programming. Oh my god, now there is a really difficult language. But I kind of like it because if you get it right you can do a lot with just a little code. It also emphasizes a ton of recursion with lists. For example, if I want to generate all the even integers from 0 to 100 and put them in a list, I could do: *let rec sequence lower upper step = if lower > upper then [ ] else* *lower :: ( sequence ( lower + step ) upper step ) ;;* *let kylesList = sequence 0 100 2 ;;* You can do the same thing in Python a lot easier with *kylesList = [ 2*n for n in range(0, 51) ]* I like Python a lot, but it's fun and challenging to explore different languages and find out their strengths and weaknesses. Ocaml is just weird, but there's a really major financial trading company called Jane Street that uses it for all their trading, software development, etc. It's supposed to run just as fast as C or C++. ( The main complaints about Java and Python is that for some apps they are supposed to be too slow. Modern society is all about speed and "efficiency" even if it gives you a fucking heart attack before you reach the age of 50. ) Would be nice to meet up, but let's wait until the second week of May when my life gets a little quieter. No point in complaining about Albright. Kimmel won't listen. Albright is probably gonna retire or die soon anyhow! He's just a minor obstacle in getting that degree. I like his course.... I just don't like him. He's really mean and sarcastic, and likes to poke fun at students, insult them, etc. Just not a nice person. I don't care how smart he is.... if you can't treat students with respect then why teach? On Wed, Apr 6, 2016 at 4:17 PM, Gabriel Scherer [email protected] [ocaml_beginners] <[email protected]> wrote: > > > The syntax is ?(z=1), not ?z(z=1). > Another problem is that an optional argument cannot be the last argument > of the function (because the typer uses the application of the *following* > argument to know that you indeed omit the optional argument instead of only > doing partial application), so you should actually write something like > let multiply ?(z=1) x y = x*y*z;; > > You won't be able to write > multiply 1 2 3 > though, you need to explicitly pass the argument, > multiply 1 2 ~z:3 > > See the OCaml manual, Section 4.1.1, Optional arguments: > http://caml.inria.fr/pub/docs/manual-ocaml/lablexamples.html#sec43 > > On Wed, Apr 6, 2016 at 5:00 PM, Douglas Lewit [email protected] > [ocaml_beginners] <[email protected]> wrote: > >> >> >> Hi everyone, >> >> I'm experiencing some issues with optional arguments passed to functions >> in Ocaml. For example, let's say I want to write a function like this: >> >> let multiply x y ?z(z = 1) = x * y * z ;; >> >> This doesn't work in utop. What I'm trying to do is this. If two >> arguments are passed to the function, then just return the product of those >> two arguments. ( Because x * y * 1 is always the same as x * y. ) But if >> three arguments are passed, then return the product of all three integers. >> What am I doing wrong? There must be a way to do this. Thanks for sharing! >> >> Best, >> >> Douglas. >> >> >> >> > >