在这里,我又回来了(对我而言)我最新杰作的奇怪行为......
此代码应该读取文件,但它不会:
readCsvContents :: String -> IO ( String )
readCsvContents fileName = do
withFile fileName ReadMode (\handle -> do
contents <- hGetContents handle
return contents
)
main = do
contents <- readCsvContents "src\\EURUSD60.csv"
putStrLn ("Read " ++ show (length contents) ++ " Bytes input data.")
Run Code Online (Sandbox Code Playgroud)
结果是
Read 0 Bytes input data.
Run Code Online (Sandbox Code Playgroud)
现在我改变了第一个函数并添加了一个putStrLn:
readCsvContents :: String -> IO ( String )
readCsvContents fileName = do
withFile fileName ReadMode (\handle -> do
contents <- hGetContents handle
putStrLn ("hGetContents gave …Run Code Online (Sandbox Code Playgroud) 在从快照计算变更集时,Git并不总是正确选择块边界.git diff有一个--diff-algorithm选项允许在这方面进行一些调整; git diff --minimal有时比git diff单独给出更好的结果.
有没有办法获得相同的优化变更集布局git add -p(基本上以diff交互方式显示帅哥)?它似乎不允许这个--diff-algorithm选项.
在Haskell中的并行列表理解中,我在尝试使用防护时遇到了问题.
largestPalindrome :: Int -> Int
largestPalindrome x = maximum [ a*b
| a <- [x,x-1..1]
| b <- [x,x-1..1]
, isPalindrome (a*b) ]
Run Code Online (Sandbox Code Playgroud)
显示的错误是
Variable not in scope: a :: Int
Run Code Online (Sandbox Code Playgroud) 我想使用这样的派生实例:
data Test3D = forall a. (Show a, Eq a, Typeable a, Generic a)
=> Test3D { testDt :: String
, testPrm :: a
}
deriving (Show, Eq, Typeable, Generic)
instance Binary (Test3D)
$(deriveJSON defaultOptions ''Test3D)
Run Code Online (Sandbox Code Playgroud)
但是我从GHC收到了:
• Can't make a derived instance of ‘Show Test3D’:
Constructor ‘Test3D’ has existentials or constraints in its type
Possible fix: use a standalone deriving declaration instead
• In the data declaration for ‘Test3D’
Run Code Online (Sandbox Code Playgroud)
这种方式对我的项目非常方便.我找不到解决方案.
是否有任何方法可以将派生实例用于此类数据?
我是haskell的新手,我读了一些关于这个称为类型签名的东西,但有一些我不理解的东西.
这是我正在看的代码:
--mult applies product
mult :: Num a => [a] -> a
mult = foldr (*) 1
--posList filters positive numbers out
posList :: (Ord a, Num a) => [a] -> [a]
posList = filter (>0)
--trueList determines whether all of the members of a list are T or not
trueList :: [Bool] -> Bool
trueList = foldr(&&) True
--evenList determines where all of the members of a list are even or not
evenList :: (Integral a, Foldable t) …Run Code Online (Sandbox Code Playgroud) 我不确定我在这里做错了什么:
data Vector2D u = Vector2D {
_x :: u,
_y :: u
} deriving stock (Show, Eq, Functor, Foldable, Traversable)
{-# INLINE addVector2 #-}
addVector2 :: (Additive a) => Vector2D a -> Vector2D a -> Vector2D a
addVector2 (Vector2D { _x = x1, _y = y1 }) (Vector2D { _x = x2, _y = y2 }) =
Vector2D { _x = x1 + x2, _y = y1 + y2 }
instance (Additive a) => Additive (Vector2D a) where
(+) …Run Code Online (Sandbox Code Playgroud) 在Haskell中,当我们谈论类型声明时。
我见过->和=>。
例如:我可以进行自己的类型声明。
addMe :: Int -> Int -> Int
addMe x y = x + y
Run Code Online (Sandbox Code Playgroud)
而且效果很好。
但是,如果我们看一下,:t sqrt就会得到:
sqrt :: Floating a => a -> a
Run Code Online (Sandbox Code Playgroud)
我们在什么时候使用=>什么时候使用->?什么时候使用“胖箭头”,什么时候使用“瘦箭头”?
我直观地理解了 的纯粹性、紧缩性和嵌套性规律MonadFix。然而,我很难理解滑动定律。
mfix (fmap h . f) = fmap h (mfix (f . h)) -- for strict h\nRun Code Online (Sandbox Code Playgroud)\n我的第一个问题是,如果h必须严格那么就不会mfix (f . h)是底值,即\xe2\x8a\xa5?毕竟,f . h在返回之前不得检查其输入,这样就不会导致悖论。但是,如果h是严格的,那么它将必须检查其输入。也许我对严格函数的理解是错误的。
第二,为什么这部法律很重要?我能理解纯粹法则、紧缩法则和嵌套法则的意义。然而,我不明白为什么mfix遵守滑动定律很重要。您能否提供一个代码示例来说明为什么滑动定律对于 很重要MonadFix?
我对上下文中的价值有一个小问题.
拿Just 'a',所以Maybe在这种情况下类型的上下文中的值是'a'
拿[3],所以[a]在这种情况下类型的上下文中的值是3
如果你申请的单子对于[3]这样的:[3] >>= \x -> [x+3],这意味着你分配x与价值3.没关系.
但现在,拿[3,2],所以类型的上下文中的价值是什么[a]?它是如此奇怪,如果你像这样应用monad:
[3,4] >>= \x -> x+3
Run Code Online (Sandbox Code Playgroud)
它得到了正确的答案[6,7],但实际上我们不明白在这种情况下x是什么.您可以回答,啊x是3然后是4,x将函数提供2次并连接如下Monad:concat (map f xs)像这样:
[3,4] >>= concat (map f x)
Run Code Online (Sandbox Code Playgroud)
所以在这种情况下,[3,4]将分配给x.这意味着错误,因为[3,4]它不是一个价值.Monad是错的.
我不知道Haskell,我只想玩它来学习它.我试图理解io,monads等,并在解释器(GHCi,版本7.10.2,WinGHCI)中写了这个:
Prelude> [1,1] >> "ok"
"okok"
Prelude> [1,1,1] >> "ok"
"okokok"
Prelude> [1..10] >> "ok"
"okokokokokokokokokok"
Prelude> [1] >> "ok" >> [1] >> "ok"
"okok"
Prelude> [1,2] >> "ok" >> [1,2] >> "ok"
"okokokokokokokok"
Prelude> [1..10] >> [1..10]
[1,2,3,4,5,6,7,8,9,10,1,2,3,4,5,6,7,8,9,10,1,2,3,4,5,6,7,8,9,10,1,2,3,4,5,6,7,8,9,10,1,2,3,4,5,6,7,8,9,10,1,2,3,4,5,6,7,8,9,10,1,2,3,4,5,6,7,8,9,10,1,2,3,4,5,6,7,8,9,10,1,2,3,4,5,6,7,8,9,10,1,2,3,4,5,6,7,8,9,10]
Run Code Online (Sandbox Code Playgroud)
小心解释一下?为什么列表中的元素数量会影响写入"ok"的次数(或者在最后一种情况下,是否写入数组的次数)?>>运算符不应该这样做,是吗?