Search the haskell list




So, I have this calculator that I am creating that accepts user input variables like "let a = 2". These variables are stored in a list of tuples (variable, value). I need help getting data from this list. My code so far



```
primary :: Parser Float
primary = do symbol "("
         e <- expression
         symbol ")"
         return e
       +++ do v <- identifier                 
              let a = (find (==(head v)) vlist)
              return a

        

        
        
        
 (adsbygoogle = window.adsbygoogle || []).push({});
 
        

    
```

I am getting an error because find returns a Maybe value and I need it to return a Float or give the user an error message. How to do it?
I'm not sure where it comes from `vlist`
  




 (adsbygoogle = window.adsbygoogle || []).push({});
 
  

. It should probably be part of the user state of the parser. For now, let's assume this is the top-level definition:



```
vlist :: [(String, Float)]
vlist = undefined -- fill in the blanks...

        

        
        
        
 (adsbygoogle = window.adsbygoogle || []).push({});
 
        

    
```

I assume you are using `Parsec`
  




 (adsbygoogle = window.adsbygoogle || []).push({});
 
  

. You can simplify your parser:



```
primary :: Parser Float
primary = choice [ between (symbol "(") (symbol ")") expression
                 , do { ident <- identifier
                      ; case lookup ident vlist of
                          Nothing -> fail $ "No such identifier: " ++ ident
                          Just v -> return v
                      }
                 ]

        

        
        
        
 (adsbygoogle = window.adsbygoogle || []).push({});
 
        

    
```


  




 (adsbygoogle = window.adsbygoogle || []).push({});
 
  

You have several options for solving this problem. Here I used the parader monad function `fail`
  




 (adsbygoogle = window.adsbygoogle || []).push({});
 
  

. This will cause the parser to return a value `Left parserError`
  




 (adsbygoogle = window.adsbygoogle || []).push({});
 
  

. Alternatively, you can replace `error`
  




 (adsbygoogle = window.adsbygoogle || []).push({});
 
  

with `fail`
  




 (adsbygoogle = window.adsbygoogle || []).push({});
 
  

, which will result in an error that can only be handled in a monad `IO`
  




 (adsbygoogle = window.adsbygoogle || []).push({});
 
  

.


 **Note** . To add `vlist`
  




 (adsbygoogle = window.adsbygoogle || []).push({});
 
  

a parser as a state, you need to define a new parser type with this state:



```
data MyParserState = MyParserState { vlist :: [(String, Float)] }
type MyParser = CharParser MyParserState

-- these parsers now need to return MyParser type!
symbol :: String -> MyParser String
identifier :: MyParser String
expression :: MyParser Float

primary :: MyParser Float
primary = choice [ between (symbol "(") (symbol ")") expression
                 , do { st <- getState
                      ; ident <- identifier
                      ; case lookup ident $ vlist st of
                          Nothing -> fail $ "No such identifier: " ++ ident
                          Just v -> return v
                      }
                 ]

        

        
        
        
 (adsbygoogle = window.adsbygoogle || []).push({});
 
        

    
```
I will preface this by saying that I don't know Java at all. I touched upon it in school 20+ years ago, but I've been doing PHP ever since.


However, on line 27, you say that if the input passed in is a double, execute this code block *blah*.


The user enters -1.


Is -1 not a double? It is a valid double, and therefore the code block executes, setting the priceFromUser to be -1 and adding it to the list.


Then, when the loop starts back up again on line 23, priceFromUser is now -1, so the loop breaks and prints the tally.


Also, note you're definining this constant SENTINEL variable, but you're not actually using it anywhere in your code.


Perhaps do something like this instead, changing the while loop into a do-while loop:



```
System.out.println("Please enter item price to start, since there needs to be at least 1 item:");

do
{

        // Keep asking for a price until the user enters a valid price
        while (!in.hasNextDouble())               
        {
                trash=in.nextLine(); //throw away the input 
                System.out.println("You did not enter a valid price. Please try again:");
        }

        priceFromUser=in.nextDouble(); //read value from user
        list.add(priceFromUser); //add valid price to the list            

          System.out.println("Please enter another item price, or enter -1 to quit.");

} while (priceFromUser != SENTINEL)
```

Note this isn't valid code because I'm not a Java programmer, but hopefully it helps you out a bit. see [creating feedback loops](https://vvvv.org/documentation/creating-feedback-loops).

it’s only the subtype of a datatype. don’t worry about that.

fyi: create an [asstring (value)](https://vvvv.org/documentation/asstring-(value)) and check its sub type for possible subtypes of the datatype “value” for this, just use an if function. see https://macmost.com/learn-how-to-use-the-if-function-in-numbers.html so something like this in a2: if(isblank(b2),c2,b2) the condition is to check if b2 is blank. if it is, get the value of c2. if it isn’t blank, then get the value of b2. hi stuti, 
index can be used with lists, not tables. 
if a variable is used as an index, it is put in the square brackets without the dollar sign and curly brackets, i.e. ${data\_list[item\_index]}. 
you can read more about using indexes with lists in the knowledge base: [https://kb.workfusion.com/disp…](https://kb.workfusion.com/display/rpae/recorder+variables#recordervariables-usingindexeswithlists) Please post the insert\_validation.php script also since the values should be accessible there (assuming that the queries return expected values). You need to give the user the option of typing in a value that represents that there are no more values to enter. Perhaps give a prompt like:



```
Please enter a value.  Enter -999999 if you have no more values [B]:[/B]
```

As for the ten (or fewer) values, you should probably have an array and a loop instead of ten separate variable names and ten separate cin statements. So you're looking at something like this:



```
double dValue[10];
    int numEntries = 0;
    double sum = 0;
    
    while (numEntries < 10)
    {
          cout << "Enter number (-999999 to stop) : ";
          cin >> dValue[numEntries];
          // check whether dValue[numEntries] == -999999
          // if so, short-circuit the loop with the "break" command
            
          // increment numEntries
          // adjust sum
    }
```

As a side note, I picked -999999 as the value to represent no more values because



1. It's an integer, so it can be represented exactly as a double (no round-off error using == to compare).
2. It's a number that, chances are, people wouldn't actually want to add, so it's sort of "out of the way". Chances are, few people want to add -999999. If they do, they can't use this program! > Hi pliz assist me, I need a code that can allow the login form to search for pin number stored in another form example when a customer enters the pin number and clicks ok button the login form searches if the pin number exists in the bank details form ..thanx in advance
> 
> 


post code i've been meaning to write a blog post on this for a while, but haven't got round to it yet, so i'll give the condensed version here :slight\_smile:

to understand why the if let syntax is the way it is, we should first look at the let syntax. in most cases, you'll see it used for simple variable dec… Check if any of the values in the cells are null.  

If they are you need to modify your code to check for null conditions before using `ToString()`. you can use lazy evaluation so if the first expression returns true it will not check the second expression. just change the ‘or’ in your statement to ‘orelse’ so it would be:

“string.isnullorempty(message) orelse message.contains(”!")"