Re: [very beginner] function

"Chantal KELLER [email protected] [ocaml_beginners]" <[email protected]>
Newsgroups gmane.comp.lang.ocaml.beginners
Message-ID <[email protected]>
Hi moonkid,

In "function x -> x*x", "x" is the name of the argument of the function.
It has a meaning inside the body of the function ("x*x"), but not
outside: the keyword "function" binds it.

For instance, "function y -> y*y" is exactly the same object: an
anonymous function taking an integer as un argument and computing its
square.

"(function x -> x * x) 5" defines this anonymous function and applies it
directly to "5", thus returning "25".

If you want to give a name to this function, you need to use the
let-binder, for instance:

# let f = function x -> x*x;;
# f 5;;

which is equivalent to the simpler:

# let f x = x*x;;
# f 5;;

This gives a name at top-level, which is re-usable anywhere else after
this point. You can also use a local name:

# let f = function x -> x*x in f 5;;

which is also equivalent to

# let f x = x*x in f 5;;

Hope this helps,
Chantal.



Le 03/11/2014 17:15, [email protected] [ocaml_beginners] a écrit :
> I understand this
> 
> # function x -> x*x ;;
> 
> But don't understand this
> 
> # (function x -> x * x) 5 ;;
> 
> It is a call of the function? Why do I have to put the complete
> function-definition again there?
> Isn't there a simpler way? Something like
> 
> # x (5) ;;
> - : int = 25
> 


------------------------------------

------------------------------------

Archives up to December 31, 2011 are also downloadable at http://www.connettivo.net/cntprojects/ocaml_beginners
The archives of the very official ocaml list (the seniors' one) can be found at http://caml.inria.fr
Attachments are banned and you're asked to be polite, avoid flames etc.
------------------------------------

Yahoo Groups Links

<*> To visit your group on the web, go to:
    http://groups.yahoo.com/group/ocaml_beginners/

<*> Your email settings:
    Individual Email | Traditional

<*> To change settings online go to:
    http://groups.yahoo.com/group/ocaml_beginners/join
    (Yahoo! ID required)

<*> To change settings via email:
    [email protected] 
    [email protected]

<*> To unsubscribe from this group, send an email to:
    [email protected]

<*> Your use of Yahoo Groups is subject to:
    https://info.yahoo.com/legal/us/yahoo/utos/terms/
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.