Advertisement
Guest User

Untitled

a guest
Jan 21st, 2019
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
F# 1.19 KB | None | 0 0
  1. type SortType =
  2.     | Ascending
  3.     | Descending
  4.     | Unknown
  5.     | NoSort
  6.  
  7. let rec isCurrentlySorted lastItem collectionTail currentSortType =
  8.     match collectionTail with
  9.     | [] -> currentSortType
  10.     | head :: tail ->
  11.         let suggestedSortType =
  12.             if head = lastItem then currentSortType
  13.             elif head > lastItem then Ascending
  14.             else Descending
  15.         if suggestedSortType = Unknown || suggestedSortType = currentSortType then isCurrentlySorted head tail currentSortType
  16.         elif currentSortType = Unknown then isCurrentlySorted head tail suggestedSortType      
  17.         else NoSort      
  18.        
  19. let isSorted collection =
  20.     let currentSort = match collection with
  21.         | [] -> NoSort
  22.         | [single] -> Unknown
  23.         | head :: tail -> isCurrentlySorted head tail Unknown
  24.     match currentSort with
  25.         | Ascending -> "Yes, ascending"
  26.         | Descending -> "Yes, descending"
  27.         | Unknown -> "ХЗ"
  28.         | _ -> "No"
  29.        
  30. isSorted [1;2;3]
  31. isSorted [3;2;1]
  32. isSorted [3;3;3]
  33. isSorted [1;2;2;3]
  34. isSorted [3;2;2;1]
  35. isSorted []
  36. isSorted [2]
  37. isSorted [1;2;2;2;3]
  38. isSorted [3;2;3;1]
  39. isSorted [3;2;2;2;0;2;-1]
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement