Sorry, an error occurred while loading the content.

## functional attempt and anonymous functions

Expand Messages
• This is my first real attempt at trying to work with the functional aspect of ocaml. Its a pretty trivial problem, but was curious if there were any
Message 1 of 4 , Sep 23, 2010
This is my first real attempt at trying to work with the functional aspect of
ocaml. Its a pretty trivial problem, but was curious if there were any
tips/pointers/errors about my functional implementation.

open Printf

(* Let's crunch some numbers *)

let calcArea f g lbound rbound n =
let rec sum_domain f g lbound dx i n =
(* f = function we are calculating sums on
g = function to calculate area
lbound = lower bound of domain
dx = change in X used to calculate rbound
i = current node
n = # of intervals *)
let a = lbound +. float_of_int (i - 1) *. dx in
if i <= n then
g dx (f a) (f (a +. dx)) +. sum_domain f g lbound dx (succ i) n
else
0.
in
let dx = (rbound -. lbound) /. float_of_int n in
sum_domain f g lbound dx 1 n
;;

let f = (function x -> x ** 2.) in
let g x y z =
x *. (min y z) and

h x y z =
x *. (max y z) and
i x y z =
x *. (min y z) +. 0.5 *. x *. ((max y z) -. (min y z))
in
printf "Inscribed Area : \t\t\t %f\n" (calcArea f g 0. 4. 25);
printf "Circumscribed Area : \t\t\t %f\n" (calcArea f h 0. 4. 25);
printf "Area of Rectangle + Area of Triangle : \t %f\n" (calcArea f i 0. 4. 25)
;;

Also, I was initially trying to pass an anymous area function like " (function
(x,y,z) -> x *. (min y z))" , but it wasn't liking it. If I want an anonymous
function with multiple arguments do I have to curry it? "(function x -> function
y -> function z -> x *. (min y z)).

Thanks,
Ian
• ... I ll try to give you a couple of tips, if I can. Is it Yahoo Groups that is removing all of the formatting from messages? Makes it really hard to read
Message 2 of 4 , Sep 23, 2010
On Thu, 23 Sep 2010, Ian Thiele wrote:
> This is my first real attempt at trying to work with the functional aspect of
> ocaml. Its a pretty trivial problem, but was curious if there were any
> tips/pointers/errors about my functional implementation.

I'll try to give you a couple of tips, if I can. Is it Yahoo Groups
that is removing all of the formatting from messages? Makes it really
hard to read code.

> let f = (function x -> x ** 2.) in

This isn't *really* an anonymous function. It's the equivalent of
writing this:

let f x = x ** 2.

> let g x y z =
> x *. (min y z) and
>
> h x y z =
> x *. (max y z) and
> i x y z =
> x *. (min y z) +. 0.5 *. x *. ((max y z) -. (min y z))
> in
> printf "Inscribed Area : \t\t\t %f\n" (calcArea f g 0. 4. 25);
> printf "Circumscribed Area : \t\t\t %f\n" (calcArea f h 0. 4. 25);
> printf "Area of Rectangle + Area of Triangle : \t %f\n" (calcArea f i 0. 4. 25)
> ;;

This all looks much cleaner to me when written like this:

let f x = x ** 2.
let g x y z = x *. (min y z)
let h x y z = x *. (max y z)
let i x y z = x *. (min y z) +. 0.5 *. x *. ((max y z) -. (min y z))
let _ =
printf "Inscribed Area : \t\t\t %f\n" (calcArea f g 0. 4. 25);
printf "Circumscribed Area : \t\t\t %f\n" (calcArea f h 0. 4. 25);
printf "Area of Rectangle + Area of Triangle : \t %f\n" (calcArea f i 0. 4. 25)
;;

You don't need to use 'and', and you also don't need to use an 'in'
keyword, though you'll notice that all of the 'printf' bits are in the
function '_', which is a bit different.

> Also, I was initially trying to pass an anymous area function like " (function
> (x,y,z) -> x *. (min y z))" , but it wasn't liking it. If I want an anonymous
> function with multiple arguments do I have to curry it? "(function x -> function
> y -> function z -> x *. (min y z)).

No, consider this, for instance:

List.fold_left (fun ctr itm -> ctr + itm) 0 [ 1; 2; 3; 4; 5 ]

The "fun" bit is an anonymous function; a function without a name.

--
Erik Arneson <dybbuk@...>
GPG Key ID : 1024D/62DA1D25 BitCoin : 1LqvuGUqJ4ZUSoE7YE9ngETjwp4yZ2uSdP
Office : +1.541.291.9776 Skype : callto://pymander
http://www.leisurenouveau.com/

[Non-text portions of this message have been removed]
• Hello there-- I can t say I ve gone through your program in detail, but the answer to your anonymous function question can be found in
Message 3 of 4 , Sep 23, 2010
Hello there--

I can't say I've gone through your program in detail, but the answer to your
anonymous function question can be found in

http://stackoverflow.com/questions/1604270/ocaml-what-is-the-different-between-fun-and-function-keywords

Erick

On Thu, Sep 23, 2010 at 2:22 PM, Ian Thiele <icthiele@...> wrote:

>
>
> This is my first real attempt at trying to work with the functional aspect
> of
> ocaml. Its a pretty trivial problem, but was curious if there were any
> tips/pointers/errors about my functional implementation.
>
> open Printf
>
> (* Let's crunch some numbers *)
>
> let calcArea f g lbound rbound n =
> let rec sum_domain f g lbound dx i n =
> (* f = function we are calculating sums on
> g = function to calculate area
> lbound = lower bound of domain
> dx = change in X used to calculate rbound
> i = current node
> n = # of intervals *)
> let a = lbound +. float_of_int (i - 1) *. dx in
> if i <= n then
> g dx (f a) (f (a +. dx)) +. sum_domain f g lbound dx (succ i) n
> else
> 0.
> in
> let dx = (rbound -. lbound) /. float_of_int n in
> sum_domain f g lbound dx 1 n
> ;;
>
> let f = (function x -> x ** 2.) in
> let g x y z =
> x *. (min y z) and
>
> h x y z =
> x *. (max y z) and
> i x y z =
> x *. (min y z) +. 0.5 *. x *. ((max y z) -. (min y z))
> in
> printf "Inscribed Area : \t\t\t %f\n" (calcArea f g 0. 4. 25);
> printf "Circumscribed Area : \t\t\t %f\n" (calcArea f h 0. 4. 25);
> printf "Area of Rectangle + Area of Triangle : \t %f\n" (calcArea f i 0. 4.
> 25)
> ;;
>
> Also, I was initially trying to pass an anymous area function like "
> (function
> (x,y,z) -> x *. (min y z))" , but it wasn't liking it. If I want an
> anonymous
> function with multiple arguments do I have to curry it? "(function x ->
> function
> y -> function z -> x *. (min y z)).
>
> Thanks,
> Ian
>
>
>

[Non-text portions of this message have been removed]
• Aha just what I was looking for. Thanks Erick.
Message 4 of 4 , Sep 23, 2010
Aha just what I was looking for. Thanks Erick.

--- In ocaml_beginners@yahoogroups.com, Erick Matsen <ematsen@...> wrote:
>
> Hello there--
>
>
> I can't say I've gone through your program in detail, but the answer to your
> anonymous function question can be found in
>
> http://stackoverflow.com/questions/1604270/ocaml-what-is-the-different-between-fun-and-function-keywords
>
>
> Erick
>
> On Thu, Sep 23, 2010 at 2:22 PM, Ian Thiele <icthiele@...> wrote:
>
> >
> >
> > This is my first real attempt at trying to work with the functional aspect
> > of
> > ocaml. Its a pretty trivial problem, but was curious if there were any
> > tips/pointers/errors about my functional implementation.
> >
> > open Printf
> >
> > (* Let's crunch some numbers *)
> >
> > let calcArea f g lbound rbound n =
> > let rec sum_domain f g lbound dx i n =
> > (* f = function we are calculating sums on
> > g = function to calculate area
> > lbound = lower bound of domain
> > dx = change in X used to calculate rbound
> > i = current node
> > n = # of intervals *)
> > let a = lbound +. float_of_int (i - 1) *. dx in
> > if i <= n then
> > g dx (f a) (f (a +. dx)) +. sum_domain f g lbound dx (succ i) n
> > else
> > 0.
> > in
> > let dx = (rbound -. lbound) /. float_of_int n in
> > sum_domain f g lbound dx 1 n
> > ;;
> >
> > let f = (function x -> x ** 2.) in
> > let g x y z =
> > x *. (min y z) and
> >
> > h x y z =
> > x *. (max y z) and
> > i x y z =
> > x *. (min y z) +. 0.5 *. x *. ((max y z) -. (min y z))
> > in
> > printf "Inscribed Area : \t\t\t %f\n" (calcArea f g 0. 4. 25);
> > printf "Circumscribed Area : \t\t\t %f\n" (calcArea f h 0. 4. 25);
> > printf "Area of Rectangle + Area of Triangle : \t %f\n" (calcArea f i 0. 4.
> > 25)
> > ;;
> >
> > Also, I was initially trying to pass an anymous area function like "
> > (function
> > (x,y,z) -> x *. (min y z))" , but it wasn't liking it. If I want an
> > anonymous
> > function with multiple arguments do I have to curry it? "(function x ->
> > function
> > y -> function z -> x *. (min y z)).
> >
> > Thanks,
> > Ian
> >
> >
> >
>
>
> [Non-text portions of this message have been removed]
>
Your message has been successfully submitted and would be delivered to recipients shortly.