我正在创建一个库,我想在这里定义一个递归类,我在这里简化为:
{-# LANGUAGE MultiParamTypeClasses
, FlexibleInstances #-}
data Snoc st b c = Snoc (st b) (c -> b)
data Top a = Top
class StackTo a st c where
runStack :: st c -> (c -> a)
instance StackTo a Top a where
runStack _ = id
instance (StackTo a st b) => StackTo a (Snoc st b) c where
runStack (Snoc st' cb) = runStack st' . cb
Run Code Online (Sandbox Code Playgroud)
这让我这样做,例如
*Main Data.Label> let f = runStack $ Snoc (Snoc …Run Code Online (Sandbox Code Playgroud) Prelude> let filter' p (x:xs) | p x = x : filter' p xs | otherwise = filter' p xs
Prelude> let filter' _ [] = []
Prelude> filter' odd [1..10]
*** Exception: <interactive>:1:5-21: Non-exhaustive patterns in function filter'
Run Code Online (Sandbox Code Playgroud)
我错过了什么模式?
Prelude> :{
Prelude| let filter' p (x:xs)
Prelude| | p x = x : filter' p xs
Prelude| | otherwise = filter' p xs
Prelude| let filter' _ [] = []
Prelude| :}
<interactive>:2:5: parse error (possibly incorrect indentation)
Run Code Online (Sandbox Code Playgroud)
在ghci中定义这个(语法明智)的惯用方法是什么?排队的是什么 …
所以我试图准确理解Haskell do符号是如何工作的.据我所知,它与monad一起使用,它基本上扩展(因为它实际上是语法糖)到与bind(>>=)或then(>>)连接的匿名函数,如下所示https://en.wikibooks.org/wiki/Haskell/Syntactic_sugar #Do_notation.
但是我的问题是为什么以下命令
Prelude> do [1, 2, 3]; "hello"
Run Code Online (Sandbox Code Playgroud)
回报
"hellohellohello"
Run Code Online (Sandbox Code Playgroud)
我知道数组实际上是monad(并且这些字符串是字符数组)但是我没有看到它如何导致上面的行为.
我正在使用流媒体库,但会接受使用管道或管道的答案.
说我有
import Streaming (Stream, Of)
import qualified Streaming.Prelude as S
streamChunks :: Int -> Stream (Of Thing) IO ()
streamChunks lastID = do
flip fix 0 $ \go thingID ->
unless (thingID > lastID) $ do
thing <- highLatencyGet thingID
S.yield thing
go (thingID+1)
Run Code Online (Sandbox Code Playgroud)
为了减少延迟,我想在处理消费者中的前highLatencyGet一个Thing时并行分叉以检索下一个Thing.
显然,我可以MVar在调用之前将我的函数转换为创建新的并分叉下一批yield,等等.
但我想知道是否有一种惯用(可组合)的方法来做到这一点,这样它就可以打包在一个库中,并且可以在任意IO上使用Stream.理想情况下,我们也可以配置预取值,例如:
prefetching :: Int -> Stream (Of a) IO () -> Stream (Of a) IO ()
Run Code Online (Sandbox Code Playgroud) 请允许我首先分享我目前所拥有的:
main :: IO ()
main = do contents <- readFile "filmList.txt"
let database = (read contents :: [Film])
putStr "Please enter your username: "
userName <- getLine
menu database
where menu newDb = do putStrLn "\nPlease select an option:"
putStrLn "1: Display all films currently in the database"
putStrLn "2: Add a new film to the database (and display all films)"
putStrLn "3: "
putStrLn "4: Save Database"
putStrLn "5: Exit"
putStr "\nSelected option: "
option <- getLine
case …Run Code Online (Sandbox Code Playgroud) 我有一个脚本需要使用新的LaunchConfiguration更新一个名为AutoScalingGroup的新的刚刚创建的AMI.不幸的是文档不好,我厌倦了反复试验.这是我到目前为止:
build_autoscale_name = "build_autoscaling"
build_autoscale_lc = LaunchConfiguration(
...launch config stuff...
, image_id=imid # new AMI
)
as_conn.create_launch_configuration(build_autoscale_lc)
ag = AutoScalingGroup(
group_name=build_autoscale_name
, launch_config=build_autoscale_lc
...other ASG stuff...
)
as_conn.create_auto_scaling_group(ag)
Run Code Online (Sandbox Code Playgroud)
失败的最新方式是:
通过此名称启动配置已存在
如果我发表评论,那么create_launch_configuration()我得到:
此名称的AutoScalingGroup已存在
我看到AutoScalingGroup有一个update方法; 我是否需要get_all_groups()使用具有相同名称的新LaunchConfiguration进行更新?或者,如果我LaunchConfiguration每次创建一个新命名(即我会遇到某种限制),这是否重要?
我需要抛出一个异常,其中包含一些多态类型的东西a供处理程序处理,比如
data MyException a = MyException a
deriving (Typeable)
Run Code Online (Sandbox Code Playgroud)
我最初有一个Exception实例的地方:
instance Show (MyException a) where
show _ = "MyException"
instance Exception (MyException a)
Run Code Online (Sandbox Code Playgroud)
但是这是一个类型错误
No instance for (Typeable a)
arising from the superclasses of an instance declaration
In the instance declaration for
‘Exception (MyException a)’
Run Code Online (Sandbox Code Playgroud)
我需要做a一个Dynamic吗?这似乎是错的,因为我们已经在处理程序中做了那种强制性的东西.这样做的惯用方法是什么?
我在GHC 7.8上
我正在学习haskell然后我来参加这个练习,我必须定义一个能够得到数字列表产品的函数.我被提供了选择,因为我是haskell的新手,有一些我不太清楚的符号.
所以我在其中一个选择中看到了这个定义:
p [x, xs] = x * product xs
Run Code Online (Sandbox Code Playgroud)
我可以理解这一点,它意味着得到列表的乘积,然后乘以x的值.
然后我在另一个选择中看到了另一个定义:
p (x : xs) = x * product xs
Run Code Online (Sandbox Code Playgroud)
我完全不明白.它使用括号和冒号,我很难找到它们的定义.如果有人可以通过语法和语义来启发我,我感激不尽.
我试图解决Haskell中的8皇后问题,而不使用任何高级功能,只有基本知识.我只走到这一步,但我得到一个我无法理解的错误.代码:
queens = [[x1,x2,x3,x4,x5,x6,x7,x8] | x1<-[1..8], x2<-[1..8],
x3<-[1..8], x4<-[1..8], x5<-[1..8],
x6<-[1..8], x7<-[1..8], x8<-[1..8],
safeH [x2,x3,x4,x5,x6,x7,x8] x1]
safeH xs e = if length xs == 1 then head xs
else e /= safeH (tail xs) (head xs)
Run Code Online (Sandbox Code Playgroud)
并且错误消息是:
y.hs:1:42:
No instance for (Num Bool) arising from the literal `1'
Possible fix: add an instance declaration for (Num Bool)
In the expression: 1
In the expression: [1 .. 8]
In a stmt of a list comprehension: x1 <- [1 .. 8]
[1 …Run Code Online (Sandbox Code Playgroud) haskell ×8
amazon-ec2 ×1
autoscaling ×1
boto ×1
concurrency ×1
conduit ×1
do-notation ×1
exception ×1
list ×1
monads ×1
stream ×1
syntax ×1
typeclass ×1