Idiomatically determine all the characters that can be used for symbols: Difference between revisions

→‎{{header|Haskell}}: added solution
mNo edit summary
(→‎{{header|Haskell}}: added solution)
Line 179:
Only follow: U+0030-U+0039, U+0660-U+0669, U+06F0-U+06F9, U+07C0-U+07C9, ..., U+1D7CE-U+1D7FF, U+1E950-U+1E959
</pre>
 
=={{header|Haskell}}==
 
Quotation from the Haskell 2010 language specification [https://www.haskell.org/onlinereport/haskell2010/haskellch2.html#x7-140002]
 
<pre> varid → (small {small | large | digit | ' }) / reservedid
conid → large {small | large | digit | ' }
reservedid → case | class | data | default | deriving | do | else
| foreign | if | import | in | infix | infixl
| infixr | instance | let | module | newtype | of
| then | type | where | _
 
small → ascSmall | uniSmall | _
ascSmall → a | b | … | z
uniSmall → any Unicode lowercase letter
large → ascLarge | uniLarge
ascLarge → A | B | … | Z
uniLarge → any uppercase or titlecase Unicode letter
 
digit → ascDigit | uniDigit
ascDigit → 0 | 1 | … | 9
uniDigit → any Unicode decimal digit</pre>
 
An identifier consists of a letter followed by zero or more letters, digits, underscores, and single quotes. Identifiers are lexically distinguished into two namespaces: those that begin with a lowercase letter (variable identifiers) and those that begin with an upper-case letter (constructor identifiers). Identifiers are case sensitive: name, naMe, and Name are three distinct identifiers (the first two are variable identifiers, the last is a constructor identifier).
 
Underscore, “_”, is treated as a lowercase letter, and can occur wherever a lowercase letter can. However, “_” all by itself is a reserved identifier, used as wild card in patterns.
 
According to the specification we may give predicates for valid symbols and identifiers in Haskell:
<lang haskell>import Data.Char
 
-- predicate for valid symbol
isSymbolic ch = isAlphaNum ch || ch `elem` "_'"
 
-- predicate for valid type construtor
isConId s = and [ not (null s)
, isUpper (head s)
, all isSymbolic (tail s) ]
 
-- predicate for valid identifier
isVarId s = and [ not (null s)
, isLower (head s)
, all isSymbolic (tail s)
, not (isReserved s) ]
 
-- predicate for reserved words
isReserved s = elem s ["case", "class", "data", "default", "deriving", "do "
, "else", "foreign", "if", "import", "in", "infix "
, "infixl", "infixr", "instance", "let", "module "
, "newtype", "of", "then", "type", "where", "_"</lang>
 
=={{header|J}}==
Anonymous user