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

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).
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...
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.
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 // //...
using the same software process model is not necessarily the best way to engineer all projects....
using the same software process model is not necessarily the best way to engineer all projects. Based on what you studied, answer the following: 1. Differentiate between processes and methodologies. 2. Determine a suitable process model for the following projects a) An enterprise software system where the stakeholders do not well understand requirements. Thus, requirements are expected to be extremely changeable due to external changes, changing expectations, changes in the budget and the rapid change in technology. b) An online...
Project: Given a string s and an integer array indices of the same length. The string...
Project: Given a string s and an integer array indices of the same length. The string s will be shuffled such that the character at the i th position moves to indices[i] in the shuffled string. Return the shuffled string. Example: Input: s = "codeleet", indices = [4,5,6,7,0,2,1,3] Output: "leetcode" Explanation: As shown, "codeleet" becomes "leetcode" after shuffling. You need to do: Create a class called ShuffledStringApp. Keeping class StringOperation and IO in util package. In this project, you will...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT