Compare items in a list

I wrote this method, which specified a number, and the list will return a new list with the number inserted in the list in the correct position depending on its value. (I'm doing Insertion sorting.)

let rec insertinto number numbers =
  match numbers with
  | [] -> [number]
  | head::tail -> if head > number then number::numbers else head::(insertinto number tail)

F # guesses the type of this method:

val insertinto : 'a -> 'a list -> 'a list when 'a : comparison

If I test this method with

[4; 10; 15] |> insertinto 12

I get

val it : int list = [4; 12; 10; 15]

This is clearly wrong. The comparison "head> number" does not work correctly.

To make it work, I have to specify the type of the numbers parameter:

let rec insertinto number numbers: int list =

Then everything works, but I don’t want to use int list all the time, I want this to work with any type of list. As long as the type implements the comparison, it should work, of course.

Why does this work with an int list and not a general list? What am I missing?

change

ok, this is apparently a problem with mono only.

+3
source share
1

Mac Mono ( 2.8) MonoDevelop F #, ( ) .

, ​​ Mono. 2.6.x . - ( ), . Mono?

, :

fsmac:~ tomaspetricek$ mono -V
Mono JIT compiler version 2.8 (tarball Thu Oct  7 12:23:27 MDT 2010)
Copyright (C) 2002-2010 Novell, Inc and Contributors. www.mono-project.com

F # Mono 2.6.x, 2.8 ( , F #)

+3

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


All Articles