List Info

Thread: "ocaml_beginners"::[] when do we need function key word




"ocaml_beginners"::[] when do we need function key word
country flaguser name
United States
2007-05-21 22:17:32

1. I found In many cases there is no "function" key word.
2. And how do we differentiate with evaluation?

beginner, thanks!

#let rec fib n =
# if n < 2 then 1 else fib(n-1) + fib(n-2);;
val fib : int -> int = <fun>;

[Non-text portions of this message have been removed]

__._,_.___
.

__,_._,___
"ocaml_beginners"::[] Re: when do we need function key word
country flaguser name
United States
2007-05-21 22:24:51

I didn't find specific description in doc and only could learn from codes.
1. Or we Only need it when argument is a function as following?
#List.map (function n -> n * 2 + 1) [0;1;2;3;4];;
- : int list = [1; 3; 5; 7; 9]

2. Like in the complicated case, it's hard to read.
Or we don't need care about it?

let rec trans (env : vartype V.t) (node : ast_node) =
let rec trdec = function
A.FunctionDec functions ->
let mk_param fenv (name, typ, pos) =
let t = lookup_base_type env typ pos in
V.enter_param fenv name t (is_ptr t)
in
let mk_func_env (name, params, typ, _, pos) =
let ret_type = match typ with
Some x -> lookup_base_type env x pos
| None -> UNIT
and types = List.map (fun(_,t,p)-> lookup_base_type env t p)
params in
let fenv = V.enter_fun env name None types ret_type in
List.iter (mk_param fenv) params;
fenv
in
let trans_func fenv (_, _, _, body, _) =
let b = trans fenv (EXP body) in
add_function (V.frame fenv) b
in
let envs = (List.map mk_func_env functions) in
List.iter2 trans_func envs functions;
T.nil
| A.VarDec(name, typ, init, pos) ->
let e,t = trexp init in
......

[Non-text portions of this message have been removed]

__._,_.___
.

__,_._,___
Re: "ocaml_beginners"::[] when do we need function key word
country flaguser name
United Kingdom
2007-05-22 06:48:32

On Tue, May 22, 2007 at 11:17:32AM +0800, LianYang wrote:
&gt; 1. I found In many cases there is no "function" key word.
&gt; 2. And how do we differentiate with evaluation?
>
> beginner, thanks!
>
> #let rec fib n =
> # if n < 2 then 1 else fib(n-1) + fib(n-2);;
> val fib : int -> int = <fun>;

&quot;function&quot;
----------

The following two definitions are the same:

type abc = A | B | C

let abc_of_string1 = function
| "A&quot; -> A
| "B&quot; -> B
| "C&quot; -> C
| _ -> failwith "the string should be A, B or C"

let abc_of_string2 str =
match str with
| "A&quot; -> A
| "B&quot; -> B
| "C&quot; -> C
| _ -> failwith "the string should be A, B or C"

function only works with a single parameter, it does pattern
matching, and it returns a function.

"fun"
-----

The following function definitions are the same:

let add1 = fun x y z -> x + y + z

let add2 x y z = x + y + z

fun takes several parameters, doesn't do any pattern matching and
returns a function.

More interesting is when you use them ... "fun&quot; is often used inside
higher-order functions like List.map, to build an anonymous function,
like this:

List.map (fun x -> 2 * x) [ 1; 2; 3 ]

Rich.

--
Richard Jones
Red Hat

__._,_.___
.

__,_._,___
Re: "ocaml_beginners"::[] when do we need function key word
country flaguser name
United Kingdom
2007-05-22 06:51:57

On Tuesday 22 May 2007 12:48:32 Richard Jones wrote:
&gt; fun takes several parameters, doesn't do any pattern matching and
> returns a function.

That isn't quite true. "fun&quot; certainly can't pattern match over sum types but
it can pattern match over products:

fun (x, y) -> (y, x)

--
Dr Jon D Harrop, Flying Frog Consultancy Ltd.
The F#.NET Journal
http://www.ffconsultancy.com/products/fsharp_journal/?e

__._,_.___
.

__,_._,___
[1-4]

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