您好,我已经完成了我的JSon类型,我正在尝试将其添加到文件中.我可以从前奏中做到这一点但是在使用IO Monad时我无法做到这一点.我得到以下内容error:
Main.hs:13:24: error:
* Couldn't match type `Char' with `[Char]'
Expected type: String
Actual type: Char
* In the second argument of `writeFile', namely `val'
In a stmt of a 'do' block: writeFile out val
In the expression:
do val <- renderJValue sample
writeFile out val
|
13 | writeFile out val
| ^^^
Run Code Online (Sandbox Code Playgroud)
主要
module Main where
import Jlib
import Put
import Data.Typeable
import System.Environment
out="data.txt"
main::IO()
main=do
val<-renderJValue sample
writeFile out val
Run Code Online (Sandbox Code Playgroud)
为什么这在IO Monad中不起作用,因为renderJValue …
拥有Haskell的经验绝对为零,我需要提出与该Python等效的代码:
from random import choice, sample
def random_subset():
return tuple(sample(('N', 'S', 'W', 'E'), choice((1, 2, 3, 4))))
def form_grid(n):
return [[random_subset() for _ in range(n)] for _ in range(n)]
form_grid(10)
Run Code Online (Sandbox Code Playgroud)
产生这样的事情:
N ESWN SNEW NSE EWSN E ENSW N NSWE WES
NE WNS SWEN EWN ENWS WEN WS W ENSW NW
WENS NWE SNEW ES E S ES SENW EW WEN
NSE NE WNE NEWS SNE W SWNE NSWE SNEW EN
S SNW WNES S WESN E ES N ESN …Run Code Online (Sandbox Code Playgroud) 我在中输入了一些代码ghci,类似于以下代码:
main = do { a <- getLine ; let b = "Hello " ++ a ; putStrLn b }
Run Code Online (Sandbox Code Playgroud)
但是,我收到此错误:
<interactive>:1:63: error: parse error on input `}'
Run Code Online (Sandbox Code Playgroud)
在Haskell / GHC的早期版本中,我记得这种方法工作得很好-甚至明确指出,在do块中,您不需要in关键字。但是,使它起作用的唯一方法似乎是:
main = do { a <- getLine ; let b = "Hello " ++ a in putStrLn b }
Run Code Online (Sandbox Code Playgroud)
不会产生此错误。
这个被删除了吗?如果是这样,我是否需要do在let in表达式中添加第二个块?
该代码的作用是什么?是someMap(的::Data.Map.Strict.Map)对象的副本是否由myMap引用或仅作为引用?我的意思是,在我阅读后可以someMap更改(通过另一个线程)readIORef吗?像C的易失性...可能吗?我希望它是复制/快照,因此任何更改都不会影响我someMap或...?
do
....
someMap <- readIORef myMap
....
Run Code Online (Sandbox Code Playgroud) 在Haskell中,我可以写:
token: Parser a -> Parser a
token p = do space
v <- p
space
return v
Run Code Online (Sandbox Code Playgroud)
在F#中,我走到了这一步:
let token = compose {
let! _ = space
let! v = parser
let! _ = space
return v
}
Run Code Online (Sandbox Code Playgroud)
换句话说,我必须引入这个未使用的let! _ =绑定来丢弃"空间"解析器(monad)的解析值,这是我不需要的.
如何在F#中避免这些无用的绑定?我尝试过使用do!,但是我收到一个错误(因为我的>>=函数不采用类型单位而是'a):
let (>>=) (p: Parser<'a>) (f: 'a -> Parser<'b>) : Parser<'b>
Run Code Online (Sandbox Code Playgroud)
这是我的构建器定义:
type ParserComposer() =
member x.Bind(p, f) = p >>= f
member x.Return(y) = ret y
member x.Zero() = failure
Run Code Online (Sandbox Code Playgroud)
我需要定义>>功能吗?将Combine()添加到构建器?任何想法如何做到这一点?代码示例?
我编写的代码块无法编译,因为 if/then/else 块没有以编译器理解的方式设置,但是我不知道如何重写它,所以它可以。
playRandomly board = do
let vMoves = getValidMoves board board
if vMoves == [] then return [] else
rMove <- uniform vMoves
let Just nBoard = runMove board rMove
rest <- playRandomly nBoard
return (rMove : rest)
Run Code Online (Sandbox Code Playgroud)
基本上,如果列表为空,该函数uniform将除以零,因此我需要一种方法来捕获它并在继续执行 do 语句之前返回空列表。有什么建议吗?
我正在阅读https://wiki.haskell.org/Do_notation_considered_harmful并惊讶于阅读以下内容
新手可能会认为陈述的顺序决定了执行的顺序.......陈述的顺序也不是评估顺序的标准.
维基文章给出了一些展示这个属性的例子.虽然这些例子很有意义,但我仍然不完全相信这个陈述是正确的,因为如果我写的是这样的话
main = do
putStrLn "foo"
putStrLn "bar"
putStrLn "baz"
Run Code Online (Sandbox Code Playgroud)
这三行按照陈述的顺序排列.那到底是怎么回事?
我理解(有些)monad并理解运算符< - 将从monad中提取值.
但它如何适用于不同类型?
通常,我已经看到它被用于从IO monad中提取字符串.但是在下面的示例代码中,我无法看到它为什么在主要的第3行失败,抱怨它期待一种类型的IO int?编译器如何推断需要IO int?
它(<-)在multWithLog方法中做了什么?
import Control.Monad.Trans.Writer.Lazy
main = do
putStrLn $ show $ logNumber 3 -- prints WriterT (Identity (3,["Got Number: 3"]))
putStrLn $ show $ multWithLog -- prints WriterT (Identity (3,["Got Number: 3"]))
_ <- logNumber 3 -- fails with Couldn't match type ‘WriterT [String] Data.Functor.Identity.Identity’ with ‘IO’
-- Expected type: IO Int
-- Actual type: Writer [String] Int
putStrLn "test"
logNumber :: Int -> Writer [String] Int
logNumber x …Run Code Online (Sandbox Code Playgroud) 我有以下代码
newtype State s a = State { runState :: s -> (s,a) }
evalState :: State s a -> s -> a
evalState sa s = snd $ runState sa s
instance Functor (State s) where
fmap f sa = State $ \s ->
let (s',a) = runState sa s in
(s',f a)
instance Applicative (State s) where
pure a = State $ \s -> (s,a)
sf <*> sa = State $ \s ->
let (s',f) = runState sf …Run Code Online (Sandbox Code Playgroud) 我想使用 hspec 创建一些具有不同值的测试。我写了下面的代码,它不能编译但给出了我的目标:
spec :: Spec
spec = do
describe "productOneLine" $ do
let
inVector = Data.Vector.replicate 0 0
inInteger = 3
outVector = Data.Vector.replicate 1 0
in
it "must manage empty vector" $ productOneLine inVector inInteger `shouldBe` outVector
let
inVector = Data.Vector.fromList [2, 4, 5]
inInteger = 4
outVector = Data.Vector.fromList [9, 6, 1, 2]
in
it "must multiply a vector by an integer" $ productOneLine inVector inInteger `shouldBe` outVector
Run Code Online (Sandbox Code Playgroud)
如何为每个以 开头的 ligne创建不同的inVector, inIntegeret集?outVectorit