Question

In: Computer Science

<Haskell> Using getCh, define an action readLine :: IO String that behaves in the same way...

<Haskell>

Using getCh, define an action readLine :: IO String that behaves in the same way as getLine, except that it also permits the delete key to be used to remove characters.

Hint: the delete character is ’\DEL’, and the control character for moving the cursor back one space is ’\b’.

Solutions

Expert Solution

Solution

> getLine' :: IO String
> getLine' = do x <- getChar
> if x == '\n' then
> return []
> else
> do xs <- getLine
> return (x:xs)

Note: `getLine` above allows for the use of the backspace key.
It works as you'd expect. Further, GHCI won't allow me to enter '\DEL'
as a character.

As a result, on windows, this solution doesn't do anything that
`getLine` doesn't despite the added complexity.

On a linux system, the behaviour is thus:

> getLine'
this^?is
"this\DELis"
> readLine
this^?is
"thiis"

> readLine :: IO String
> readLine = get ""

> get :: String -> IO String
> get xs = do x <- getChar
> case x of
> '\n' -> return xs
> '\DEL' -> if null xs then
> get xs
> else
> do putStr "\ESC[1D \ESC[ID"
> get $ init xs
> _ -> get $ xs ++ [x]
> getLine' :: IO String
> getLine' = do x <- getChar
> if x == '\n' then
> return []
> else
> do xs <- getLine
> return (x:xs)

Note: `getLine` above allows for the use of the backspace key.
It works as you'd expect. Further, GHCI won't allow me to enter '\DEL'
as a character.

As a result, on windows, this solution doesn't do anything that
`getLine` doesn't despite the added complexity.

On a linux system, the behaviour is thus:

> getLine'
this^?is
"this\DELis"
> readLine
this^?is
"thiis"

> readLine :: IO String
> readLine = get ""

> get :: String -> IO String
> get xs = do x <- getChar
> case x of
> '\n' -> return xs
> '\DEL' -> if null xs then
> get xs
> else
> do putStr "\ESC[1D \ESC[ID"
> get $ init xs
> _ -> get $ xs ++ [x]

---

all the best


Related Solutions

<Haskell> Define an action adder :: IO () that reads a given number of integers from...
<Haskell> Define an action adder :: IO () that reads a given number of integers from the keyboard, one per line, and displays their sum. For example: > adder How many numbers? 5 1 3 5 7 9 The total is 25 Hint: start by defining an auxiliary function that takes the current total and how many numbers remain to be read as arguments. You will also likely need to use the library functions read and show.
android studio Reproduce the same action as this using a ListView. <resources> <string-array name="pizzas"> <item>Ham and...
android studio Reproduce the same action as this using a ListView. <resources> <string-array name="pizzas"> <item>Ham and Pineapple</item> <item>Supreme</item> <item>Seafood</item> <item>Italian</item> <item>Meat Lovers</item> </string-array> </resources> Following is the layout XML File <?xml version="1.0" encoding="utf-8"?> <android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity"> <Spinner android:id="@+id/spinner" android:layout_width="368dp" android:layout_height="wrap_content" tools:layout_editor_absoluteX="8dp" tools:layout_editor_absoluteY="42dp" /> <TextView android:id="@+id/textView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="TextView" tools:layout_editor_absoluteX="163dp" tools:layout_editor_absoluteY="145dp" /> </android.support.constraint.ConstraintLayout> Following is the JAVA main File. package com.example.wincrap.myapplication; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.AdapterView; import android.widget.ArrayAdapter; import android.widget.Spinner; import android.widget.TextView; public class...
#include <bits/stdc++.h> using namespace std; string ltrim(const string &); string rtrim(const string &); #define BUFFLEN 10...
#include <bits/stdc++.h> using namespace std; string ltrim(const string &); string rtrim(const string &); #define BUFFLEN 10 /*     This code closely mimics the Python hash table we created in class this     week. Each "person" is defined by their name, zipcode, and their pet's name.     Persons are hashed by their zipcode. */ //---------------------------------------------------------------------------- /* function declarations ------------------------*/ int computeKey(int); void add_to_buffer(string,int,string); void find_in_buffer(int); //---------------------------------------------------------------------------- /* define a "person" --------------------*/ class person{     public:     // variables that define a person     string name;     int zipcode;...
define aray and string?
define aray and string?
using any form of induction, that a binary string begins and ends with the same character...
using any form of induction, that a binary string begins and ends with the same character iff contains an even number of occurences of substrings from (01,10).
All action potentials are the same size?
Question 71 (1 point) All action potentials are the same size. Question 71 options: True False Question 72 Parkinson's Disease is a pituitary disorder. Question 72 options: True False Question 75 PTH stimulates osteoblasts. Question 75 options: True False Question 76 People with myopia have difficulty focusing on objects that are far away. Question 76 options: True False Question 77 Hydroxyapatite is formed from potassium and calcium. Question 77 options: True False Please answer all questions.
15.1 File IO Warm-up For this exercise, you will receive two string inputs, which are the...
15.1 File IO Warm-up For this exercise, you will receive two string inputs, which are the names of the text files. You need to check if the input file exists. If input file doesn't exist, print out "Input file does not exist." You are required to create 4 functions: void removeSpace (ifstream &inStream, ofstream &outStream): removes white space from a text file and write to a new file. If write is successful, print out "Text with no white space is...
Taxes are the way our government generates income. Although the richest Io/o of the people in...
Taxes are the way our government generates income. Although the richest Io/o of the people in our country pay 39% of the taxes and the bottom 50% combined pay only 2.9% combined, everyone wants things from the government. They want roads and bridges and schools and an army and welfare and Medicaid and concerts and ]..... To provide these things our government has to raise money and cut expenses. Which of the following laws help raise money? Which help cut...
public class AddValueNewArray { // You must define the addValueNew method, which behaves // similarly to...
public class AddValueNewArray { // You must define the addValueNew method, which behaves // similarly to the addValueTo method in AddValueToArray.java. // However, instead of adding the given value to the given // array, it will return a _new_ array, where the new array // is the result of adding the value to each element of the // given array. To be clear, the given array NEVER CHANGES. // // TODO - define your code below this comment // //...
Q, Haskell. I need to define function encode and decode. Details about the functions are provided...
Q, Haskell. I need to define function encode and decode. Details about the functions are provided in code. -- | encode -- -- Given a string, return a list of encoded values of type (Int,Char) -- -- >>> encode ['a','a','a','a','b','c','c','a','a','d','e','e','e','e'] -- [(4,'a'),(1,'b'),(2,'c'),(2,'a'),(1,'d'),(4,'e')] -- -- >>> encode "hello" -- [(1,'h'),(1,'e'),(2,'l'),(1,'o')] -- -- >>> encode [] -- [] -- encode :: String -> [(Int,Char)] encode = undefined -- | decode -- -- Given a list of encoded values of type (Int,Char), generate...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT