|
List Info
Thread: "ocaml_beginners"::[] tail recursive
|
|
| "ocaml_beginners"::[] tail
recursive |

|
2006-09-10 16:54:19 |
Hello everybody,
how can I make this sum (fun x -> x < 2) [1;2;3];; a
tail recursive funtion?:
let sum f alist = let rec sum f alist num =
match alist with []->num
|hd::tl->sum (if (f hd)=true then (num++) else num) tl
num;;
Ty
---------------------------------
Yahoo! Messenger with Voice. Make PC-to-Phone Calls to the
US (and 30+ countries) for 2¢/min or less.
[Non-text portions of this message have been removed]
Archives up to August 22, 2005 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:
ht
tp://groups.yahoo.com/group/ocaml_beginners/join
(Yahoo! ID required)
<*> To change settings via email:
mailto:ocaml_beginners-digest@yahoogroups.com
mailto:ocaml_beginners-fullfeatured@yahoogroups.com
<*> To unsubscribe from this group, send an email to:
ocaml_beginners-unsubscribe@yahoogroups.com
<*> Your use of Yahoo! Groups is subject to:
http://docs.yahoo.c
om/info/terms/
|
|
| "ocaml_beginners"::[] tail
recursive |

|
2006-09-10 17:07:11 |
On Sun, 10 Sep 2006 18:54:19 +0200, Tyson Fugel
<tfugel yahoo.com> wrote:
> Hello everybody,
> how can I make this sum (fun x -> x < 2)
[1;2;3];; a tail recursive
> funtion?:
> let sum f alist = let rec sum f alist num =
> match alist with []->num
> |hd::tl->sum (if (f hd)=true then (num++) else num)
tl num;;
> Ty
not the most efficient but short answer
let sum f alist = List.length (List.filter f alist)
or
let sum f alist =
let rec aux num = function
| [] -> num
| h :: t when f h -> aux (succ num) t
| h :: t -> aux (num) t
in
aux 0 alist
val sum : ('a -> bool) -> 'a list -> int =
<fun>
# sum (fun x -> x < 2) [1;2;3];;
- : int = 1
I think the trick is that you should pass the new value of
num to the
called recursive function.
peter
Archives up to August 22, 2005 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/
<*> To unsubscribe from this group, send an email to:
ocaml_beginners-unsubscribe@yahoogroups.com
<*> Your use of Yahoo! Groups is subject to:
http://docs.yahoo.c
om/info/terms/
|
|
| "ocaml_beginners"::[] tail
recursive |

|
2006-09-10 22:24:37 |
Thanks Peter,
I've changed my code like that, but it doesn't count
correctly, I suppose that I'd put a function instead of
last myfun.
let sum f alist =let rec mysum myfun alst num=
match alst with
[] -> num
| x: s
-> mysum myfun xs (num+1)
in mysum f alist 0;;
Yerkin
Peter Halacsy <peter halacsy.com> wrote:
On Sun, 10 Sep 2006 18:54:19 +0200, Tyson Fugel wrote:
> Hello everybody,
> how can I make this sum (fun x -> x < 2)
[1;2;3];; a tail recursive
> funtion?:
> let sum f alist = let rec sum f alist num =
> match alist with []->num
> |hd::tl->sum (if (f hd)=true then (num++) else num)
tl num;;
> Ty
not the most efficient but short answer
let sum f alist = List.length (List.filter f alist)
or
let sum f alist =
let rec aux num = function
| [] -> num
| h :: t when f h -> aux (succ num) t
| h :: t -> aux (num) t
in
aux 0 alist
val sum : ('a -> bool) -> 'a list -> int =
# sum (fun x -> x < 2) [1;2;3];;
- : int = 1
I think the trick is that you should pass the new value of
num to the
called recursive function.
peter
Archives up to August 22, 2005 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
---------------------------------
All-new Yahoo! Mail - Fire up a more powerful email and get
things done faster.
[Non-text portions of this message have been removed]
Archives up to August 22, 2005 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:
ht
tp://groups.yahoo.com/group/ocaml_beginners/join
(Yahoo! ID required)
<*> To change settings via email:
mailto:ocaml_beginners-digest@yahoogroups.com
mailto:ocaml_beginners-fullfeatured@yahoogroups.com
<*> To unsubscribe from this group, send an email to:
ocaml_beginners-unsubscribe@yahoogroups.com
<*> Your use of Yahoo! Groups is subject to:
http://docs.yahoo.c
om/info/terms/
|
|
| "ocaml_beginners"::[] tail
recursive |

|
2006-09-11 05:38:39 |
On Mon, 11 Sep 2006 00:24:37 +0200, Tyson Fugel
<tfugel yahoo.com> wrote:
> Thanks Peter,
> I've changed my code like that, but it doesn't
count correctly, I
> suppose that I'd put a function instead of last myfun.
> let sum f alist =let rec mysum myfun alst num=
> match alst with
> [] -> num
> | x: s
-> mysum myfun xs (num+1)
> in mysum f alist 0;;
> Yerkin
The function f is missing from mysum. This code calculates
the length of
alist, isn't it?
As far as I understand you want to count the number of
elements of a list
that satisfies the predicate f.
> let sum f alist =let rec mysum myfun alst num=
> match alst with
> [] -> num
> | x: s
-> mysum myfun xs (num+1)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
(* this matches every element of alist *)
(* change to *)
(* | x: s
-> if f x then myfun xs (num+1) else myfun xs (num) *)
> in mysum f alist 0;;
peter
Archives up to August 22, 2005 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:
ht
tp://groups.yahoo.com/group/ocaml_beginners/join
(Yahoo! ID required)
<*> To change settings via email:
mailto:ocaml_beginners-digest@yahoogroups.com
mailto:ocaml_beginners-fullfeatured@yahoogroups.com
<*> To unsubscribe from this group, send an email to:
ocaml_beginners-unsubscribe@yahoogroups.com
<*> Your use of Yahoo! Groups is subject to:
http://docs.yahoo.c
om/info/terms/
|
|
| "ocaml_beginners"::[] tail
recursive |

|
2006-09-11 05:47:29 |
2006/9/10, Tyson Fugel <tfugel yahoo.com>:
> Hello everybody,
> how can I make this sum (fun x -> x < 2)
[1;2;3];; a tail recursive funtion?:
>
> let sum f alist = let rec sum f alist num =
> match alist with []->num
> |hd::tl->sum (if (f hd)=true then (num++) else num)
tl num;;
Mmm, it is already a tail recursive function : nothing is
done after
the recursive call to sum. But this is not ocaml code (num++
???). You
want:
let sum f alist =
let rec sum_rec f alist num =
match alist with
| [] -> num
| hd::tl ->
sum_rec f tl (if (f hd)=true then (num+1) else num) in
sum_rec f alist 0;;
Archives up to August 22, 2005 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:
ht
tp://groups.yahoo.com/group/ocaml_beginners/join
(Yahoo! ID required)
<*> To change settings via email:
mailto:ocaml_beginners-digest@yahoogroups.com
mailto:ocaml_beginners-fullfeatured@yahoogroups.com
<*> To unsubscribe from this group, send an email to:
ocaml_beginners-unsubscribe@yahoogroups.com
<*> Your use of Yahoo! Groups is subject to:
http://docs.yahoo.c
om/info/terms/
|
|
| "ocaml_beginners"::[] tail
recursive |

|
2006-09-12 11:44:34 |
On Sunday 10 September 2006 17:54, Tyson Fugel wrote:
> Hello everybody,
> how can I make this sum (fun x -> x < 2)
[1;2;3];; a tail recursive
> funtion?:
>
> let sum f alist = let rec sum f alist num =
> match alist with []->num
>
> |hd::tl->sum (if (f hd)=true then (num++) else num)
tl num;;
# let sum f list =
List.fold_left ( + ) 0 (List.filter f list);;
val sum : (int -> bool) -> int list -> int =
<fun>
or the deforested:
# let sum f list =
List.fold_left (fun n e -> if f e then n+1 else n) 0
list;;
val sum : ('a -> bool) -> 'a list -> int =
<fun>
--
Dr Jon D Harrop, Flying Frog Consultancy Ltd.
Objective CAML for Scientists
http://www.ffconsultancy.com/products/ocaml_for_scient
ists
Archives up to August 22, 2005 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:
ht
tp://groups.yahoo.com/group/ocaml_beginners/join
(Yahoo! ID required)
<*> To change settings via email:
mailto:ocaml_beginners-digest@yahoogroups.com
mailto:ocaml_beginners-fullfeatured@yahoogroups.com
<*> To unsubscribe from this group, send an email to:
ocaml_beginners-unsubscribe@yahoogroups.com
<*> Your use of Yahoo! Groups is subject to:
http://docs.yahoo.c
om/info/terms/
|
|
| "ocaml_beginners"::[] tail
recursive |

|
2006-09-12 11:44:34 |
On Sunday 10 September 2006 17:54, Tyson Fugel wrote:
> Hello everybody,
> how can I make this sum (fun x -> x < 2)
[1;2;3];; a tail recursive
> funtion?:
>
> let sum f alist = let rec sum f alist num =
> match alist with []->num
>
> |hd::tl->sum (if (f hd)=true then (num++) else num)
tl num;;
# let sum f list =
List.fold_left ( + ) 0 (List.filter f list);;
val sum : (int -> bool) -> int list -> int =
<fun>
or the deforested:
# let sum f list =
List.fold_left (fun n e -> if f e then n+1 else n) 0
list;;
val sum : ('a -> bool) -> 'a list -> int =
<fun>
--
Dr Jon D Harrop, Flying Frog Consultancy Ltd.
Objective CAML for Scientists
http://www.ffconsultancy.com/products/ocaml_for_scient
ists
Archives up to August 22, 2005 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:
ht
tp://groups.yahoo.com/group/ocaml_beginners/join
(Yahoo! ID required)
<*> To change settings via email:
mailto:ocaml_beginners-digest@yahoogroups.com
mailto:ocaml_beginners-fullfeatured@yahoogroups.com
<*> To unsubscribe from this group, send an email to:
ocaml_beginners-unsubscribe@yahoogroups.com
<*> Your use of Yahoo! Groups is subject to:
http://docs.yahoo.c
om/info/terms/
|
|
| "ocaml_beginners"::[] tail
recursive |

|
2006-09-19 04:29:38 |
Hi,
What is the difference between lp1 and lp2? Can I say that
both functions are forward recursion, not a tail recursion,
because they return "Stack overflow during evaluation
(looping recursion?)" then I call lp1() or lp2()? A
tail recursive function shouldn't return "Stack
overflow", am I right?
let rec lp1 () = lp1(); ()
let rec lp2 () = lp2();;
val lp1 : unit -> unit = <fun>
val lp2 : unit -> 'a = <fun>
---------------------------------
Do you Yahoo!?
Everyone is raving about the all-new Yahoo! Mail.
[Non-text portions of this message have been removed]
Archives up to August 22, 2005 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:
ht
tp://groups.yahoo.com/group/ocaml_beginners/join
(Yahoo! ID required)
<*> To change settings via email:
mailto:ocaml_beginners-digest@yahoogroups.com
mailto:ocaml_beginners-fullfeatured@yahoogroups.com
<*> To unsubscribe from this group, send an email to:
ocaml_beginners-unsubscribe@yahoogroups.com
<*> Your use of Yahoo! Groups is subject to:
http://docs.yahoo.c
om/info/terms/
|
|
| "ocaml_beginners"::[] tail
recursive |

|
2006-09-19 05:17:10 |
Actually, lp2 doesn't overflow the stack. The fact the last
thing it
does is call itself makes it tail-recursive.
lp1 is not tail recursive because the last thing it does is
return
unit (after calling itself).
On 9/19/06, Tyson Fugel <tfugel yahoo.com> wrote:
> Hi,
> What is the difference between lp1 and lp2? Can I say
that both functions are forward recursion, not a tail
recursion, because they return "Stack overflow during
evaluation (looping recursion?)" then I call lp1() or
lp2()? A tail recursive function shouldn't return
"Stack overflow", am I right?
> let rec lp1 () = lp1(); ()
> let rec lp2 () = lp2();;
> val lp1 : unit -> unit = <fun>
> val lp2 : unit -> 'a = <fun>
>
>
>
> ---------------------------------
> Do you Yahoo!?
> Everyone is raving about the all-new Yahoo! Mail.
>
> [Non-text portions of this message have been removed]
>
>
>
> Archives up to August 22, 2005 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
>
>
>
>
>
>
>
>
>
>
>
Archives up to August 22, 2005 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:
ht
tp://groups.yahoo.com/group/ocaml_beginners/join
(Yahoo! ID required)
<*> To change settings via email:
mailto:ocaml_beginners-digest@yahoogroups.com
mailto:ocaml_beginners-fullfeatured@yahoogroups.com
<*> To unsubscribe from this group, send an email to:
ocaml_beginners-unsubscribe@yahoogroups.com
<*> Your use of Yahoo! Groups is subject to:
http://docs.yahoo.c
om/info/terms/
|
|
| "ocaml_beginners"::[] tail
recursive |

|
2006-09-19 07:06:12 |
but if you trace it lp2() finally shows a stack overflow
Jonathan Roewen <jonathan.roewen gmail.com> wrote:
Actually, lp2 doesn't overflow the stack. The fact the
last thing it
does is call itself makes it tail-recursive.
lp1 is not tail recursive because the last thing it does is
return
unit (after calling itself).
On 9/19/06, Tyson Fugel <tfugel yahoo.com> wrote:
> Hi,
> What is the difference between lp1 and lp2? Can I say
that both functions are forward recursion, not a tail
recursion, because they return "Stack overflow during
evaluation (looping recursion?)" then I call lp1() or
lp2()? A tail recursive function shouldn't return
"Stack overflow", am I right?
> let rec lp1 () = lp1(); ()
> let rec lp2 () = lp2();;
> val lp1 : unit -> unit = <fun>
> val lp2 : unit -> 'a = <fun>
>
>
>
> ---------------------------------
> Do you Yahoo!?
> Everyone is raving about the all-new Yahoo! Mail.
>
> [Non-text portions of this message have been removed]
>
>
>
> Archives up to August 22, 2005 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
>
>
>
>
>
>
>
>
>
>
>
---------------------------------
How low will we go? Check out Yahoo! Messenger’s low
PC-to-Phone call rates.
[Non-text portions of this message have been removed]
Archives up to August 22, 2005 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:
ht
tp://groups.yahoo.com/group/ocaml_beginners/join
(Yahoo! ID required)
<*> To change settings via email:
mailto:ocaml_beginners-digest@yahoogroups.com
mailto:ocaml_beginners-fullfeatured@yahoogroups.com
<*> To unsubscribe from this group, send an email to:
ocaml_beginners-unsubscribe@yahoogroups.com
<*> Your use of Yahoo! Groups is subject to:
http://docs.yahoo.c
om/info/terms/
|
|
|
|