Parsing/Shunting-yard algorithm: Difference between revisions
m
→{{header|Haskell}}: Applied hlint, ormolu. Added type signatures.
m (Removed useless "sequtils" import.) |
m (→{{header|Haskell}}: Applied hlint, ormolu. Added type signatures.) |
||
Line 2,237:
<lang Haskell>import Text.Printf
prec :: String -> Int
prec "^" = 4
prec "*" = 3
Line 2,243 ⟶ 2,244:
prec "-" = 2
leftAssoc :: String -> Bool
leftAssoc "^" = False
leftAssoc _ = True
isOp
isOp
isOp _ = False
simSYA
simSYA xs
where
final =
lastStep =
(reverse (takeWhile testOp st) ++ out▼
)
| t == ")" = (reverse (takeWhile (/="(") st) ++ out,▼
$ last final
tail $ dropWhile (/="(") st, t)▼
|| prec t < prec x)▼
(t :) (dropWhile testOp st),
t
)
| t == "(" = (out, "(" : st, t)
| t == ")" =
t
)
| otherwise = (t : out, st, t)
where
testOp x =
isOp x
&& ( leftAssoc t && prec t == prec x
)
main :: IO ()
main = do
mapM_
mapM_ (\(x,y,z) -> printf "%30s%20s%7s\n" ▼
( \(x, y, z) ->
(unwords $ reverse x) (unwords y) z) $ simSYA $ words a</lang>▼
printf
(unwords y)
z
)
$ simSYA $ words a</lang>
Output:
|