以下代码成功编译,但在使用 GHC 9.2.3 时收到警告-Wredundant-constraints:
{-# LANGUAGE UndecidableInstances, FlexibleInstances #-}\n\nclass Functor f => C f where c :: f Int\n\ninstance (Functor f, Applicative f) => C f where c = pure 42\nRun Code Online (Sandbox Code Playgroud)\n由此产生的警告:
\ntest.hs:5:10: warning: [-Wredundant-constraints]\n \xe2\x80\xa2 Redundant constraint: Functor f\n \xe2\x80\xa2 In the instance declaration for \xe2\x80\x98C f\xe2\x80\x99\n |\n5 | instance (Functor f, Applicative f) => C f where c = pure 42\n | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\nRun Code Online (Sandbox Code Playgroud)\n但是,如果我删除此约束,代码将不再进行类型检查:
\ntest.hs:5:10: error:\n \xe2\x80\xa2 Could not deduce (Functor …Run Code Online (Sandbox Code Playgroud) 有没有一种简单的方法可以将一个普通的 Haskell 文件 ( .lhs)转换为一个普通的 Haskell ( .hs) 源文件?
我认为可能有 GHC 选项,但 GHC 手册似乎没有太多关于文学程序或.lhs格式的信息。“识字”这个词甚至没有出现在索引中!
在文学编程的维基链接包含指向脚本“鸟”和“之间的转换\begin{code}.. \end{code}”的风格或转换.lhs到TeX的格式,但仅此而已。
我正在从BNFC开始在Haskell中编写解析器和类型检查器.类型检查器的主要功能实现如下:
typecheck :: Program -> Err ()
typecheck (PDefs ds) = do
env <- foldM (\env (DFun typ id args _ _) ->
updateFun env id (argTypes args,typ) ) (emptyEnv) (ds)
mapM_ (checkDef env) ds
where argTypes = map (\(ADecl _ typ _) -> typ)
Run Code Online (Sandbox Code Playgroud)
where PDefs,DFun和ADecl是在语言的抽象语法中定义的代数数据类型的构造函数,checkDef并且updateFun是函数.Program是语法的"起点".使用的monad是monad Err:
data Err a = Ok a | Bad String
deriving (Read, Show, Eq, Ord)
instance Monad Err where
return = …Run Code Online (Sandbox Code Playgroud) 在我的函数式编程课程中,讲师创建了一个函数reverse来获取一个列表并返回一个具有相同元素但顺序相反的列表:
reverse :: [a] -> [a]
reverse [] = []
reverse (x:xs) = reverse xs ++ [x]
Run Code Online (Sandbox Code Playgroud)
然而,在书中我从定义中了解到,他们创建了相同的函数,并在函数名称后添加了 ' :
reverse' :: [a] -> [a]
reverse' [] = []
reverse' (x:xs) = reverse' xs ++ [x]
Run Code Online (Sandbox Code Playgroud)
我想知道这两个函数的行为是否相同,或者 ' 是否改变了它们的行为?此外,是否存在一种表示法优于另一种表示法的情况?任何帮助,将不胜感激!
这是我尝试模拟用户然后创建互斥体的代码。未创建互斥体。我收到 ERROR_ACCESS_DENIED 错误。
void Impersonate()
{
DWORD logonType = LOGON32_LOGON_INTERACTIVE;
DWORD logonProvider = LOGON32_PROVIDER_DEFAULT;
HANDLE userToken;
HANDLE hMutex;
DWORD err;
LPSTR user = "zoom"; // the user I created myself on my machine.
// It has Administrator privileges, and my account,
// from which I start the app, is Admin too
LPSTR password = "zoom";
LPSTR domain = ".";
hMutex = NULL;
LogonUserA(user, domain, password, logonType, logonProvider,&userToken);
// just to make sure that mutexes are created fine before impersonation
hMutex …Run Code Online (Sandbox Code Playgroud) 导出堆栈溢出的定义是:
"在Haskell中,派生实例是一个与数据或newtype声明一起自动生成的实例声明.派生实例声明的主体是从关联类型的定义中语法派生的."
说实话,我真的不明白.
以下代码取自:链接
data BaseballPlayer = Pitcher
| Catcher
| Infielder
| Outfielder
deriving Show
barryBonds :: BaseballPlayer -> Bool
barryBonds Outfielder = True
barryInOf = print(barryBonds Outfielder)
Run Code Online (Sandbox Code Playgroud)
我的问题是,派生语句在这个特定情况下做了什么,以及派生语句一般做了什么?
这个问题并不重复: 如何在haskell OBS 中推导出工作:我不是在询问它的源代码是如何工作的,我在问它是做什么的.
我正在尝试在 Haskell 中创建自己的自定义数据类型。
我有以下数据类型:
type Length = Integer
type Rotation = Integer
data Colour = Colour { red, green, blue, alpha :: Int }
deriving (Show, Eq)
Run Code Online (Sandbox Code Playgroud)
我正在尝试创建一个自定义数据类型,它可以是上述数据类型之一。我有以下几点:
data Special
= L Length
| R Rotation
| Col Colour
deriving (Show, Eq)
Run Code Online (Sandbox Code Playgroud)
但是,如果我有数据类型的实例,我希望能够提取Length,Rotation和Colour值Special。
如果我有:
L length
Run Code Online (Sandbox Code Playgroud)
请问length这里是类型Special或类型的Length?如果length是类型Special有没有办法提取它所以它是类型Length?
例如,下面的代码有效吗?
takeL (x:xs)
| x == (L length) = length
Run Code Online (Sandbox Code Playgroud)
任何见解表示赞赏。
haskell functional-programming pattern-matching algebraic-data-types custom-data-type
我在理解这段代码时缺少什么关键的直觉?我不明白这些是如何组成的。首先需要一个函数(a->c)和构造类型(fab)。然而,第二次产量(时尚)。此外,第一和第二是根据看起来循环的双图来定义的。
class Bifunctor f where
bimap :: (a -> c) -> (b -> d) -> f a b -> f c d
bimap g h = first g . second h
first :: (a -> c) -> f a b -> f c b
first g = bimap g id
second :: (b -> d) -> f a b -> f a d
second = bimap id
Run Code Online (Sandbox Code Playgroud) 我找到了用Haskell写的逻辑门异或的代码,但我不知道这个“ (/=)”是什么意思!
xor :: Bool -> Bool -> Bool
xor = (/=)
Run Code Online (Sandbox Code Playgroud) I am a beginner to Haskell and is now trying to develop a postfix calculator. Below is my code so far:
calcK :: String -> String
calcK = show calc
calc :: String -> [Double]
calc = foldl interprete [] . words
interprete s x
| x `elem` ["+","-","*","/","^"] = operate x s
| x `elem` ["inc","dec","sqrt","sin","cos","inv"] = operate2 x s
| x `elem` ["+all"] = [sum s]
| x `elem` ["*all"] = [product s]
| otherwise = read x:s …Run Code Online (Sandbox Code Playgroud)