semigroup

Can you formulate the Bubble sort as a monoid or semigroup?

大憨熊 提交于 2019-12-30 09:32:33
问题 Given the following pseudocode for the bubble-sort procedure bubbleSort( A : list of sortable items ) repeat swapped = false for i = 1 to length(A) - 1 inclusive do: /* if this pair is out of order */ if A[i-1] > A[i] then /* swap them and remember something changed */ swap( A[i-1], A[i] ) swapped = true end if end for until not swapped end procedure Here is the code for Bubble Sort as Scala def bubbleSort[T](arr: Array[T])(implicit o: Ordering[T]) { import o._ val consecutiveIndices = (arr

Can you formulate the Bubble sort as a monoid or semigroup?

一个人想着一个人 提交于 2019-12-30 09:31:48
问题 Given the following pseudocode for the bubble-sort procedure bubbleSort( A : list of sortable items ) repeat swapped = false for i = 1 to length(A) - 1 inclusive do: /* if this pair is out of order */ if A[i-1] > A[i] then /* swap them and remember something changed */ swap( A[i-1], A[i] ) swapped = true end if end for until not swapped end procedure Here is the code for Bubble Sort as Scala def bubbleSort[T](arr: Array[T])(implicit o: Ordering[T]) { import o._ val consecutiveIndices = (arr

Defining a Semigroup instance that depends on itself

試著忘記壹切 提交于 2019-12-24 07:59:40
问题 ... or mishaps of a Haskell programmer that has to code Scala, part 5. I have the following structure in Scala: case class ResourceTree( resources: Map[String, ResourceTree] ) And, using Cats, I would like to define a Semigroup instance of it. object ResourceTreeInstances { implicit val semigroupInstance = new Semigroup[ResourceTree] { override def combine(x: ResourceTree, y: ResourceTree): ResourceTree = { ResourceTree( x.resources |+| y.resources ) } } This will result in the following

Floating points wrapped in Sum and Product even if it breaks associativity?

旧城冷巷雨未停 提交于 2019-12-11 08:50:09
问题 Trying to learn Haskell and stumbled upon this: Prelude> import Data.Semigroup Prelude Data.Semigroup> let x = Sum 0.1 <> Sum 0.2 <> Sum 0.3 Prelude Data.Semigroup> let y = (Sum 0.1 <> Sum 0.2) <> Sum 0.3 Prelude Data.Semigroup> x == y False Obviously that's the normal inaccuracy with floating point arithmetic, but why are floating point values instances of Num or perhaps why is there an instance instance Num a => Semigroup (Sum a) if associativity doesn't hold? Are there any other areas

Keeping IO lazy under append

穿精又带淫゛_ 提交于 2019-12-10 15:27:39
问题 I may have been under the false impression that Haskell is lazier than it is, but I wonder if there's a way to get the best of both worlds... Data.Monoid and Data.Semigroup define two variations of First . The monoidal version models the leftmost, non-empty value, whereas the semigroup version simply models the leftmost value. This works fine for pure value values, but consider impure values: x = putStrLn "x" >> return 42 y = putStrLn "y" >> return 1337 Both of these values have the type Num

Can you formulate the Bubble sort as a monoid or semigroup?

大城市里の小女人 提交于 2019-12-01 05:58:32
Given the following pseudocode for the bubble-sort procedure bubbleSort( A : list of sortable items ) repeat swapped = false for i = 1 to length(A) - 1 inclusive do: /* if this pair is out of order */ if A[i-1] > A[i] then /* swap them and remember something changed */ swap( A[i-1], A[i] ) swapped = true end if end for until not swapped end procedure Here is the code for Bubble Sort as Scala def bubbleSort[T](arr: Array[T])(implicit o: Ordering[T]) { import o._ val consecutiveIndices = (arr.indices, arr.indices drop 1).zipped var hasChanged = true do { hasChanged = false consecutiveIndices