如何使用PureScript从标准输入(stdin)获取字符串?
PureScript 读取标准输入(stdin)的完整实现
1. 安装必要依赖
使用Spago管理依赖的话,执行以下命令引入所需包:
spago install node-readline effect
2. 完整代码(Main.purs)
module Main where import Effect (Effect) import Effect.Console (log) import Node.ReadLine (Interface, close, createInterface, question) import Node.ReadLine.Types (ReadLineOptions(..)) main :: Effect Unit main = do -- 创建标准输入读取接口,默认绑定stdin/stdout let readLineOpts = ReadLineOpts { input: Nothing , output: Nothing , completer: Nothing , terminal: Nothing , historySize: Nothing , prompt: Nothing } rl <- createInterface readLineOpts -- 提示用户输入并读取内容 input <- question rl "请输入字符串:" -- 输出读取到的内容 log $ "你输入的内容是:" <> input -- 关闭读取接口 close rl
3. 编译与运行
通过Spago编译并执行程序:
spago run
运行后终端会显示输入提示,输入字符串按回车,程序就会打印出你输入的内容。
内容的提问来源于stack exchange,提问作者LKB
相关产品推荐
相关产品推荐

