小编hli*_*liu的帖子

双重惊叹的用法是什么?

在Data.ByteString.Internal中,ByteString具有构造函数

PS !!(ForeignPtr Word8) !!Int !!Int 
Run Code Online (Sandbox Code Playgroud)

这些双重惊叹在这里意味着什么?我搜索并得到(!!)可用于索引列表(!!) :: [a] -> Int -> a.

haskell

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

为什么延迟模式匹配splitAt函数的版本更快?

splitAt功能可以实现如下(https://wiki.haskell.org/Lazy_pattern_match):

import Prelude hiding (splitAt)
splitAt :: Int -> [a] -> ([a], [a])
splitAt n xs =
  if n<=0
     then ([], xs)
     else
        case xs of
          [] -> ([], [])
          y:ys ->
            case splitAt (n-1) ys of
              ~(prefix, suffix) -> (y : prefix, suffix) -- Here using lazy pattern match

main :: IO ()
main = do
  let r = splitAt 1000000 $ repeat 'a'
  print $ length $ fst r
Run Code Online (Sandbox Code Playgroud)

使用严格的模式匹配可以非常慢地减慢速度.

time ./lazy     -- 0.04s …
Run Code Online (Sandbox Code Playgroud)

recursion haskell pattern-matching

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

当属性的类型是判别式时,为什么“typeof”不缩小联合类型?

如果联合类型的成员共享一个属性,并且该属性的类型可用于区分这些成员,那么我应该能够使用作为条件来缩小if子句中的类型范围typeof。但这不起作用。

例如,在if下面的子句中, 的类型event应推断为UserTextEvent, 的类型event.target应推断为HTMLInputElement

type UserTextEvent = { value: string, target: HTMLInputElement };
type UserMouseEvent = { value: [number, number], target: HTMLElement };

type UserEvent = UserTextEvent | UserMouseEvent 


function handle(event: UserEvent) {
  if (typeof event.value === 'string') {
    event.value  // string, as expected

    event.target // should be narrowed to HTMLInputElement, but
                 // is still HTMLInputElement | HTMLElement. Why?
  }
}
Run Code Online (Sandbox Code Playgroud)

type-inference discriminated-union narrowing typescript union-types

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

如何使用autoconf重新生成配置文件?

我使用 autoconf 重新生成配置文件,它有效。但是当我执行生成的配置文件时./configure,出现一些错误消息,例如

./configure: line 3713: syntax error near unexpected token `blas'
./configure: line 3713: `  withval=$with_blas; R_ARG_USE(blas)'
Run Code Online (Sandbox Code Playgroud)

我用谷歌搜索发现这blas是一个库,但安装后仍然给出错误消息。我的 Mac 上安装了版本为“”的 autoconf autoconf (GNU Autoconf) 2.69,我尝试编译的是 R 源代码https://svn.r-project.org/R/


我已运行 autoconf -f 和 autoreconf -f 来尝试重新生成已成功生成的配置文件。但是,当我运行时,./configure错误再次发生。

错误消息显示syntax error near unexpected token blaswithval=$with_blas; R_ARG_USE(blas)。我认为问题可能是未知的功能R_ARG_USE。我R_ARG_USE在代码库中 grep 并发现它在文件 m4/R.m4 中定义:

AC_DEFUN([R_ARG_USE],
[if test "${withval}" = no; then
  use_$1=no
else
  use_$1=yes
fi
])# R_ARG_USE
Run Code Online (Sandbox Code Playgroud)

这是否意味着当我运行 autoconf 或 autoreconf …

autoconf r configure

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

为什么要将Reader的构造函数参数定义为函数?

在学习Reader Monad时,我发现它被定义为:

newtype Reader r a = Reader { runReader :: r -> a }

instance Monad (Reader r) where
  return a = Reader $ \_ -> a
  m >>= k  = Reader $ \r -> runReader (k (runReader m r)) r
Run Code Online (Sandbox Code Playgroud)

我想知道为什么使用函数作为构造函数参数而不是其他东西,如元组:

newtype Reader r a = Reader { runReader :: (r, a) }

instance Monad (Reader r) where
  -- Here I cannot get r when defining return function, 
  -- so does that's the reason that must using a …
Run Code Online (Sandbox Code Playgroud)

haskell reader-monad

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

如何在静态目录下使用图像作为Yesodweb中的背景?

Yesod的新手,我想知道如何使用目录static/img下的图像作为div的背景图像.我使用Scaffolding网站并试过:

.mydiv {
  background-image: url("static/img/bg.jpg");
}
Run Code Online (Sandbox Code Playgroud)

但似乎网址终于改变了http://myhost/tmp/static/img/bg.jpg.

haskell yesod

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

如何链接到本地​​ Haskell 库?

当前有两个带有堆栈和 cabal 文件的项目(我使用堆栈来构建),一个是名为 test 的 exe,另一个是名为 testlib 的库。我想在测试项目中使用 testlib,我该怎么做才能让堆栈知道 testlib 是一个自定义库以及如何找到它?

-- projects/test/test.yaml
-- projects/testlib/testlib.yaml
Run Code Online (Sandbox Code Playgroud)

haskell haskell-stack

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

Scala未来的失败是为了理解

我有三个顺序期货,并在这样的理解中使用

val comF = for {
  f1 <- future1
  f2 <- future2
  f3 <- future3
} yield {
  // something
}

comF onSuccess { }
comF onFailure { 
  // ----------------      Here is the problem  --------------------------------
  //
  // How do I know which future failed(throw exception), when the callback comes here ?
  // Thanks for the help! Different futures using different exceptions can solve it.
}
Run Code Online (Sandbox Code Playgroud)

现在我有一个像List [Future [T]]这样的未来列表,首先我使用这种方法将它转移到Future [List [T]](为什么这个未来的列表转换为未来的列表转换编译和工作?).然后我得到了未来

val fList: Future[List[T]]
fList on Failure {
  // …
Run Code Online (Sandbox Code Playgroud)

scala future

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

被困在Haskell Wiki的State Monad的例子中

我正在学习State Monad并且无法理解Wiki中的一个例子(http://en.wikibooks.org/wiki/Haskell/Understanding_monads/State)

rollDie :: GeneratorState Int
rollDie = do generator <- get
             let (value, newGenerator) = randomR (1,6) generator
             put newGenerator
             return value
Run Code Online (Sandbox Code Playgroud)

put有定义

put newState = State $ \_ -> ((), newState)
Run Code Online (Sandbox Code Playgroud)

它似乎put只是创造了一个新的State,这条线的实际用途是什么?如果要使用该值可能应该使用<-提取,而如果state要再使用则应该使用get.如果删除这一行(或者我错过了什么?)没有区别,那么,这条线的真正含义是什么?

monads haskell state-monad

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

如何在Haskell中创建具有空构造函数的数据类型的值?

Network包中的PortNumber 根本没有构造函数,其定义和doc如下(或请参考https://hackage.haskell.org/package/network-2.6.2.1/docs/Network.html):

data PortNumber

Use the Num instance (i.e. use a literal) to create a PortNumber value with
the correct network-byte-ordering. You should not use the PortNum constructor.
It will be removed in the next release.

instances
...
Num PortNumber
...
Run Code Online (Sandbox Code Playgroud)

令我困惑的是它如何使用Num实例创建一个PortNumber?我知道PortNumber是类Num的一个实例,它可以被认为是一个Num,但它如何才能看到Num实例(例如文字10000)作为PortNumber?在构造具有构造函数的PortID时PortNumber PortNumber,似乎可以使用PortNumber 10000.这是怎么发生的?

constructor haskell

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

如何删除琐碎的参数?

我有一个定义如下的函数:

errorWhenNothing :: Maybe a -> M (Maybe a) -- M is a Monad
errorWhenNothing Nothing = throwError "is Nothing!"
errorWhenNothing m = return m
Run Code Online (Sandbox Code Playgroud)

该参数m似乎微不足道,删除它使功能更简单,更紧凑.问题是我无法重写第二个定义

errorWhenNothing = return
Run Code Online (Sandbox Code Playgroud)

GHC抱怨 Equations for 'errorWhenNothing' have different numbers of arguments..

我想知道有没有办法删除m

haskell

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