list - ocaml bubble sort -


my basic idea implement bubble sort of type ('a list -> 'a list). use variables sorted , result. if change of elements in list, sorted becomes 1. otherwise, sorted remains 0. result 1 cycle of comparison.

i think there wrong sorted variable. can figure out problem is?

let rec sort (l: int list) : int list =     let sorted=0 in     let result = match l            | []->[]            | x::xs-> if xs=[] x                      else let y::ys = xs in                           if x<y x::sort(xs)                           else let sorted=1 in                                y::sort(x::ys)     in     if sorted=0 result     else sort(result) 

it seems me you're trying use sorted mutable variable. ocaml variables immutable. once bind variable value, binding can't changed. each of let sorted = statements defines new variable named sorted. last test show sorted equal 0. testing first definition of sorted, can never have other value 0.


Comments

Popular posts from this blog

android - getbluetoothservice() called with no bluetoothmanagercallback -

sql - ASP.NET SqlDataSource, like on SelectCommand -

ios - Undefined symbols for architecture armv7: "_OBJC_CLASS_$_SSZipArchive" -