我有一个类型类Foo与关联类型:
{-# LANGUAGE TypeFamilies #-}
class Foo a where
type Bar a
foo :: a -> Bar a
Run Code Online (Sandbox Code Playgroud)
现在我想定义一个包含其中一个关联类型的数据类型,并Show为其派生一个实例:
data Baz a = Baz (Bar a) deriving (Show)
Run Code Online (Sandbox Code Playgroud)
但是,这不会编译,因为您无法保证有Show实例Bar a
No instance for (Show (Bar a))
arising from the 'deriving' clause of a data type declaration
Run Code Online (Sandbox Code Playgroud)
我可以修复通过打开的问题FlexibleContexts,并UndecidableInstances和写作手册Show为例说明如下
{-# LANGUAGE FlexibleContexts, UndecidableInstances #-}
data Baz a = Bar a
instance (Show a, Show (Bar a)) => Show (Baz …Run Code Online (Sandbox Code Playgroud) 在之前的回答中,Petr Pudlak CFunctor为那些从Hask到Hask之外的算子定义了这个类.使用类型系列重写一下它看起来像
class CFunctor f where
type Dom f :: * -> * -> * -- domain category
type Cod f :: * -> * -> * -- codomain category
cmap :: Dom f a b -> Cod f (f a) (f b) -- map morphisms across categories
Run Code Online (Sandbox Code Playgroud)
例如,看起来像的例子
instance CFunctor Maybe where
type Dom Maybe = (->) -- domain is Hask
type Cod Maybe = (->) -- codomain is …Run Code Online (Sandbox Code Playgroud) 在解释器中工作时,将函数绑定到名称通常很方便,例如:
ghci> let f = (+1)
ghci> f 1
2
Run Code Online (Sandbox Code Playgroud)
这会将名称别名f为函数(+1).简单.
但是,这并不总是有效.我发现导致错误的一个例子是尝试nub从Data.List模块中进行别名.例如,
ghci> :m Data.List
ghci> nub [1,2,2,3,3,3]
[1,2,3]
ghci> let f = nub
ghci> f [1,2,2,3,3,3]
<interactive>:1:14:
No instance for (Num ())
arising from the literal `3'
Possible fix: add an instance declaration for (Num ())
In the expression: 3
In the first argument of `f', namely `[1, 2, 2, 3, ....]'
In the expression: f [1, 2, 2, 3, ....] …Run Code Online (Sandbox Code Playgroud) 根据位置,Haskell中的部分应用程序得到了正确的答案.
Prelude> (/2) 10
5.0
Prelude> (2/) 10
0.2
Prelude> (+3) 10
13
Prelude> (3+) 10
13
Run Code Online (Sandbox Code Playgroud)
但是,对于 - 运算符,我得到了一个错误,(-3)因为Haskell(似乎)将其解释为值-3而不是部分应用程序.
Prelude> (-3) 10
<interactive>:4:1:
Could not deduce (Num (a0 -> t))
arising from the ambiguity check for ‘it’
from the context (Num (a -> t), Num a)
bound by the inferred type for ‘it’: (Num (a -> t), Num a) => t
at <interactive>:4:1-7
The type variable ‘a0’ is ambiguous
When checking that ‘it’
has the …Run Code Online (Sandbox Code Playgroud) 应用程序可以以两种模式运行 - "实时",它查看对世界状态的每次更新,或"采样",其中它仅每T毫秒查看世界状态.
如果我正在编写Haskell(或任何使用ADT的语言),我会将其建模为
data Mode = RealTime | Sampled Int
Run Code Online (Sandbox Code Playgroud)
可以以类型安全的方式使用如下
case mode of
RealTime -> -- do realtime stuff
Sampled interval -> -- do sample stuff with 'interval'
Run Code Online (Sandbox Code Playgroud)
我说它是"类型安全的",因为如果您在实时模式下运行,则无法尝试访问该interval字段(如果您在采样模式下操作,则会在您需要时提供该字段).
如何以类型安全的方式在Java中对相同的东西进行建模?那就是我想要的
interval在实时模式下禁止访问该字段,以及这在Java中可行吗?如果没有,实现这种类型安全的惯用方法是什么?
我正在完成20个中级Haskell练习,这是一个非常有趣的练习.它涉及到执行类型类的各种实例Functor和Monad(而这需要职能FunctorS和MonadS作为参数),但像可爱的名字Furry和Misty掩饰我们正在做什么(一些有趣的代码使).
我一直试图以无点的方式做一些这样的事情,我想知道是否有一个将点 - 完全(?)定义转变为无点定义的一般方案.例如,以下是类型类Misty:
class Misty m where
unicorn :: a -> m a
banana :: (a -> m b) -> m a -> m b
Run Code Online (Sandbox Code Playgroud)
(函数unicorn和banana是,return并且>>=,如果不是很明显),这是我的apple(相当于flip ap)的实现:
apple :: (Misty m) => m a -> m (a -> b) -> m b
apple x f = banana (\g -> banana (unicorn …Run Code Online (Sandbox Code Playgroud) 我有一个具有以下结构的包(好吧,这是大大简化,但......)
app/
src/
Main.hs
data/
data.txt
app.cabal
Paths_app.hs
Setup.hs
Run Code Online (Sandbox Code Playgroud)
在Paths_app.hs我有:
module Paths_app where
getDataFileName :: FilePath -> IO FilePath
getDataFileName = return
Run Code Online (Sandbox Code Playgroud)
在Main.h中我有:
module Main where
import Paths_app
main = do
file <- getDataFileName "data/data.txt"
data <- readFile file
putStrLn $ "Your data is: " ++ data
Run Code Online (Sandbox Code Playgroud)
我的app.cabal文件的相关部分如下所示:
name: app
version: 1.0
build-type: Simple
data-files: data/data.txt
executable foo
build-depends: base, haskell98
main-is: Main.hs
hs-source-dirs: src
Run Code Online (Sandbox Code Playgroud)
这构建正常(使用cabal configure后跟cabal install)但可执行文件抱怨它找不到data.txt文件.我试过更换线路
file <- getDataFileName "data/data.txt"
Run Code Online (Sandbox Code Playgroud)
同
file <- getDataFileName …Run Code Online (Sandbox Code Playgroud) 我有一个自定义类型的数学向量
{-# LANGUAGE MultiParamTypeClasses, FlexibleInstances #-}
class Vector v a where
infixl 6 <+>
(<+>) :: v -> v -> v -- vector addition
infixl 6 <->
(<->) :: v -> v -> v -- vector subtraction
infixl 7 *>
(*>) :: a -> v -> v -- multiplication by a scalar
dot :: v -> v -> a -- inner product
Run Code Online (Sandbox Code Playgroud)
我想将数字a和函数a -> vector放入类的实例中.数字很简单:
instance Num a => Vector a a where
(<+>) = (+) …Run Code Online (Sandbox Code Playgroud) 以下程序创建两个并发运行的线程,每个线程在打印一行文本到stdout之前随机休眠一段时间.
import Control.Concurrent
import Control.Monad
import System.Random
randomDelay t = randomRIO (0, t) >>= threadDelay
printer str = forkIO . forever $ do
randomDelay 1000000 -- ?s
putStrLn str
main = do
printer "Hello"
printer "World"
return ()
Run Code Online (Sandbox Code Playgroud)
输出通常看起来像
>> main
Hello
World
World
Hello
WoHrelld
o
World
Hello
*Interrupted
>>
Run Code Online (Sandbox Code Playgroud)
你如何确保一次只有一个线程可以写入stdout?这似乎是的那种STM要善于东西,但所有STM交易必须有型STM a一些a,而且打印在屏幕上的动作有型IO a,而且似乎没有被嵌入的方式IO进入STM.
我有一个简单的MATLAB脚本,可以打印一些文本并在循环中显示数字
for i = 1:3
x = randn(100, 1);
fprintf('Mean = %.2f\n', mean(x));
fprintf('Std = %.2f\n', std(x));
figure;
plot(cumsum(x));
end
Run Code Online (Sandbox Code Playgroud)
我想使用该publish函数创建一个包含此脚本输出的HTML文件,文本和图形在循环中交错,即它们命令它们出现在输出中应该是
但是,输出当前按以下顺序显示
如何实现所需的输出?