Answer the question
In order to leave comments, you need to log in
How to check if a string is a number in Haskell?
There is this code in Haskell:
import Data.Char
isAtom :: String -> Bool
isAtom word = all isAlpha word
token :: String -> String
token word
| word `elem` ["+", "-", "/", "*", "%"] = "operator"
| word == "(" = "lbracket"
| word == ")" = "rbracket"
| (c1 == '"') && (c2 == '"') = "string"
| isAtom word = "atom"
where
c1 = head word
c2 = last word
read word :: Float
and catch an exception, or loop through all the characters in the string and check if they are digits. The second option would be good if it weren't for fractional numbers - you could just write all isDigit word
. Is there any simpler and more obvious way to do this? (+ 1 (* 5 4 (/ 4 2)) 3)
. I already wrote this in Python, now I'm just rewriting the same in Haskell to get my hands on it a little before writing a full-fledged interpreter for "Write your Scheme in 48 hours"
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question