List Info

Thread: "ocaml_beginners"::[] List.fold_left




"ocaml_beginners"::[] List.fold_left
user name
2006-09-12 05:40:07
Hello,
  Can somebody provide me with List.fold_left function's
examples? 
  I've found only this: 
  fold_left f a [x1; x2;…;xn] = f(…(f (f a x1) x2)…)xn
  That is difficult to understand: what is f here -
function, a - parameter, [x1; x2;…;xn] - list?. 
  For example, I'm gonna to concatenate a list of strings.
Why this is not working?
  let addstrings inlist = List.fold_left ("")
inlist (fun xs x->xs^x);;
  But, this works: let rev_lst lst = List.fold_left (fun x
xs->xs:) []
lst;;

 				
---------------------------------
Get your own web address for just $1.99/1st yr. We'll help.
Yahoo! Small Business.

[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"::[] List.fold_left
user name
2006-09-12 06:23:07
Sure;

# let sum list = List.fold_left (+) 0 list;;
val sum : int list -> int = <fun>
# sum [1;2;3;4;5];;
- : int = 15
# let max_list = List.fold_left max min_int;;
val max_list : int list -> int
# max_list [1;4;3;2;-9;1];;
- : int = 4

Jonathan

On 9/12/06, Tyson Fugel <tfugelyahoo.com> wrote:
> Hello,
>  Can somebody provide me with List.fold_left
function's examples?
>  I've found only this:
>  fold_left f a [x1; x2;…;xn] = f(…(f (f a x1) x2)…)xn
>  That is difficult to understand: what is f here -
function, a - parameter, [x1; x2;…;xn] - list?.
>  For example, I'm gonna to concatenate a list of
strings. Why this is not working?
>  let addstrings inlist = List.fold_left
("") inlist (fun xs x->xs^x);;
>  But, this works: let rev_lst lst = List.fold_left (fun
x xs->xs:) []
lst;;
>
>
> ---------------------------------
> Get your own web address for just $1.99/1st yr. We'll
help. Yahoo! Small Business.
>
> [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"::[] List.fold_left
user name
2006-09-12 07:05:42
On Mon, Sep 11, 2006 at 10:40:07PM -0700, Tyson Fugel wrote:
> Hello,
>   Can somebody provide me with List.fold_left
function's examples? 
>   I've found only this: 
>   fold_left f a [x1; x2;…;xn] = f(…(f (f a x1) x2)…)xn
>   That is difficult to understand: what is f here -
function, a -
>   parameter, [x1; x2;…;xn] - list?.  For example, I'm
gonna to
>   concatenate a list of strings. Why this is not
working?
>   let addstrings inlist = List.fold_left
("") inlist (fun xs x->xs^x);;
>   But, this works: let rev_lst lst = List.fold_left
(fun x xs->xs:) []
lst;;

# let l = ["abc"; " def"; "
gfj"];;
val l : string list = ["abc"; "
def"; " gfj"]
# let s l = List.fold_left ( ^ ) "" l;;
val s : string list -> string = <fun>
# s l;;
- : string = "abc def gfj"

I understand your request for examples. That is how I learn
these stuff.

Regards
Johann
-- 
Johann Spies          Telefoon: 021-808 4036
Informasietegnologie, Universiteit van Stellenbosch

     "Let the wicked forsake his way, and the
unrighteous 
      man his thoughts; and let him return unto the LORD, 
      and He will have mercy upon him; and to our God, for 
      he will abundantly pardon."          Isaiah 55:7



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"::[] List.fold_left
user name
2006-09-12 08:49:46
On Mon, Sep 11, 2006 at 10:40:07PM -0700, Tyson Fugel wrote:
> Hello,
>   Can somebody provide me with List.fold_left
function's examples? 

Have a look at:

http://ww
w.ocaml-tutorial.org/if_statements,_loops_and_recursion#
Looping_over_lists

Rich.

-- 
Richard Jones, CTO Merjis Ltd.
Merjis - web marketing and technology - http://merjis.com
Internet Marketing and AdWords courses - http://merjis.com/courses - NEW!
Merjis blog - http://blog.merjis.com -
NEW!


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"::[] List.fold_left
user name
2006-09-12 08:49:46
On Mon, Sep 11, 2006 at 10:40:07PM -0700, Tyson Fugel wrote:
> Hello,
>   Can somebody provide me with List.fold_left
function's examples? 

Have a look at:

http://ww
w.ocaml-tutorial.org/if_statements,_loops_and_recursion#
Looping_over_lists

Rich.

-- 
Richard Jones, CTO Merjis Ltd.
Merjis - web marketing and technology - http://merjis.com
Internet Marketing and AdWords courses - http://merjis.com/courses - NEW!
Merjis blog - http://blog.merjis.com -
NEW!


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"::[] List.fold_left
user name
2006-09-12 08:49:46
On Mon, Sep 11, 2006 at 10:40:07PM -0700, Tyson Fugel wrote:
> Hello,
>   Can somebody provide me with List.fold_left
function's examples? 

Have a look at:

http://ww
w.ocaml-tutorial.org/if_statements,_loops_and_recursion#
Looping_over_lists

Rich.

-- 
Richard Jones, CTO Merjis Ltd.
Merjis - web marketing and technology - http://merjis.com
Internet Marketing and AdWords courses - http://merjis.com/courses - NEW!
Merjis blog - http://blog.merjis.com -
NEW!


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"::[] List.fold_left
user name
2006-09-12 11:41:29
On Tuesday 12 September 2006 06:40, Tyson Fugel wrote:
>   Can somebody provide me with List.fold_left
function's examples?
>   I've found only this:
>   fold_left f a [x1; x2;…;xn] = f(…(f (f a x1) x2)…)xn
>   That is difficult to understand: what is f here -
function, a -
> parameter, [x1; x2;…;xn] - list?. For example, I'm
gonna to concatenate a
> list of strings.

Use "String.concat".

> Why this is not working? let addstrings inlist = 
> List.fold_left ("") inlist (fun xs
x->xs^x);; But, this works: let rev_lst
> lst = List.fold_left (fun x xs->xs:) []
lst;;

This won't work because your arguments are the wrong way
around:

  List.fold_left ("") inlist (fun xs x->xs^x)

You could do:

# let concat inlist =
    List.fold_left (fun xs x -> xs^x) ("")
inlist;;
val concat : string list -> string = <fun>
# concat ["Hello"; " world!"];;
- : string = "Hello world!"

Or more succinctly:

# let concat = List.fold_left ( ^ ) "";;
val concat : string list -> string = <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"::[] List.fold_left
user name
2006-09-12 11:41:29
On Tuesday 12 September 2006 06:40, Tyson Fugel wrote:
>   Can somebody provide me with List.fold_left
function's examples?
>   I've found only this:
>   fold_left f a [x1; x2;…;xn] = f(…(f (f a x1) x2)…)xn
>   That is difficult to understand: what is f here -
function, a -
> parameter, [x1; x2;…;xn] - list?. For example, I'm
gonna to concatenate a
> list of strings.

Use "String.concat".

> Why this is not working? let addstrings inlist = 
> List.fold_left ("") inlist (fun xs
x->xs^x);; But, this works: let rev_lst
> lst = List.fold_left (fun x xs->xs:) []
lst;;

This won't work because your arguments are the wrong way
around:

  List.fold_left ("") inlist (fun xs x->xs^x)

You could do:

# let concat inlist =
    List.fold_left (fun xs x -> xs^x) ("")
inlist;;
val concat : string list -> string = <fun>
# concat ["Hello"; " world!"];;
- : string = "Hello world!"

Or more succinctly:

# let concat = List.fold_left ( ^ ) "";;
val concat : string list -> string = <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"::[] List.fold_left
user name
2006-09-12 16:07:44
This solution is amazing! 
  Thanks for examples and links.

Johann Spies <jspiessun.ac.za> wrote:
          On Mon, Sep 11, 2006 at 10:40:07PM -0700, Tyson
Fugel wrote:
> Hello,
> Can somebody provide me with List.fold_left function's
examples? 
> I've found only this: 
> fold_left f a [x1; x2;…;xn] = f(…(f (f a x1) x2)…)xn
> That is difficult to understand: what is f here -
function, a -
> parameter, [x1; x2;…;xn] - list?. For example, I'm
gonna to
> concatenate a list of strings. Why this is not working?
> let addstrings inlist = List.fold_left ("")
inlist (fun xs x->xs^x);;
> But, this works: let rev_lst lst = List.fold_left (fun
x xs->xs:) []
lst;;

# let l = ["abc"; " def"; "
gfj"];;
val l : string list = ["abc"; "
def"; " gfj"]
# let s l = List.fold_left ( ^ ) "" l;;
val s : string list -> string = <fun>
# s l;;
- : string = "abc def gfj"

I understand your request for examples. That is how I learn
these stuff.

Regards
Johann
-- 
Johann Spies Telefoon: 021-808 4036
Informasietegnologie, Universiteit van Stellenbosch

"Let the wicked forsake his way, and the unrighteous 
man his thoughts; and let him return unto the LORD, 
and He will have mercy upon him; and to our God, for 
he will abundantly pardon." Isaiah 55:7 


         

 		
---------------------------------
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"::[] List.fold_left
user name
2006-09-12 16:32:40
On Tue, 12 Sep 2006, Jon Harrop wrote:

> This won't work because your arguments are the wrong
way around:
>
>  List.fold_left ("") inlist (fun xs
x->xs^x)

And for a bit more edification, the reason that we know this
(well aside 
from just being familiar with the function) is that when you
look at the 
signature of the function you see:

# List.fold_left;;
- : ('a -> 'b -> 'a) -> 'a -> 'b list ->
'a = <fun>

What this tells us is that the first argument (call it f) is
a function 
that takes two arguments of its own, the second argument is
a value that 
has the same type as the first parameter of f (and the
return value of f), 
and the third argument to fold_left is a list of elements,
each of which 
has the same type as the second argument of f.

So in the case of string concatination, 'a = 'b = string,
and we'd want
(string -> string -> string) -> string -> string
list, but what you wrote 
is of the form: string -> string list -> (string ->
string -> string), 
hence the problem.

Further (and to address one of the original questions), it
might be 
worthwhile to look at a definition of fold_left:

let rec fold_left f base lst =
   match lst with
   | [] -> base
   | hd::tl -> fold_left f (f base hd) tl

So what fold_left does is to see if the list is empty, if it
is, it just 
returns the current value of base.  If not, then it takes
the first 
element of the list, hd, and computes a new value for base
by applying f 
to the current value of base and hd.  It then makes a
recursive call to 
itself using the new value of base, and the shorter tail of
the list. 
This leads to that structure mentioned in the OP:

fold_left f base [x1 x2 x3 ... xn] =
   f (f (f (f (f base x1) x2) x3) ... x(n-1)) xn)

So in the case of string concatination we have (ignoring the
actual 
evaluation order):
fold_left (^) "" ["a";
"b"; "c"]
=> fold_left (^) ("" ^ "a")
["b"; "c"]
=> fold_left (^) (("" ^ "a") ^
"b") ["c"]
=> fold_left (^) ((("" ^ "a") ^
"b") ^ "c") []
=> ((("" ^ "a") ^
"b") ^ "c")
=> "abc"

William D. Neumann

---

"There's just so many extra children, we could just
feed the
children to these tigers.  We don't need them, we're not
doing 
anything with them.

Tigers are noble and sleek; children are loud and
messy."

         -- Neko Case

Life is unfair.  Kill yourself or get over it.
 	-- Black Box Recorder


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/
 



[1-10] [11-16]

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