以下代码有点神秘.在非玩具版本的问题中,我试图在monad Result中进行monadic计算,其值只能在IO中构造.似乎IO背后的魔力使这样的计算严格,但我无法弄清楚究竟是怎么发生的.
代码:
data Result a = Result a | Failure deriving (Show)
instance Functor Result where
fmap f (Result a) = Result (f a)
fmap f Failure = Failure
instance Applicative Result where
pure = return
(<*>) = ap
instance Monad Result where
return = Result
Result a >>= f = f a
Failure >>= _ = Failure
compute :: Int -> Result Int
compute 3 = Failure
compute x = traceShow x $ Result x
compute2 :: Monad …Run Code Online (Sandbox Code Playgroud) 我想创建一个数据结构来存储使用Symbol标记在类型级别的项目.这个:
data Store e (ss :: [Symbol]) where
Nil :: Store e '[]
Cons :: e s -> Store e ss -> Store e (s ': ss)
data HasElem (a :: k) (as :: [k]) where
AtHead :: HasElem a (a ': as)
InTail :: HasElem a as -> HasElem a (b ': as)
class HasElemC (a :: k) (as :: [k]) where hasElem :: HasElem a as
instance HasElemC {OVERLAPPING} a (a ': as) where hasElem = AtHead
instance …Run Code Online (Sandbox Code Playgroud) 我经常编写正在剥离新类型的唯一构造函数的函数,例如在以下函数中返回第一个不是Nothing的参数:
process (Pick xs) = (\(First x) -> x) . mconcat . map (First . process) $ xs
Run Code Online (Sandbox Code Playgroud)
我认为lambda是不必要的冗长.我想写这样的东西:
process (Pick xs) = -First . mconcat . map (First . process) $ xs
Run Code Online (Sandbox Code Playgroud)
Haskell的元编程工具是否允许类似的东西?以更简洁的方式解决这个问题的任何其他解决方案也是受欢迎的.
UPD.已经要求整个代码:
data Node where
Join :: [Node] -> Node
Pick :: [Node] -> Node
Given :: Maybe String -> Node
Name :: String -> Node
process :: Node -> Maybe String
process (Join xs) = liftM os_path_join (mapM process xs)
process (Pick xs) = getFirst . …Run Code Online (Sandbox Code Playgroud) 假设我想在没有外部工具(如LiquidHaskell)的帮助下构建满足某些不变量的子类型(理想情况下,即使没有类型类,我也希望这样做).最优雅的方式是什么?到目前为止我尝试了以下内容:
class Validated a where
type Underlying a
validate :: Underlying a -> Bool
construct :: Underlying a -> a
use :: a -> Underlying a
makeValidated :: Validated a => Underlying a -> Maybe a
makeValidated u = if validate u
then Just (construct u)
else Nothing
newtype Name = Name String
instance Validated Name where
type Underlying Name = String
validate str = and [ isUppercase (str !! 0 )
, all isLetter str ]
construct = Name …Run Code Online (Sandbox Code Playgroud) data T t where
A :: Show (t a) => t a -> T t
B :: Coercible Int (t a) => t a -> T t
f :: T t -> String
f (A t) = show t
g :: T t -> Int
g (B t) = coerce t
Run Code Online (Sandbox Code Playgroud)
为什么f编译但g生成如下错误?我正在使用GHC 8.4.
• Couldn't match representation of type ‘Int’ with that of ‘t a’
Inaccessible code in
a pattern with constructor:
B :: forall k …Run Code Online (Sandbox Code Playgroud) 说,我有
struct Foo
{
char a;
char b;
};
void bar(Foo foo);
Run Code Online (Sandbox Code Playgroud)
初始化结构并将其传递给函数的最简洁方法是什么?理想情况下,我想写一些类似的东西
bar(Foo = {'a','b'});
Run Code Online (Sandbox Code Playgroud)
如果 Foo 是一个工会呢?
UPD:我真诚地道歉,这个问题应该只与 C++03 相关。此外,在这种特殊情况下,应避免远离 POD(该代码适用于嵌入式系统,因此需要更短的字节码)。vonbrand,感谢 C++11 的回答。
haskell ×5
anonymous ×1
c++ ×1
coercion ×1
constructor ×1
gadt ×1
io-monad ×1
newtype ×1
strictness ×1
strip ×1
struct ×1
traversal ×1
validation ×1