小编Sav*_*xey的帖子

Control.Arrow:为什么"让(a,b)=(第一,第二)"失败?

我想要的是写这样的东西:

let (a,b) = if *condition* then (first, second) else (second, first)

我发现即使是这样我也写不出来:

let (a,b) = (first,second)

它失败并出现错误:

 <interactive>:7:5:                                                                                                                          
Could not deduce (Arrow a0)                                                                                                             
from the context (Arrow a)
  bound by the inferred type for `a':
             Arrow a => a b c -> a (b, d) (c, d)
  at <interactive>:7:5-26
The type variable `a0' is ambiguous
When checking that `a' has the inferred type
  a :: forall (a :: * -> * -> *) b c d.
       Arrow a => …
Run Code Online (Sandbox Code Playgroud)

haskell functional-programming arrows

8
推荐指数
1
解决办法
178
查看次数

来自多个班级的朋友模板功能

我有这个代码:

template<typename T> T f() {
// ...
}

class A {
    friend A f();
};

class B {
    friend B f();
};
Run Code Online (Sandbox Code Playgroud)

我收到ambiguating new declaration of ‘B f()’错误.

但是,如果我将我的代码更改为以下

template<typename T> void f(T arg) {
// ...
}

class A {
    friend void f(A);
};

class B {
    friend void f(B);
};
Run Code Online (Sandbox Code Playgroud)

程序编译精细.

有人可以帮我弄清问题是什么?

c++ oop function friend friend-function

4
推荐指数
1
解决办法
315
查看次数

F#((*)2)和((<<<)1)的行为不同

我有这两段代码:

  • [| 0 .. N-1 |] |> Array.map((<<<)1)

  • [| 0 .. N-1 |] |> Array.map((*)2)

我认为他们做的事情完全相同,但事实并非如此.在第一种情况下,我得到1, 2, 4并在第二 - 0, 2, 4.我不明白为什么有1第一种情况?如果我写let a = 0 <<< 1,我明白0.这是一个错误吗?谢谢!

mapping f# functional-programming bitwise-operators

3
推荐指数
1
解决办法
82
查看次数

F#:为什么Array.createZero这么快?

我有这个代码:

let timer = new System.Diagnostics.Stopwatch()
timer.Start()
Array.zeroCreate<int> 100000000

timer.Stop()
printfn "%ims" timer.ElapsedMilliseconds

timer.Reset()
timer.Start()
Array.create 100000000 0

timer.Stop()
printfn "%ims" timer.ElapsedMilliseconds
Run Code Online (Sandbox Code Playgroud)

我测试了它并得到了这些结果:

0ms
200ms
Run Code Online (Sandbox Code Playgroud)

如何Array.zeroCreate快速创建数组并确保其所有元素都具有默认值?在其他语言中,我知道没有这样的可能性(据我所知).在其他语言中,我只知道数组的快速初始化,哪些元素不能保证具有默认值,因为它们可以在存储器中初始化,其中存在一些垃圾.

谢谢!

arrays f# initialization array-initialization

3
推荐指数
1
解决办法
353
查看次数

计算使用相同参数调用许多函数的结果的最大乘积

我有这个代码:

f1 =: some function that returns list of numbers
f2 =: some function that returns list of numbers
f3 =: some function that returns list of numbers
f4 =: some function that returns list of numbers
max_mult_all =: (*/f1) >. (*/f2) >. (*/f3) >. (*/f4)
Run Code Online (Sandbox Code Playgroud)

有没有更好的初始化方法max_mult_all?如果我有数百个这样的功能怎么办?是否可以使用Insert(/)样式?

谢谢!

functional-programming function j

3
推荐指数
1
解决办法
79
查看次数

结合(a - > Maybe a)和(a - > a)函数

我有两个功能:

f :: a -> Maybe a
g :: a -> a
Run Code Online (Sandbox Code Playgroud)

我想创建这样的功能:

h :: a -> Maybe a

h x
| isJust(f x) = Just (g $ fromJust(f x))
| otherwise   = Nothing
Run Code Online (Sandbox Code Playgroud)

我怎样才能以更优雅的方式做到这一点?

haskell functional-programming function dot-operator

3
推荐指数
2
解决办法
138
查看次数

将IO [a - > b]应用于类型a的单个参数

我有

functions :: IO[a -> b]   -- IO List of functions
param :: IO[a]            -- Single parameter
Run Code Online (Sandbox Code Playgroud)

而且我想得到

result :: IO[b]
result = IO[*first function* <$> param, *second function* <$> param, ...]
Run Code Online (Sandbox Code Playgroud)

我不知何故可以获得类型的结果IO[IO b],但我想要IO[b]

haskell functional-programming function

1
推荐指数
1
解决办法
78
查看次数

Scala:为什么这个函数不是尾递归?

我有Merge Sort的这种实现:

import scala.annotation.tailrec

object MergeSort {
  def sortBy[T]: ((T, T) => Int) => Seq[T] => Seq[T] = comparator => seqToSort => {
    @tailrec
    def merge(xs : Seq[T], ys : Seq[T], accum : Seq[T] = Seq()) : Seq[T] = (xs, ys) match {
      case (Seq(), _) => ys ++ accum
      case (_, Seq()) => xs ++ accum
      case (x::rx, y::ry) =>
        if(comparator(x, y) < 0)
          merge(xs, ry, y +: accum)
        else
          merge(rx, ys, x +: accum)
    }

    @tailrec
    // Problem …
Run Code Online (Sandbox Code Playgroud)

recursion functional-programming scala tail-recursion purely-functional

0
推荐指数
1
解决办法
130
查看次数