Haskell syntax, parsing errors for dummies

Today I read a lot about Haskell, but this formation is driving me crazy. I want to understand the main errors as soon as possible in order to start coding normally. The function here should return a string that begins with the next line "math Sign", the string (2sdwds+asd)+3should return +asd)+3. Here is the code

getToNextSign :: String -> String 
getToNextSign str = do

let mathSigns = ['+' , '-' , '*' , '/' , '^' , ')']
let a = head str
if a `elem` mathSigns 
 then str
 else if tail str /= [] 
       then getToNextSign $ tail str
       else []

main = do
putStrLn $ getToNextSign "(2sdwds+asd)+3"

It gives me "input parsing error =". I'm also not sure how to call it basically, and I really need the putStrLn function. I don’t think I need it, but I tried in 2874 different ways to write this, and now I just gave up and need help.

+3
source share
3 answers

Haskell, , , . :

getToNextSign :: String -> String
getToNextSign str = do
  let mathSigns = ['+' , '-' , '*' , '/' , '^' , ')']
  let a = head str
  if a `elem` mathSigns
   then str
   else if tail str /= []
         then getToNextSign $ tail str
         else []

main = do
  putStrLn $ getToNextSign "(2sdwds+asd)+3"

, , . let .

getToNextSign :: String -> String
getToNextSign str =
  if a `elem` mathSigns
   then str
   else if tail str /= []
         then getToNextSign $ tail str
         else []
  where
    a = head str
    mathSigns = ['+' , '-' , '*' , '/' , '^' , ')']
+2

, , , . , if-else if-else , :

getToNextSign :: String -> String
getToNextSign str
  | a `elem` mathSigns = str
  | tail str /= []     = getToNextSign $ tail str
  | otherwise          = []
  where
    a = head str
    mathSigns = ['+' , '-' , '*' , '/' , '^' , ')']

, / ,

getToNextSign :: String -> String
getToNextSign (c:cs)
  | c `elem` mathSigns = c:cs
  | not (null cs)      = getToNextSign cs
  | otherwise          = []
  where
    mathSigns = ['+' , '-' , '*' , '/' , '^' , ')']

, .

getToNextSign :: String -> String
getToNextSign str = case str of
     c:_ | c `elem` mathSigns -> str
     c:[] -> []
     _:cs -> getToNextSign cs
  where mathSigns = ['+' , '-' , '*' , '/' , '^' , ')']

, , , getToNextSign , , , .

+3

Here is a simpler alternative to your problem using the Prelude, dropWhileand list functions elem, which is of type sig (a -> Bool) -> [a] -> [a]. It takes a function that discards items from the list if the condition provided by the function is true.

Therefore, your function can be rewritten as follows.

let getNextSign x = dropWhile  ( not . `elem` "+-*/^)" ) x

Try to avoid explicit recursion whenever possible, and use their higher order functions reliably. And Prelude has many list manipulation features that come in handy all the time.

+3
source

All Articles