List Info

Thread: "ocaml_beginners"::[] A simple question about a function




"ocaml_beginners"::[] A simple question about a function
country flaguser name
United States
2007-08-02 18:17:50


HI everyone.

I'm new in Ocaml language, and I had tried to understand the following
function, but Im not able.

let op = function
p->fst p (snd p);;

If I put it at the top level it shows me :
val op : ('a->'b)*'a ->'b = <fun>;

I think that p must be like ( , ) but if i trie to write op((+),3);;
It shows me -: int -> int = <fun>;

Can somebody tell me how this function works and what does it do ?
Thank you in advance
--
View this message in context: http://www.nabble.com/A-simple-question-about-a-function-tf4207163.html#a11967990
Sent from the Ocaml Beginner mailing list archive at Nabble.com.

__._,_.___
.

__,_._,___
Re: "ocaml_beginners"::[] A simple question about a function
country flaguser name
United States
2007-08-02 18:35:13

Msam85 wrote:
&gt; HI everyone.
>
> I'm new in Ocaml language, and I had tried to understand the following
> function, but Im not able.
&gt;
> let op = function
> p->fst p (snd p);;
>;
> If I put it at the top level it shows me :
> val op : ('a->'b)*'a ->'b = <fun>;
>
> I think that p must be like ( , ) but if i trie to write op((+),3);;
> It shows me -: int -> int = <fun>;
>
> Can somebody tell me how this function works and what does it do ?

I know it's summer and all, but is this for a class? The function is
pretty contrived. Where did you find it?

I'll say that you're pretty close to figuring it out. Try this:

# let new_function = op ((+), 3);;
val new_function : int -> int = <fun>;
# new_function 5;;
- : int = 8

__._,_.___
.

__,_._,___
"ocaml_beginners"::[] Re: A simple question about a function
country flaguser name
United States
2007-08-02 18:55:42

Look at http://en.wikipedia.org/wiki/Currying
and remember that (+) is a function of two arguments.

--- In ocaml_beginners%40yahoogroups.com">ocaml_beginnersyahoogroups.com, Msam85 <carloselcheca...> wrote:
&gt;
>
> HI everyone.
>
> I'm new in Ocaml language, and I had tried to understand the following
> function, but Im not able.
&gt;
> let op = function
> p->fst p (snd p);;
>;
> If I put it at the top level it shows me :
> val op : ('a->'b)*'a ->'b = <fun>;
>
> I think that p must be like ( , ) but if i trie to write op((+),3);;
> It shows me -: int -> int = <fun>;
>
> Can somebody tell me how this function works and what does it do ?
> Thank you in advance
> --
> View this message in context:
http://www.nabble.com/A-simple-question-about-a-function-tf4207163.html#a11967990
> Sent from the Ocaml Beginner mailing list archive at Nabble.com.
>

__._,_.___
.

__,_._,___
Re: "ocaml_beginners"::[] A simple question about a function
country flaguser name
United Kingdom
2007-08-02 18:44:19

On Friday 03 August 2007 00:17:50 Msam85 wrote:
&gt; HI everyone.
>
&gt; I'm new in Ocaml language, and I had tried to understand the following
> function, but Im not able.
&gt;
> let op = function
> p->fst p (snd p);;

This is equivalent to:

let op p = fst p (snd p)

Pattern matching makes deconstruction faster and more readable, so it is even
easier to write:

let op (f, x) = f x

So the "op&quot; function accepts a pair and applies the second element of the pair
to the first element of the pair.

> If I put it at the top level it shows me :
> val op : ('a->'b)*'a ->'b = <fun>;
>
> I think that p must be like ( , ) but if i trie to write op((+),3);;
> It shows me -: int -> int = <fun>;

Yes. That takes the curried "add&quot; function and partially applies "3&quot; to give a
function that adds three:

# let op (f, x) = f x;;
val op : ('a -> 'b) * 'a -> 'b = <fun>;
# let add_three = op((+), 3);;
val add_three : int -> int = <fun>;
# add_three 5;;
- : int = 8

--
Dr Jon D Harrop, Flying Frog Consultancy Ltd.
OCaml for Scientists
http://www.ffconsultancy.com/products/ocaml_for_scientists/?e

__._,_.___
.

__,_._,___
"ocaml_beginners"::[] Re: A simple question about a function
country flaguser name
United States
2007-08-03 04:26:32


THANK YOU ALL FOR THE EXPLANATION, I WAS VERY CLOSE , BUT ITS DIFICULT FOR ME
TO SEE THIS KIND OF FUNCTIONS, LIKE JON HARROP WROTE PATTERN MATCHING MAKES
THAT FUNCTION MORE UNDERSTANDABLE.

[QUOTE]
KARL ZILLES WROTE: I KNOW IT'S SUMMER AND ALL, BUT IS THIS FOR A CLASS? THE
FUNCTION IS
PRETTY CONTRIVED. WHERE DID YOU FIND IT?
[/QUOTE]

WHAT DID YOU MEAN WITH "PRETTY CONTRIVED&QUOT;? SORRY FOR MY BAD ENGLISH IM FROM
SPAIN.
THAT FUNCTION WAS IN ONE EXAM IN THE UNIVERSITY OF LA CORUņA IN SPAIN.

THERE IS NO NOTES OR MANUAL ABOUT OCAML IN SPANISH ON INTERNET , SO COULD
YOU GIVE ME SOME URLS TO LEARN OCAML FOR NEWBIES LIKE ME ? IM TRYING TO
LEARN BUT I COME FROM JAVA AND C AND ITS SOOO DIFERENT....

THANK YOU AGAIN
--
VIEW THIS MESSAGE IN CONTEXT: HTTP://WWW.NABBLE.COM/A-SIMPLE-QUESTION-ABOUT-A-FUNCTION-TF4207163.HTML#A11980069
SENT FROM THE OCAML BEGINNER MAILING LIST ARCHIVE AT NABBLE.COM.

__._,_.___
RECENT ACTIVITY
VISIT YOUR GROUP
SPONSORED LINKS
YAHOO! FINANCE

IT'S NOW PERSONAL

GUIDES, NEWS,

ADVICE & MORE.

GREEN GROUPS

ON YAHOO! GROUPS

SHARE YOUR PASSION

FOR THE PLANET.

FIND ENLIGHTENMENT

YOGA GROUPS AND

RESOURCES ON

YAHOO! GROUPS.

"ocaml_beginners"::[] Re: A simple question about a function
country flaguser name
United States
2007-08-03 04:28:23


THANK YOU ALL FOR THE EXPLANATION, I WAS VERY CLOSE , BUT ITS DIFICULT FOR ME
TO SEE THIS KIND OF FUNCTIONS, LIKE JON HARROP WROTE PATTERN MATCHING MAKES
THAT FUNCTION MORE UNDERSTANDABLE.

[]
I KNOW IT'S SUMMER AND ALL, BUT IS THIS FOR A CLASS? THE FUNCTION IS
PRETTY CONTRIVED. WHERE DID YOU FIND IT?
[/QUOTE]

WHAT DID YOU MEAN WITH "PRETTY CONTRIVED&QUOT;? SORRY FOR MY BAD ENGLISH IM FROM
SPAIN.
THAT FUNCTION WAS IN ONE EXAM IN THE UNIVERSITY OF LA CORUņA IN SPAIN.

THERE IS NO NOTES OR MANUAL ABOUT OCAML IN SPANISH ON INTERNET , SO COULD
YOU GIVE ME SOME URLS TO LEARN OCAML FOR NEWBIES LIKE ME ? IM TRYING TO
LEARN BUT I COME FROM JAVA AND C AND ITS SOOO DIFERENT....

THANK YOU AGAIN
--
VIEW THIS MESSAGE IN CONTEXT: HTTP://WWW.NABBLE.COM/A-SIMPLE-QUESTION-ABOUT-A-FUNCTION-TF4207163.HTML#A11980069
SENT FROM THE OCAML BEGINNER MAILING LIST ARCHIVE AT NABBLE.COM.

__._,_.___
.

__,_._,___
"ocaml_beginners"::[] Re: A simple question about a function
country flaguser name
United States
2007-08-03 04:29:36


THANK YOU ALL FOR THE EXPLANATION, I WAS VERY CLOSE , BUT ITS DIFICULT FOR ME
TO SEE THIS KIND OF FUNCTIONS, LIKE JON HARROP WROTE PATTERN MATCHING MAKES
THAT FUNCTION MORE UNDERSTANDABLE.

KARL ZILLES WROTE:
&GT;
> I KNOW IT'S SUMMER AND ALL, BUT IS THIS FOR A CLASS? THE FUNCTION IS
> PRETTY CONTRIVED. WHERE DID YOU FIND IT?
>

WHAT DID YOU MEAN WITH "PRETTY CONTRIVED&QUOT;? SORRY FOR MY BAD ENGLISH IM FROM
SPAIN.
THAT FUNCTION WAS IN ONE EXAM IN THE UNIVERSITY OF LA CORUņA IN SPAIN.

THERE IS NO NOTES OR MANUAL ABOUT OCAML IN SPANISH ON INTERNET , SO COULD
YOU GIVE ME SOME URLS TO LEARN OCAML FOR NEWBIES LIKE ME ? IM TRYING TO
LEARN BUT I COME FROM JAVA AND C AND ITS SOOO DIFERENT....

THANK YOU AGAIN
--
VIEW THIS MESSAGE IN CONTEXT: HTTP://WWW.NABBLE.COM/A-SIMPLE-QUESTION-ABOUT-A-FUNCTION-TF4207163.HTML#A11980069
SENT FROM THE OCAML BEGINNER MAILING LIST ARCHIVE AT NABBLE.COM.

__._,_.___
.

__,_._,___
Re: "ocaml_beginners"::[] A simple question about a function
country flaguser name
Germany
2007-08-03 05:35:54

Zitat von Msam85 < carloselcheca%40gmail.com">carloselchecagmail.com>:

>
>; HI everyone.
>
&gt; I'm new in Ocaml language, and I had tried to understand the following
> function, but Im not able.
&gt;
> let op = function
> p->fst p (snd p);;

================================================
oliversiouxsie2:~$ ocaml
Objective Caml version 3.09.2

# let op = function
p->fst p (snd p);;
val op : ('a -> 'b) * 'a -> 'b = <fun>;
# op (fst, (2,3));;
- : int = 2
# op (snd, (2,3));;
- : int = 3
# fst (2,3);;
- : int = 2
# snd (2,3);;
- : int = 3
# op (sin, 2.89);;
- : float = 0.248946786673152565
# let l = [(sin, 2.0); (sin, 3.0); (cos, 2.0) ];;
val l : ((float -> float) * float) list =
[(<fun&gt;, 2.); (<fun&gt;, 3.); (<fun&gt;, 2.)]
# List.map op l;;
- : float list =
[0.909297426825681709; 0.141120008059867214; -0.416146836547142407]
#
================================================

>
>; If I put it at the top level it shows me :
> val op : ('a->'b)*'a ->'b = <fun>;
>
> I think that p must be like ( , ) but if i trie to write op((+),3);;
> It shows me -: int -> int = <fun>;

Maybe it would make sense that you explain what you want to do,
so we can better find, what you did wrong.
Did you wrote this function and don't know how it works,
or is this an example you got from somewhere else and you want to
know what it does?

>
>; Can somebody tell me how this function works and what does it do ?

It takes a pair as argument, and applies the first
member of the argument to the second member of the argument.

See the examples above.

Ciao,
Oliver

__._,_.___
Recent Activity
Visit Your Group
SPONSORED LINKS
Yahoo! Finance

It's Now Personal

Guides, news,

advice & more.

Yahoo! Groups

Real Food Group

Share recipes

and favorite meals.

Yahoo! Groups

Join a yoga group

and take the stress

out of your life.

[1-8]

about | contact  Other archives ( Real Estate discussion Medical topics )