28

What is a valid name for a function?

Examples

-- works
let µ x = x * x  
let ö x = x * x

-- doesn't work  
let € x = x * x  
let § x = x * x

I am not sure, but my hunch is that Haskell doesn't allow Unicode function names, does it? (Unicode like in http://www.cse.chalmers.se/~nad/listings/lib-0.4/Data.List.html)

Matthias Braun
  • 32,039
  • 22
  • 142
  • 171
mrsteve
  • 4,082
  • 1
  • 26
  • 63

1 Answers1

28

From the Haskell report:

Haskell uses the Unicode character set. However, source programs are currently biased toward the ASCII character set used in earlier versions of Haskell .

Recent versions of GHC seem to be fine with unicode (at least in the form of UTF-8):

Prelude> let пять=5; два=2; умножить=(*); на=id in пять `умножить` на два
10

(In case you wonder, «пять `умножить` на два» means «five `multiplied` by two» in Russian.)

Your examples do not work because those character are «symbols» and can be used in infix operators but not in function names. See "uniSymbol" category in the report.

Prelude> let x € y = x * y in 2 € 5
10
Roman Cheplyaka
  • 37,738
  • 7
  • 72
  • 121
  • 1
    tl;dr @mrsteve: Haskell treats identifiers consisting of punctuation (except `'`) to be infix operators. – ephemient Feb 16 '11 at 23:45
  • 10
    In particular, `isAlphaNum '€' == False`, whereas `isAlphaNum 'ж' == True`. Both € and § are in ISO/IEC_8859-15, btw, which is quite standard (in the non-US western world) – barsoap Feb 16 '11 at 23:48
  • When you use non-ASCII characters as a parameter name and bind a function to the parameter, you can use it as an infix operator. Here's an example that checks whether a function is associative: `asc (♥) a b c = a ♥ (b ♥ c) == (a ♥ b) ♥ c`. Call it like this: `asc (+) 1 2 3` – Matthias Braun Mar 21 '18 at 19:40