以下是有效的
main = do
trace $ show $ 5
Run Code Online (Sandbox Code Playgroud)
但事实并非如此
main = do
(trace . show) 5
Run Code Online (Sandbox Code Playgroud)
在psci中,跟踪的类型是
forall r. Prim.String -> Control.Monad.Eff.Eff (trace :: Debug.Trace.Trace | r) Prelude.Unit
Run Code Online (Sandbox Code Playgroud)
和节目的类型是
forall a. (Prelude.Show a) => a -> Prim.String
Run Code Online (Sandbox Code Playgroud)
因为show的返回值是Prim.String第一个输入到trace中的,Prim.String所以它们应该是可组合的.trace $ show通过类型检查进一步证明了这一点.但相反,我得到这个错误:
Error at line 1, column 10:
Error in declaration it
Cannot unify Prim.Object with Prim.Function Prim.String.
Run Code Online (Sandbox Code Playgroud)
我在这里错过了什么?现在我的心理模型trace就像putStrLn在Haskell中一样,而且一个人肯定可以用show.(putStrLn . show) 5作品.
跟踪和显示的预期类型的组合结果:
forall a r. (Prelude.Show …Run Code Online (Sandbox Code Playgroud) 我刚刚开始学习Purescript所以我希望这不是一个愚蠢的问题.
假设我们有一个对象
a = {x:1,y:2}
Run Code Online (Sandbox Code Playgroud)
我们想要将x改为等于2.据我所知,如果我们使用ST monad,我们将不得不复制整个对象以更改值.如果初始对象很大,这将是非常低效的.改变对象的正确方法是什么?
我正在寻找一种List Char -> String在Purescript中编写函数的惯用方法.
这似乎是一件很简单的事情,但我是Purescript的新手,并且已经暂时浏览文档一段时间没有任何进展!
背景信息:我正在将一个简单的函数从Haskell移植到Purescript
generateId :: Int -> [Char]
Run Code Online (Sandbox Code Playgroud)
这会生成指定长度的String.将代码转换为使用List Char操作非常容易(List来自Data.ListPurescript).在Haskell中[Char]是相同的,String因此不需要其他处理,但是,我找不到在Purescript中转换List Char为本机String的函数!
我的搜索导致我fromCharArray :: Array Char -> String的Data.String,但是我不能找到一种方法,从转换List Char到Array Char!
我可以手动通过折叠它们之间的转换List Char和全面建设Array Char使用snoc,但肯定我必须失去了什么似乎像在Purescript基本字符串操作一个内置的解决方案!
编辑: fromList适用于从任何Unfoldable(如阵列)转换为List.仍然保留这个问题以防万一有更实用的方法.
在测试时,我想从磁盘读取和写入文件(JSON in,JSON out).我如何在PureScript中执行此操作?在Haskell我有类似的东西
main :: IO ()
main = do
input <- readFile "input.json"
writeFile "output.json" $ process input
Run Code Online (Sandbox Code Playgroud)
我的所有尝试到目前为止都失败了,包括试图使用readFile从import Node.FS.Sync.
顺便说一句,如果我的JSON文件中还有其他(更好)的阅读方式,请告诉我.(我不是JavaScript专家,但我想将一些 - 严格 - Haskell代码移植到JS,以便它可以在其他地方使用.)
所以,我试图通过转换99个Haskell问题中的一些Haskell代码来学习Purescript ,并且很快就遇到了我知道如何解决它的情况,但它太简单了.这是问题10,11和12的Haskell代码; 基本上是一些RLE编码和解码功能:
-- Problem 10
rle :: Eq ? => [?] -> [(Int, ?)]
rle [] = []
rle (x:xs) = let (h, t) = span (== x) xs
in (length h + 1, x) : rle t
-- Problem 11
data RleItem ? = Pair Int ? | Single ? deriving (Show)
encode :: Eq ? => [?] -> [RleItem ?]
encode = map unpack . rle
where unpack (1, x) = …Run Code Online (Sandbox Code Playgroud) 虽然这个例子是设计的,但如果忽略数据构造函数,为什么我不能使用通配符模式呢?
module Main where
import Prelude
import Control.Monad.Eff.Console (log)
data Person = Amy { name :: String } | George { name :: String }
--Implementations Options Below
main = log $ personToString $ George { name: "George" }
Run Code Online (Sandbox Code Playgroud)
没有错误
personToString :: Person -> String
personToString (Amy { name: n }) = n
personToString (George { name: n }) = n
Run Code Online (Sandbox Code Playgroud)
错误
personToString :: Person -> String
personToString (_ { name: n }) = n
Run Code Online (Sandbox Code Playgroud)
http://try.purescript.org/?session=a1503b9a-0546-7832-39b0-6321a89ef2e3
Unable to parse module: …Run Code Online (Sandbox Code Playgroud) 我purescript通过nodejs以下命令序列成功运行了纸浆的helloworld :
$ pulp init
...
$ pulp run
* Building project in /data/works/beta_projects/js_games/processing/hello-ps
* Build successful.
Hello sailor!
Run Code Online (Sandbox Code Playgroud)
接下来,我想Hello sailor!通过在div内部文本中注入消息来使消息在正文中显示
<html>
<body>
<div id="output" />
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
我需要做什么?
我尝试使用pulp server命令并获得空白页,http://localhost:1337/而无需提供index.html任何信息,并且控制台中没有任何消息。
有pulp browserify一条命令可以打印出我希望包含在index.html的script标签中的javascript的命令,但是我不知道我需要在哪里放置index.html文件。
以下是一个最小的、人为的示例:
read :: FilePath -> Aff String
read f = do
log ("File: " <> f) -- (1)
readTextFile UTF8 f -- (2)
Run Code Online (Sandbox Code Playgroud)
我想在发生(1)潜在错误之前进行一些调试日志记录(2)。到目前为止,在 Spago REPL 中执行以下代码适用于成功案例:
$ spago repl
> launchAff_ $ read "test/data/tree/root.txt"
File: test/data/tree/root.txt
unit
Run Code Online (Sandbox Code Playgroud)
(2)问题:如果- file is directory here -出现错误,(1)则似乎根本没有执行:
$ spago repl
> launchAff_ $ read "test/data/tree"
~/purescript-book/exercises/chapter9/.psci_modules/node_modules/Effect.Aff/foreign.js:532
throw util.fromLeft(step);
^
[Error: EISDIR: illegal operation on a directory, read] {
errno: -21,
code: 'EISDIR',
syscall: 'read'
} …Run Code Online (Sandbox Code Playgroud) debugging logging functional-programming purescript visual-studio-code
我有两个我认为等效的类型类和实例定义的实现。PureScript 版本构建时没有错误,但 Haskell 版本失败并出现错误Un-determined variables: e, f。
我可以在 Haskell 中完成这个工作吗?
哈斯克尔:
class Foo a b c | a b -> c where
newtype Bar a b = Bar (a, b)
instance (Foo a c e, Foo b d f) => Foo (Bar a b) (Bar c d) (Bar e f) where
Run Code Online (Sandbox Code Playgroud)
纯脚本:
class Foo a b c | a b -> c
newtype Bar a b = Bar (Tuple a b)
instance (Foo a c e, Foo b …Run Code Online (Sandbox Code Playgroud) PureScript中Sproxy有什么用?
在追求中,它被写成
data SProxy (sym :: Symbol)
--| A value-level proxy for a type-level symbol.
Run Code Online (Sandbox Code Playgroud)
purescipt 中的 Symbol 是什么意思?