Mantis Bug Tracker

View Issue Details Jump to Notes ] Issue History ] Print ]
IDProjectCategoryView StatusDate SubmittedLast Update
0004714OCamlOCaml generalpublic2009-02-11 20:082009-03-20 17:57
Reporterecc 
Assigned To 
PrioritynormalSeverityfeatureReproducibilityN/A
StatusacknowledgedResolutionopen 
PlatformOSOS Version
Product Version3.11.0 
Target VersionFixed in Version 
Summary0004714: please add List.cons
DescriptionSince :: is treated specially, one cannot use ( :: ) as a shorthand for the function (fun x y -> x :: y). It would be convenient to define List.cons to be this function, complementing List.hd and List.tl.

(Unless this would be offensive to les francophones, in which case another name would be fine :-)
TagsNo tags attached.
Attached Files

- Relationships

-  Notes
(0004837)
dbuenzli (reporter)
2009-02-12 01:38

I also needed this function really more than once. Daniel
(0004856)
doligez (administrator)
2009-03-16 15:18

First, :: is a constructor and in general in OCaml, you cannot use a constructor name as a shorthand for one the corresponding construction functions.
Second, :: only looks special because it is an infix symbol, but like any other infix symbol, if you enclose it in parentheses it is parsed as a prefix symbol.

For example: let x = (::) (1, []);;
(0004859)
dbuenzli (reporter)
2009-03-20 11:34

Yes but what we need is to use as a function and you cannot do that. See the following toplevel session :


        Objective Caml version 3.11.0

# ( < );;
- : 'a -> 'a -> bool = <fun>
# ( :: );;
Error: Syntax error
# (::);;
Error: Syntax error

Thus the need for List.cons e.g. to pass to folding functions.

Best,

Daniel
(0004860)
doligez (administrator)
2009-03-20 14:59

You cannot do that for any constructor. Why is there a particular need for :: ?
Since the function definition is only a few characters, it's not clear that we'll
want to "pollute" the stdlib namespace in this case. We'll have to argue it
between the OCaml developers to make a decision.
(0004861)
dbuenzli (reporter)
2009-03-20 17:57

While it is at first suprising that (::) cannot be used as a function as the other operators allow, I don't really care about this. But having List.cons instead of (fun a b -> a :: b) could be nice and more readable in certain cases; I know I missed it a few times.

OTOH, for (left) folds List.cons wouldn't be in the right order anyway.

Best,

Daniel

- Issue History
Date Modified Username Field Change
2009-02-11 20:08 ecc New Issue
2009-02-12 01:38 dbuenzli Note Added: 0004837
2009-03-16 15:18 doligez Note Added: 0004856
2009-03-16 15:18 doligez Status new => acknowledged
2009-03-20 11:34 dbuenzli Note Added: 0004859
2009-03-20 14:59 doligez Note Added: 0004860
2009-03-20 17:57 dbuenzli Note Added: 0004861


Copyright © 2000 - 2011 MantisBT Group
Powered by Mantis Bugtracker