SML-NJ is Nil vs Null List

I have a question about how SML from New Jersey interprets lists:

Suppose I have a function f(x : 'a, n : int) : 'a listsuch that freturns a list of ncopies x, for example. f(2,5) = [2,2,2,2,2], f(9,0) = [].

So, I go into REPL and I check f(9,0) = niland it returns true. From this, I suggested that you can use list = nilto check if the listlist is empty. I used this in a function and it did not work. I ended up learning that type definitions are different:

sml: 121.2-123.10 Error: operator and operand do not agree [equality type is required] operator domain: '' Z * '' Z
  operand: 'list *' Y list
  in the expression:
    xs = nil

(Where xs was my list). Then I found out that a way to check if a list is an empty list, c null list. Why is this so? What is going on with nil? Can someone explain this behavior to me?

I also note that obviously (case xs = of nilcoincides with validation null xs. Does this mean that nil is a type?

+4
source share
1 answer

, . 'a list. , . 1::[], - . , , . , null, , ( , ). . . , .

:

- fun f(x : 'a, n : int) : 'a list =
    case n of
        0 => []
      | _ => x::f(x, n-1);
val f = fn : 'a * int -> 'a list
- f(4,5);
val it = [4,4,4,4,4] : int list
- f(4,0);
val it = [] : int list

, 0, int. 'a list.

- it = [];
val it = true : bool

, , 'a list, . :

- [];
val it = [] : 'a list
- val list1 : int list = [];
val list1 = [] : int list
- val list2 : char list = [];
val list2 = [] : char list
- list1 = [];
val it = true : bool
- list2 = [];
val it = true : bool
- list1 = list2;
stdIn:6.1-6.14 Error: operator and operand don't agree [tycon mismatch]
  operator domain: int list * int list
  operand:         int list * char list
  in expression:
    list1 = list2

, case xs of nil - , , , nil ( []) 'a list. ( , case .) nil , 'a list , , ' t , , , .

+6

Source: https://habr.com/ru/post/1535853/


All Articles