Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Turtle: Prompting for input, without a trailing newline

I'm writing a console application that needs to prompt the user for several things. I'm using the turtle library.

My function looks like this:

askInput :: IO (Maybe Text)
askInput = do
    echo "Input something: "
    s <- readline
    return s

But echo is implemented using putStrLn and as a result, will print its argument with a trailing newline.

Is there an input function in the turtle library similar to Python's raw_input, that combines prompting followed by reading the user input?

like image 804
Mariano Avatar asked Feb 01 '26 12:02

Mariano


2 Answers

You can import from the text package and use many functions which aren't exported from turtle. In this case:

{-# LANGUAGE OverloadedStrings #-}

import qualified Data.Text.IO as Text

main = Text.putStr "Input something: " -- doesn't print newline
like image 117
András Kovács Avatar answered Feb 03 '26 06:02

András Kovács


I also want to mention that turtle has a newly added printf function which outputs a formatted string without the trailing newline, so another solution is:

{-# LANGUAGE OverloadedStrings #-}

import Turtle

main = printf "Input something: "
like image 23
Gabriella Gonzalez Avatar answered Feb 03 '26 08:02

Gabriella Gonzalez