给定一个操作(??),使得
(a ?? b) ?? c = a ?? (b ?? c)
Run Code Online (Sandbox Code Playgroud)
(也就是说(??)是联想的)
一定是这样
liftA2 (??) (liftA2 (??) a b) c = liftA2 (??) a (liftA2 (??) b c)
Run Code Online (Sandbox Code Playgroud)
(也就是说liftA2 (??)是联想的)
如果我们愿意,我们可以将其重写为:
fmap (??) (fmap (??) a <*> b) <*> c = fmap (??) a <*> (fmap (??) b <*> c)
Run Code Online (Sandbox Code Playgroud)
我花了一点时间盯着适用的法律,但我无法拿出证据证明情况确实如此。所以我开始反驳它。我尝试过的所有开箱即用的应用程序(Maybe、[]、Either等)都遵循法律,所以我想我会创建自己的应用程序。
我最好的想法是制作一个空的应用程序,并附加一条额外的信息。
data Vacuous a = Vac Alg
Run Code Online (Sandbox Code Playgroud)
Alg我稍后会在自己方便的时候定义哪些代数可以使财产失败但适用的法律成功。
现在我们这样定义我们的实例:
instance Functor Vacuous where
fmap f …Run Code Online (Sandbox Code Playgroud) 一组2个元素的二进制运算数是2^(2*2)=16.

该集合上的关联二进制操作数仅为8.

一组3个元素的二进制运算数为3 ^(3*3)= 19683.
该集合上的关联二进制操作数仅为113.如何知道一组n个元素上有多少个关联二元运算?
另外,为了获得所有这113个操作并写入文件,有必要编写程序.
如果我将尝试获取所有19683操作,然后检查它的所有19683操作的关联属性"a*(b c)==(a b)*c",这将起作用但是这需要很长时间才能完成= 4个元素!
如何编写一个有效的算法来解决这个任务?
请帮我!
我正在尝试在运算符和运算符上为我的自定义数据类型创建一个Semigroup和VerifiedSemigroup实例:Bool&&||
%case data Lógico = Cierto | Falso
(&&) : Lógico -> Lógico -> Lógico
(&&) Cierto Cierto = Cierto
(&&) _ _ = Falso
(||) : Lógico -> Lógico -> Lógico
(||) Falso Falso = Falso
(||) _ _ = Cierto
Run Code Online (Sandbox Code Playgroud)
所以我先制作一个命名实例为Semigroup在 &&运营商:
-- Todos
instance [TodosSemigroup] Semigroup Lógico where
(<+>) a b = a && b
Run Code Online (Sandbox Code Playgroud)
但在制作VerifiedSemigroup实例时,如何告诉Idris使用TodosSemigroup实例Lógico? …
formal-verification named-instance typeclass idris semigroup
我是一个Haskell新手,我想知道为什么没有替代实例,Either但是半群,其行为与我期望的替代:
instance Semigroup (Either a b) where
Left _ <> b = b
a <> _ = a
Run Code Online (Sandbox Code Playgroud)
此实例丢弃或更正"错误",当两个操作数都被标记时Right,它将采用第一个.这不是替代品提供的"选择"吗?
我希望semigroup实例看起来大致如下:
instance (Semigroup b) => Semigroup (Either a b) where
Left e <> _ = Left e
_ <> Left e = Left e
Right x <> Right y = Right (x <> y)
Run Code Online (Sandbox Code Playgroud)
这意味着它传播错误并附加常规结果.
我想我有一个错误的概念Either或涉及的类型类.
这是基数排序的伪代码:
Pseudocode for Radix Sort:
Radix-Sort(A, d)
// Each key in A[1..n] is a d-digit integer. (Digits are
// numbered 1 to d from right to left.)
1. for i = 1 to d do
Use a stable sorting algorithm to sort A on digit i.
Run Code Online (Sandbox Code Playgroud)
这是基数排序的Scala代码:
object RadixSort {
val WARP_SIZE = 32
def main(args: Array[String]) = {
var A = Array(123,432,654,3123,654,2123,543,131,653,123)
radixSortUintHost(A, 4).foreach(i => println(i))
}
// LSB radix sort
def radixSortUintHost(A: Array[Int], bits: Int): Array[Int] = { …Run Code Online (Sandbox Code Playgroud) 该base哈斯克尔库具有以下类型同义词在Data.Semigroup:
type ArgMin a b = Min (Arg a b)
type ArgMax a b = Max (Arg a b)
Run Code Online (Sandbox Code Playgroud)
这两种类型的同义词的目的是什么?在哪里可以有效地使用它们?
解释 argmin 和 argmax 函数在数学中的作用以及它们与这些类型同义词的关系可能会有所帮助。
这里有一些额外的信息,因此您不必跳到 Hackage。
这是 的定义Arg:
-- | 'Arg' isn't itself a 'Semigroup' in its own right, but it can be
-- placed inside 'Min' and 'Max' to compute an arg min or arg max.
data Arg a b = Arg a b
Run Code Online (Sandbox Code Playgroud)
它的文档字符串表明ArgMinandArgMax …
Maybe表示可能由于错误而不会产生结果的计算.因此,这种计算必须短路.
现在Maybe的Semigroup/Monoid实例似乎打破了这种语义,因为前者偏向于Just,后者将错误情况Nothing视为空元素:
Just "foo" <> Nothing -- Just "foo"
Nothing <> Just "bar" -- Just "bar"
Just "foo" <> Just "bar" -- Just "foobar"
Nothing <> Nothing -- Nothing
Run Code Online (Sandbox Code Playgroud)
我期望Nothing前两个案例.
这是替代实现(希望它是正确/合法的):
instance Semigroup a => Semigroup (Maybe a) where
Nothing <> _ = Nothing
_ <> Nothing = Nothing
Just a <> Just b = Just (a <> b)
instance Monoid a => Monoid (Maybe a) where
mempty = Just mempty …Run Code Online (Sandbox Code Playgroud) 基础中是否有任何新类型基本上可以实现以下目标?
newtype SemigroupFlip a = SemigroupFlip a
instance Semigroup a => Semigroup (SemigroupFlip a) where
(SemigroupFlip a) <> (SemigroupFlip b) = SemigroupFlip (b <> a)
instance Monoid a => Monoid (SemigroupFlip a) where
mempty = SemigroupFlip mempty
Run Code Online (Sandbox Code Playgroud) 给出以下用于冒泡排序的伪代码
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
Run Code Online (Sandbox Code Playgroud)
这是Bubble Sort as Scala的代码
def bubbleSort[T](arr: Array[T])(implicit o: Ordering[T]) {
import o._
val consecutiveIndices = (arr.indices, arr.indices drop …Run Code Online (Sandbox Code Playgroud) 该程序可以按制造或按年份对列表进行排序。什么是 (<>)?
import Data.Semigroup ((<>))
compare = comparing year <> comparing mfg
.
.
.
Run Code Online (Sandbox Code Playgroud)