|
List Info
Thread: "ocaml_beginners"::[] ocaml for dummies
|
|
| "ocaml_beginners"::[] ocaml
for dummies |
  United States |
2008-06-09 22:01:14 |
|
I'm totally lost. I was given an assignment to write a function
greetings that will print a greeting to the name given the function;
the return value should be unit, and the greeting should be followed
by a single newline. Given:
# print_string "hello"; print_newline ();;
hello
- : unit = ()
#let greetings s = ... ;;
val greetings : string -> unit = <fun>
#greetings "Mark";;
Greetings, Mark!
- : unit = ()
The closest I've came was:
# let greetings s = print_string s;;
val greetings : string -> unit = <fun>
# greetings "Mark";;
Mark- : unit = ()
Why am I so lost with OCaml? I learned Perl, C++, Java and they
seemed simple in the beginning but OCaml....
__._,_.___
.
__,_._,___
|
| Re: "ocaml_beginners"::[]
ocaml for dummies |

|
2008-06-09 22:03:59 |
|
I'm not sure why you're lost, but it looks like all you're missing is prepending "Greetings, " and
appending "!" to your string.
Where are you getting lost?
~~ Robert.
William Lassiter wrote:
> I'm totally lost. I was given an assignment to write a function
> greetings that will print a greeting to the name given the function;
> the return value should be unit, and the greeting should be followed
> by a single newline. Given:
>
> # print_string "hello"; print_newline ();;
> hello
> - : unit = ()
> #let greetings s = ... ;;
> val greetings : string -> unit = <fun>
> #greetings "Mark";;
> Greetings, Mark!
> - : unit = ()
>
> The closest I've came was:
>
> # let greetings s = print_string s;;
> val greetings : string -> unit = <fun>
> # greetings "Mark";;
> Mark- : unit = ()
>
> Why am I so lost with OCaml? I learned Perl, C++, Java and they
> seemed simple in the beginning but OCaml....
>
>
__._,_.___
.
__,_._,___
|
| Re: "ocaml_beginners"::[]
ocaml for dummies |
  United States |
2008-06-09 23:23:44 |
|
On Tue, Jun 10, 2008 at 1:01 PM, William Lassiter < wlassiter2%40yahoo.com">wlassiter2 yahoo.com> wrote:
> I'm totally lost. I was given an assignment to write a function
> greetings that will print a greeting to the name given the function;
> the return value should be unit, and the greeting should be followed
> by a single newline. Given:
>
> # print_string "hello"; print_newline ();;
> hello
> - : unit = ()
> #let greetings s = ... ;;
> val greetings : string -> unit = <fun>
> #greetings "Mark";;
> Greetings, Mark!
> - : unit = ()
>
> The closest I've came was:
>
> # let greetings s = print_string s;;
> val greetings : string -> unit = <fun>
> # greetings "Mark";;
> Mark- : unit = ()
I'm a beginner as well but I know that the ^ operator can be used for
string concatenation so try that in your 'greetings' function.
__._,_.___
.
__,_._,___
|
| Re: "ocaml_beginners"::[]
ocaml for dummies |
  United Kingdom |
2008-06-09 23:57:09 |
|
On Tuesday 10 June 2008 04:01:14 William Lassiter wrote:
> I'm totally lost. I was given an assignment to write a function
> greetings that will print a greeting to the name given the function;
> the return value should be unit, and the greeting should be followed
> by a single newline. Given:
>
> # print_string "hello"; print_newline ();;
> hello
> - : unit = ()
> #let greetings s = ... ;;
> val greetings : string -> unit = <fun>
> #greetings "Mark";;
> Greetings, Mark!
> - : unit = ()
>
> The closest I've came was:
>
> # let greetings s = print_string s;;
> val greetings : string -> unit = <fun>
> # greetings "Mark";;
> Mark- : unit = ()
They are probably looking for:
# let greetings name =
print_string "Greetings, ";
print_string name;
print_string "!";
print_newline();;
val greetings : string -> unit = <fun>
Which may be used as follows:
# greetings "Mark";;
Greetings, Mark!
- : unit = ()
# greetings "Jon";;
Greetings, Jon!
- : unit = ()
There are also many other approaches. One alternative is to use string
concatenation to build up a single string (but you weren't told about this ^
operator):
# let greetings name =
print_string ("Greetings, " ^ name ^ "!");
print_newline();;
val greetings : string -> unit = <fun>
Another alternative is to iterate the print_string function over a sequence of
strings (functional programming makes this easy):
# let greetings name =
List.iter print_string ["Greetings, "; name; "!"];
print_newline();;
val greetings : string -> unit = <fun>
Finally, the most likely alternative when using OCaml as a production language
is to use printf:
# open Printf;;
# let greetings = printf "Greetings, %s!n%!";;
val greetings : string -> unit = <fun>
This is preferred because it is both concise and efficient.
Note that I have been using print_newline explicitly for two reasons:
1. It is all you gave me to work with. In practice, you can replace
(print_string s; print_newline()) with (print_endline s).
2. The expression (print_string "n") prints a newline but the print_newline
(and print_endline) function also flushes the output, ensuring that it will
be seen on screen immediately.
The enigmatic %! at the end of my final printf implementation is a flush.
--
Dr Jon D Harrop, Flying Frog Consultancy Ltd.
http://www.ffconsultancy.com/products/?e
__._,_.___
.
__,_._,___
|
| Re: "ocaml_beginners"::[]
ocaml for dummies |
  Belgium |
2008-06-10 04:12:45 |
|
On Tue, 10 Jun 2008 03:01:14 -0000, William Lassiter wrote:
>
> Why am I so lost with OCaml? I learned Perl, C++, Java and they
> seemed simple in the beginning but OCaml....
How would you do it in C ?
C.
__._,_.___
.
__,_._,___
|
| Re: "ocaml_beginners"::[]
ocaml for dummies |
  United States |
2008-06-10 08:50:40 |
|
On Tue, 10 Jun 2008 03:01:14 -0000, William Lassiter wrote
> Why am I so lost with OCaml? I learned Perl, C++, Java and they
> seemed simple in the beginning but OCaml....
I suppose it should really be asked: What have you looked at to try to find
a solution? Have you read any of the OCaml reference manual? Particularly
chapter one and at least the chapter on the core library? Have you looked
at something like the OCaml Tutorial website <http://www.ocaml-
tutorial.org/>, or Jason Hickey's excellent introductory book
<http://www.cs.caltech.edu/courses/cs134/cs134b/book.pdf>? Any of the above
are probably preferable to just random trial and error.
--
William D. Neumann
__._,_.___
.
__,_._,___
|
| Re: "ocaml_beginners"::[]
ocaml for dummies |
  Switzerland |
2008-06-10 16:59:00 |
|
On Jun 10, 2008, at 3:50 PM, William D. Neumann wrote:
> On Tue, 10 Jun 2008 03:01:14 -0000, William Lassiter wrote
>
>> Why am I so lost with OCaml? I learned Perl, C++, Java and they
>> seemed simple in the beginning but OCaml....
>
> I suppose it should really be asked: What have you looked at to try
> to find
> a solution? Have you read any of the OCaml reference manual?
> Particularly
> chapter one and at least the chapter on the core library? Have you
> looked
> at something like the OCaml Tutorial website <http://www.ocaml-
> tutorial.org/>, or Jason Hickey's excellent introductory book
> <http://www.cs.caltech.edu/courses/cs134/cs134b/book.pdf>? Any of
> the above
> are probably preferable to just random trial and error.
>
> --
>
> William D. Neumann
Functional part of the language are probably a bit weird to understand
for people having strong habit of imperative programming. It took me a
while to change my mindset personnaly.
The build environnement is rough around the edges to say the least,
there is plain Makefiles, Makefiles+OcamlMakefile, evt. autoconf/
automake, ocamlfind, ocamlbuild and omake...
William (Lassiter), could you explain with more details what is
difficult for you, I'm a beginner too with an experience of ~500-700
lines of code I may help cause I still find this language very
interesting.
Regards.
--
Philippe Strauss
philou%40philou.ch">philou philou.ch
__._,_.___
.
__,_._,___
|
[1-7]
|
|