From: Vitaly Lugovsky <vsl@ontil.ihep.su>
To: Johann Spies <jspies@maties.sun.ac.za>
Cc: <caml-list@inria.fr>
Subject: [Caml-list] Re: your mail
Date: Fri, 3 Aug 2001 21:42:04 +0400 (MSD) [thread overview]
Message-ID: <Pine.LNX.4.30.0108032057520.22519-100000@ontil.ihep.su> (raw)
In-Reply-To: <E15SceZ-0002Rv-00@bywoner>
On Fri, 3 Aug 2001, Johann Spies wrote:
> Thank you to everyone that replied. I have realized now that it is not
> that easy to start programming in ocaml (or a functional language)
> when you are an amateur and coming from a procedural/imperative
> programming background.
Why? Your falut is very common! I was a Lisp and Scheme programmer
with a years of experience when I started to use ML, and I had same
problems. Static typing is not so common... ;)
> Maybe Sylvain summarized my problems with his questions:
>
> > There are many different things in your code. Answering to some
> > questions might help you:
> > + what is the type of lys?
> > + what is the type of the elements of lys?
>
> I am not sure about the difference between these two questions.
The second one specifies type as a list or any other *set of elements*.
The first question is much more generic.
> I realised my mistake that lys was a char list and I expected wys_dit to
> print strings.
Another mistake was that your function produced a useless unit list.
> > + what is the type of wys_dit?
>
> As I understand it wys_dit is a function. I always thought of
> functions as using types not necessarily having types.
It's a main point of functional programming. Function is a first-class
object, as generic as integers, etc. E.g. function from integer to integer
is an element of a set, specified by the type (int -> int). And, sure,
there could be a function with a type ((int -> int) -> int) which takes
the first function as a parameter.
> > + how many parameters do you want for wys_die_lys?
>
> I thought it needed one, originally, but it did not work, so I tried a
> second. At least the interpreter did not complain. Another response
> to my question, explained why I did not see an interpreter complaint.
Compiler guessed a type of your function from a case of it's usage.
> No, I did not see that. I do not always understand the messages of
> the interpreter.
It's just a type of your expression. Always.
> > But the easiest way is:
> >
> > let wys_die_lys = List.iter print_string;;
>
> In this function, there is no explicit parameter. Where is this
> behaviour documented?
Just check it's type, and you'll understand all.
# let wys_die_lys = List.iter print_string;;
val wys_die_lys : string list -> unit = <fun>
List.iter have a type ('a -> unit) -> 'a list -> unit.
print_string takes one string argument, and, as it is a first argument
of List.iter, it binds 'a to string.
So, compiler knows exactly, that the right part of expression is a
function from string list to unit, and let ... = expression just binds it
to the name wys_die_lys.
> > + do you expect wys_dit to return a result?
> I did.
And your result was a unit list - list of NULLs ;)
> > + what type for that result?
> A printed string.
It's not a result. It's a side effect.
> > + what is the exact meaning of the :: operator?
>
> I am not sure. I could not find an explanation for it. I have just
> now searched the "operator" section of the ocaml-manual for :: again
> and could only find :
>
> type 'a list = [] | :: of 'a * 'a list
As you can see from a definition, :: is a list constructor. "cons", if
you know lisp.
> > I would recommend not to use print_string as long as
> > you're not comfortable with the language; writing purely
> > functional code (no side effects) will be easier.
>
> I have always used print statements in programs to help me with
> debugging and to see what is going on (that is besides the necessity
> to get some output from the program). A program with no side effects
> does not make sense to me. What is the use of it?
What use? Result of the function. Any program is just a function from
input to output, without side-effects.
> As you can see, these questions revealed my lack of understanding of
> some of the basics. I suspect the only way to get to understand it is
> by reading, asking and trying.
Please, try to read and understand this lectures:
http://www.cl.cam.ac.uk/Teaching/Lectures/funprog-jrh-1996/index.html
-------------------
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
next prev parent reply other threads:[~2001-08-03 17:53 UTC|newest]
Thread overview: 12+ messages / expand[flat|nested] mbox.gz Atom feed top
2001-08-03 10:58 Johann Spies
2001-08-03 13:26 ` [Caml-list] Re: your mail Francois Pottier
2001-08-03 17:42 ` Vitaly Lugovsky [this message]
2001-08-03 18:23 ` [Caml-list] Please help a newbie md5i
2001-08-05 21:37 ` John Max Skaller
2001-08-06 10:02 ` empty type (was [Caml-list] Please help a newbie) Hendrik Tews
2001-08-07 17:32 ` John Max Skaller
-- strict thread matches above, loose matches on Subject: below --
2003-02-07 5:23 Nikolaj Bjorner
2003-02-07 13:10 ` [Caml-list] Re: your mail Mike Potanin
2002-01-21 23:27 cgillot
2002-01-21 22:43 ` [Caml-list] Re: your mail Markus Mottl
2002-01-22 9:00 ` Daniel de Rauglaudre
2002-01-22 13:12 ` Markus Mottl
2001-03-06 20:25 rakesh malhotra
2001-03-07 16:53 ` [Caml-list] Re: your mail Brian Rogoff
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=Pine.LNX.4.30.0108032057520.22519-100000@ontil.ihep.su \
--to=vsl@ontil.ihep.su \
--cc=caml-list@inria.fr \
--cc=jspies@maties.sun.ac.za \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox