You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何使用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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.21 19:18:17