Benno wrote:
> For what might be intersting comparison and just looking at different
> languages a bit more, here is the code transcribed to python. I'd love
> to see the equiv in lisp
Yes please. I'd love to see this in lisp.
> or ocaml
Included below.
> if someone wants to put it up. Up front
> note: I've just transcribed this and left out error handling.
Same with me.
(* A program to round a number to nearest multiple of 5. *)
let amount = float_of_string Sys.argv.(1) in
let remainder = mod_float amount 5.0 in
let base_amount = amount -. remainder in
let x =
if remainder > 2.0 then
5.0
else
0.0
in
print_endline (string_of_float (base_amount +. x))
To run it, cut and paste the above to a file named say "lesson1.ml"
and then run (on Debian/Ubuntu, apt-get install ocaml):
ocaml lesson1.ml <number>
Following Benno's example, things of note:
1/ Ocaml, like Python and C puts the first argument Sys.argv.(1).
2/ Ocaml uses conversion functions like float_of_string and
string_of_float to convert between types. Conversions must be
explicit.
3/ Ocaml is a lot more picky about whats an integer and whats
a float and uses +. and -. to add and subtract floats while
using plain + and - for ints.
If you want to add an int to a float, you must convert one of
them to the same type as the other (using int_of_float or
float_of_int) and then use the right operator.
4/ Ocaml lets you set a value based on the result of an if statment.
5/ Ocaml uses parentheses in a very different way to Ruby of Ocaml.
6/ We're running the above like a script. Ocaml also compiles to
bytecode (like Perl/Python/Ruby etc) and unlike those, also
compiles to native binaries.
Erik
--
+-----------------------------------------------------------+
Erik de Castro Lopo
+-----------------------------------------------------------+
"O'Caml ... a "language designed for smart people" if there
ever was one." -- Mike Vanier
_______________________________________________
coders mailing list
[email protected]
http://lists.slug.org.au/listinfo/coders