Note that there are some explanatory texts on larger screens.

plurals
  1. POcreating a dialogue in command line in haskell, where output depends on previous processed input
    primarykey
    data
    text
    <p>I am finishing a program that translates a higher-order language to another and I am having trouble dealing with the interface I am trying to implement. </p> <p>The idea is that it asks the user for a number to be chosen, then checks whether it is a number, if it is, it follows with a line asking to write the given term to be translated. </p> <p>If the number is 4, it has an additional line asking for the term to be substituted, thus the translating function takes 2 args instead of 1. Therefore I'd like to have 3 lines of input for the user to complete depending on the previous answer, however my code asks for a number and then prints the next question and returns directly to the beginning, printing the first lines again.</p> <p>I have noticed that it does not test the validity of the Char taken from the input*<em>(isDigit num)</em>* and always chooses the path of <em>if(ord num /= 4)</em> equals to True, even when I key the number 4!</p> <p>I have tried several paths to sort this out but I am at a lost. I am more than happy to change my interface code completely if you know of any better approaches, because I cannot think of any other.</p> <p>I am quite amateur at Haskell by the way.</p> <pre><code>main :: IO () main = do putStrLn "Choose a definition to be implemented:\n 1 - Def 1 dealing with a Generalised class of CRS terms without permutations,\n 2 - Def 2 which is an extension of Def 1 with permutations,\n 3 - Def 4 which takes a closed nominal term-in-context, returning a closed CRS (meta)term,\n 4 - Def 6 which extends on Def 4 by adding a ground nominal substitution to the arguments.\n Definition number: " num &lt;- getChar unless (isDigit num) (return ()) def num main def :: Char -&gt; IO String def num = do putStrLn "write a nominal term-in-context: " hFlush stdout str &lt;- getLine case num of '1' -&gt; (return . readExp1) str '2' -&gt; (return . readExp2) str '3' -&gt; (return . readExp4) str '4' -&gt; do putStrLn "write a nominal term and the variable it substitutes:" sub &lt;- getLine let term = readExp6 str (readSub sub) return term {- reads and ouputs type TrmCxt ((atm,Var),Trm) -} readExp1 :: String -&gt; String readExp1 s = case (inputL parseT s) of Left err -&gt; "error: " ++ err Right (fc , t) -&gt; show $ translate1 [] fc t </code></pre> <p>I have added to the snippet the function <em>readExp1</em> which parses the input and calls the translating function for you to understand what it does and what it returns. The others are equivalent in their return type.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload