Use list cons operator (a :: b) as a function

前端 未结 3 421
隐瞒了意图╮
隐瞒了意图╮ 2021-01-07 21:40

F# lets you turn operators into functions by surrounding them with ( ): for instance, (+) is of type int -> int -> int

相关标签:
3条回答
  • 2021-01-07 22:18

    Unfortunately, no, you can't. :: is not an operator, but a "symbolic keyword" according to the language grammar (see section 3.6 of the spec), as are :?> and a few others. The language doesn't seem completely consistent here, though, since there are a few symbolic keywords which can be treated as though they were operators (at least (*) and (<@ @>)).

    0 讨论(0)
  • 2021-01-07 22:24

    Paraphrased from http://cs.hubfs.net/forums/permalink/11713/11713/ShowThread.aspx#11713

    (::) is a discriminated union 'constructor' for the list<'a> type, and so raised the question of whether as a function value its arguments should be curried (like +) or tupled (like all DU constructors). Either way seems fishy/unexpected to some people, so F# simply disallows the construct.

    Of course you can always write e.g.

    let cons x y = x :: y
    

    and use cons, or just use a lambda fun x y -> x::y, if you want a "curried prefix function of two args" for this.

    0 讨论(0)
  • 2021-01-07 22:38

    :: and [] can both be represented by List<_>.Cons and List<_>.Empty respectively. Keep in mind though that the former takes a tuple as an argument. These are here so lists can be created in languages other than F#.

    > List.Cons(4, List.Empty);;
    val it : int list = [4]
    
    > 4::[];;
    val it : int list = [4]
    
    > List<int>.Cons(4, List<int>.Empty);;
    val it : int list = [4]
    
    > List.Cons;;
    val it : 'a * 'a list -> 'a list = <fun:clo@24-7> //'
    
    > List<int>.Empty;;
    val it : int list = []
    
    0 讨论(0)
提交回复
热议问题