Danushka Bandara
Danushka Bandara

Reputation: 43

Standard ML unusual datatype

for the following function,

fun foo (x, y) = 
  if x > 5 then y[1]
  else y[0];

standard ML shows the data type of y to be (int list → 'a). Should it not be ('a list) instead?

Upvotes: 1

Views: 47

Answers (1)

Silvio Mayolo
Silvio Mayolo

Reputation: 70287

We don't use [] to access list elements in SML like we do in, say, Python. This

y[1]

is seen as

y [1]

i.e. the function y applied to a single argument, which happens to be the list containing the number 1. Which is why the inferred type is "a function that accepts a list of integers".

In ML, we index into a list the same way we do everything else: with a function. There's no special syntax; it's just a function.

fun foo (x, y) = if x > 5 then List.nth (y, 1)
                 else List.nth (y, 0);

Upvotes: 1

Related Questions