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

前端 未结 3 422
隐瞒了意图╮
隐瞒了意图╮ 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: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.Cons(4, List.Empty);;
    val it : int list = [4]
    
    > List.Cons;;
    val it : 'a * 'a list -> 'a list =  //'
    
    > List.Empty;;
    val it : int list = []
    

提交回复
热议问题