在Data.ByteString.Internal中,ByteString具有构造函数
PS !!(ForeignPtr Word8) !!Int !!Int
Run Code Online (Sandbox Code Playgroud)
这些双重惊叹在这里意味着什么?我搜索并得到(!!)可用于索引列表(!!) :: [a] -> Int -> a.
该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) 如果联合类型的成员共享一个属性,并且该属性的类型可用于区分这些成员,那么我应该能够使用作为条件来缩小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
我使用 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 blas和withval=$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 …
在学习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) 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.
当前有两个带有堆栈和 cabal 文件的项目(我使用堆栈来构建),一个是名为 test 的 exe,另一个是名为 testlib 的库。我想在测试项目中使用 testlib,我该怎么做才能让堆栈知道 testlib 是一个自定义库以及如何找到它?
-- projects/test/test.yaml
-- projects/testlib/testlib.yaml
Run Code Online (Sandbox Code Playgroud) 我有三个顺序期货,并在这样的理解中使用
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) 我正在学习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.如果删除这一行(或者我错过了什么?)没有区别,那么,这条线的真正含义是什么?
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.这是怎么发生的?
我有一个定义如下的函数:
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 ×8
autoconf ×1
configure ×1
constructor ×1
future ×1
monads ×1
narrowing ×1
r ×1
reader-monad ×1
recursion ×1
scala ×1
state-monad ×1
typescript ×1
union-types ×1
yesod ×1