caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* RE: [Caml-list] Please help a newbie
@ 2001-08-02 13:37 Dave Berry
  2001-08-02 16:08 ` Brian Rogoff
  0 siblings, 1 reply; 9+ messages in thread
From: Dave Berry @ 2001-08-02 13:37 UTC (permalink / raw)
  To: ocaml mailing list

For the language designers on the list:  This is an example of why
curried notation is a bad idea.  If the function were written using
tuples, Johann would have been less likely to make this mistake, and if
he had, the compiler could have given a message that was easier to
understand.


-----Original Message-----
> # let rec wys_die_lys l = function
>     [] -> []
>         | h :: t -> wys_dit h ::    wys_die_lys l t;;
> val wys_die_lys : 'a -> string list -> unit list = <fun>
-------------------
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


^ permalink raw reply	[flat|nested] 9+ messages in thread
* [Caml-list] Please help a newbie
@ 2001-08-02 13:42 Johann Spies
  2001-08-02 12:54 ` Frank Atanassow
                   ` (2 more replies)
  0 siblings, 3 replies; 9+ messages in thread
From: Johann Spies @ 2001-08-02 13:42 UTC (permalink / raw)
  To: ocaml mailing list

I have read a lot of tutorials and examples and am now trying to write
something myself.  My problem will show that I do not understand a
basic thing in functional programming.  I will appreciate some help on
this.

The following illustrates my problem:

------------------
$ ocaml
        Objective Caml version 3.01

# let lys = ['a';'b';'c'];;
val lys : char list = ['a'; 'b'; 'c']
# let wys_dit woord = print_string woord;;
val wys_dit : string -> unit = <fun>
# let rec wys_die_lys l = function
    [] -> []
        | h :: t -> wys_dit h ::    wys_die_lys l t;;
val wys_die_lys : 'a -> string list -> unit list = <fun>
# wys_die_lys lys;;
- : string list -> unit list = <fun>
# wys_die_lys ['x','y','z'];;
- : string list -> unit list = <fun>
------------------------------------------------

Now my question:  I expected 

wys_die_lys lys to print out a b c
and 
wys_die_lys ['x','y','z'] to print x y z

Why did this not happen?


Johann
-- 
Johann Spies          Telefoon: 021-808 4036
Informasietegnologie, Universiteit van Stellenbosch

     "All scripture is given by inspiration of God, and is 
      profitable for doctrine, for reproof, for correction, 
      for instruction in righteousness;"          
                                     II Timothy 3:16 
-------------------
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


^ permalink raw reply	[flat|nested] 9+ messages in thread
* (no subject)
@ 2001-08-03 10:58 Johann Spies
  2001-08-03 18:23 ` [Caml-list] Please help a newbie md5i
  0 siblings, 1 reply; 9+ messages in thread
From: Johann Spies @ 2001-08-03 10:58 UTC (permalink / raw)
  To: ocaml mailing list
  Cc: Sylvain Pogodalla, Francois.Thomasset, Brian Rogoff, Dave Berry, luther

[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #1: Type: multipart/mixed; boundary="Frank Atanassow <franka@cs.uu.nl>, Francois.Pottier@inria.fr,", Size: 5553 bytes --]

Frank Atanassow <franka@cs.uu.nl>, Francois.Pottier@inria.fr,
    Basile.Starynkevitch@cea.fr, Benedikt.Rosenau@dlr.de,
    Wolfgang Lux <lux@wi.uni-muenster.de>,
    Stefano Lanzavecchia <stf@apl.it>,
    Sebastien Briais <sbriais@ens-lyon.fr
Subject: Re: [Caml-list] Please help a newbie
References: <87lml2a896.fsf@#maties.sun.ac.za> <3B6949D5.784BAC9F@xrce.xerox.com>
From: Johann Spies <jspies@maties.sun.ac.za>
Date: 03 Aug 2001 10:58:03 +0000
In-Reply-To: Sylvain Pogodalla's message of "Thu, 02 Aug 2001 14:38:45 +0200"
Message-ID: <87g0b98l6s.fsf@#maties.sun.ac.za>
Lines: 132
User-Agent: Gnus/5.0803 (Gnus v5.8.3) Emacs/20.7
MIME-Version: 1.0
Content-Type: text/plain; charset=us-ascii

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.

My knowledge of the theory of programming is not good.  I started
programming as a hobby using Basic, Pascal, Dbase III, a little bit of
Prolog and for the past few years, Python.

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.  I
realised my mistake that lys was a char list and I expected wys_dit to
print strings.

> + 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.

> + do you think wys_dit is able to print one element of lys?

Answered above.

> + 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.

> + do you see the interpreter answers that it "expects" two
>   parameters?

No, I did not see that.  I do not always understand the messages of
the interpreter.

> + can you compare the function f1 and f2 such that:
> let f1 x = match x with
> | [] -> print_string "empty list";print_newline()
> | h::t -> print_string "not empty list";print_newline();;
> let f2 = function 
> | [] -> print_string "empty list";print_newline()
> | h::t -> print_string "not empty list";print_newline();;
> + how many parameters do they need?

Maybe this is what I am struggling to understand.  I can not see how
f2 can work.  What data does it use?

Related to this is what Benedikt referred to:

> 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?

Francois Thomasset's remark on the same topic also underlines my lack
of understanding of the handling of arguments a function:

> 2/ your wys_die_lys has 2 arguments, as can be seen from the
> signature val wys_die_lys : 'a -> string list -> unit list = <fun>
> (the system deduced the string list type for the type of the second
> argument from the call to print_string in wys_dit).



> + do you expect wys_dit to return a result?
I did.
> + what type for that result?
A printed string.
> + 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

>From that I suspect that :: is an alternative for [] - telling the
compiler that the tipe is a list?

I tried to figure out what it does by reading examples of it's use.


> + do you really want to build a unit list, as the type of wys_die_lys
> describes ?

No.  I am do not see the of what use a "unit list" can be. 

Some of you also helped me to see the necessity of flushing the
output:

> You need to flush stdout or print_newline; also the function
> wys_die_lys does not need to return a unit list but just a unit;

Someone wrote:

> 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?

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.

I have not responded to everything. I appreciate all the help, remarks
and code sent to me and I will probably ask some more questions in the
future.

Johann
-- 
Johann Spies          Telefoon: 021-808 4036
Informasietegnologie, Universiteit van Stellenbosch

     "Love is patient, love is kind. It does not envy, it
      does not boast, it is not proud. It is not rude, it is
      not self seeking, it is not easily angered, it keeps
      no record of wrongs. Love does not delight in evil but
      rejoices with the truth. It always protects, always
      trusts, always hopes, always perseveres." 
                                I Corinthians 13:4-7 
-------------------
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


^ permalink raw reply	[flat|nested] 9+ messages in thread

end of thread, other threads:[~2001-08-05 21:38 UTC | newest]

Thread overview: 9+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2001-08-02 13:37 [Caml-list] Please help a newbie Dave Berry
2001-08-02 16:08 ` Brian Rogoff
2001-08-02 13:42 Johann Spies
2001-08-02 12:54 ` Frank Atanassow
2001-08-02 13:01 ` Francois Thomasset
2001-08-02 13:25   ` Francois Thomasset
2001-08-02 13:05 ` Sven
2001-08-03 10:58 Johann Spies
2001-08-03 18:23 ` [Caml-list] Please help a newbie md5i
2001-08-05 21:37   ` John Max Skaller

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).