Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

please add List.cons #4714

Closed
vicuna opened this issue Feb 11, 2009 · 6 comments
Closed

please add List.cons #4714

vicuna opened this issue Feb 11, 2009 · 6 comments
Assignees

Comments

@vicuna
Copy link

vicuna commented Feb 11, 2009

Original bug ID: 4714
Reporter: ecc
Assigned to: @gasche
Status: closed (set by @xavierleroy on 2016-12-07T10:48:59Z)
Resolution: fixed
Priority: normal
Severity: feature
Version: 3.11.0
Fixed in version: 4.03.0+dev / +beta1
Category: ~DO NOT USE (was: OCaml general)
Monitored by: @dbuenzli

Bug description

Since :: 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 :-)

@vicuna
Copy link
Author

vicuna commented Feb 12, 2009

Comment author: @dbuenzli

I also needed this function really more than once. Daniel

@vicuna
Copy link
Author

vicuna commented Mar 16, 2009

Comment author: @damiendoligez

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, []);;

@vicuna
Copy link
Author

vicuna commented Mar 20, 2009

Comment author: @dbuenzli

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 =

( :: );;

Error: Syntax error

(::);;

Error: Syntax error

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

Best,

Daniel

@vicuna
Copy link
Author

vicuna commented Mar 20, 2009

Comment author: @damiendoligez

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.

@vicuna
Copy link
Author

vicuna commented Mar 20, 2009

Comment author: @dbuenzli

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

@vicuna
Copy link
Author

vicuna commented Apr 26, 2015

Comment author: @gasche

List.cons should be included in 4.03.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants