有没有办法在Haskell中序列化(读/显示)函数?
举例来说:
:t (+1)
(+1) :: Num a => a -> a
Run Code Online (Sandbox Code Playgroud)
我希望能够有类似的东西:
read "(+1)" :: Num a => a -> a
Run Code Online (Sandbox Code Playgroud)
不幸的是,这会引发错误:
Could not deduce (Read (a -> a)) arising from a use of `read'
from the context (Num a)
bound by an expression type signature: Num a => a -> a
at <interactive>:1:1-30
Possible fix:
add (Read (a -> a)) to the context of
an expression type signature: Num a => a -> a
or add an instance …Run Code Online (Sandbox Code Playgroud) 我有一些简单的原始操作,例如:
在的情况下,操作单子:
import Control.Monad.Operational
type Process a = Program ProcessI a
data ProcessI a where
GetInput :: ProcessI String
Dump :: String -> ProcessI ()
getInput :: Process String
getInput = singleton GetInput
dump :: String -> Process ()
dump = singleton . Dump
Run Code Online (Sandbox Code Playgroud)
或者在免费 monad的情况下:
import Control.Monad.Free
type Process = Free ProcessF
data ProcessF a
= GetInput (String -> a)
| Dump String a
deriving (Functor)
getInput :: Process String
getInput = liftF $ GetInput id …Run Code Online (Sandbox Code Playgroud) 在Haskell中,您可以拥有无限列表,因为它不完全计算它们,它使用thunk.我想知道是否有一种方法可以序列化或以其他方式将一个数据保存到文件中.例如,假设您有一个列表[0..].然后,你做一些处理就可以了(我最感兴趣tail和(:),但它应该支持这样做filter或map为好.)这里是有点什么,我找的一个例子.
serial::(SerialThunk a)=>a->serThunk
serialized = serial ([0..] :: [Int])
main=writeToFile "foo.txt" serialized
Run Code Online (Sandbox Code Playgroud)
和
deserial::(SerialThunk a)=>serThunk->a
main=do
deserialized <- readFromFile "foo.txt" :: IO [Int]
print $ take 10 deserialized
Run Code Online (Sandbox Code Playgroud)