A simple question about a function

View: New views
6 Messages — Rating Filter:   Alert me  

A simple question about a function

by Msam85 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

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

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

by Karl Zilles :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Msam85 wrote:

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

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



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

by Jon Harrop :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

On Friday 03 August 2007 00:17:50 Msam85 wrote:
> 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);;

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" 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" function and partially applies "3" 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

by gaberosenhouse :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

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

--- In ocaml_beginners@..., Msam85 <carloselcheca@...> wrote:

>
>
> 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: A simple question about a function

by Msam85 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

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:
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"? 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 :)

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

by OLBA :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Zitat von Msam85 <carloselcheca@...>:

>
> 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);;


================================================
oliver@siouxsie2:~$ 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>, 2.); (<fun>, 3.); (<fun>, 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