2

I'm learning Haskell. I see an author uses some mathematic unicode chars in the code here ( for example). I try use it too, for example:

(∀) :: (a -> b) -> [a] -> [b]
f ∀ [] = []
f ∀ (x:xs) = f x : f ∀ xs

ghci loads this sucessfully, but I can't call this function... When I try paste the symbol into ghci console from the clipboard, nothing happens (Windows 7, Lucida console font).

It is very convenient - to use mathematic symbols (in my opinion). How can I use it in the ghci?

phadej
  • 11,947
  • 41
  • 78
Andrey Bushman
  • 11,712
  • 17
  • 87
  • 182
  • This may help you: http://stackoverflow.com/questions/25373116/how-can-i-set-my-ghci-prompt-to-a-lambda-character-on-windows – Sibi Jan 30 '15 at 08:42
  • I did it (`chcp.com 65001`) but this is not help me. The `λ` symbol is displayed sucessfully, but `∀` - not (when I try paste it). Oh, I can't paste the `λ` too via the clipboard... I can print it through my code only... – Andrey Bushman Jan 30 '15 at 09:08
  • codepage 65001 is not UTF8, it just uses the same packing mechanism. – Jeremy List Jan 30 '15 at 09:56
  • Is it really more convenient to use mathematical symbols while typing? Especially with cmd.exe that doesn't support copy-paste by keyboard, switching to mouse on every type of that single character doesn't seem really convenient to me. Worse still if you have multiple of them, so you have to switch to mouse, select, copy, back to cmd.exe and paste. – Shahbaz Jan 30 '15 at 11:14
  • It seems more compact and more convinient for reading. When I need write a special symbol I do it through the hot keys. For example Alt + 0169 is ©, or Alt + 8704 is ∀. I got used to print special characters through the hot keys. It is convenient for me. – Andrey Bushman Jan 30 '15 at 11:22
  • 2
    This seems to work on Mac in it's Terminal: `*Main> (+1) ∀ [1,2]` `[2,3]`. I suspect this is *windows console* problem, not related to Haskell. – phadej Jan 30 '15 at 12:14
  • I found this: http://hackage.haskell.org/package/base-unicode-symbols – Andrey Bushman Jan 30 '15 at 13:00

1 Answers1

1

The most common way of getting the visuals of that is using a font and a text editor that supports ligatures, like Hasklig. That way it looks like you want it to, but it's saved as a regular ASCII file, which makes it much easier to work with with tools that don't support that technology.

I would recommend against using actual non-ASCII characters as identifiers; it might be common in Agda, but it can get simply annoying when trying to work on the same code with multiple people, on different platforms, and so on.

Bartek Banachewicz
  • 38,596
  • 7
  • 91
  • 135
  • But I see many matematics symbols in *source codes* in the books too... For example: Richard Bird (University of Oxford) "Pearls of Functional Algorithm Design". – Andrey Bushman Jan 30 '15 at 09:14
  • And I thought that it perhaps is a widespread method naming of functions in Haskell (by mathematicians). When I am reading the books I try its source code too. – Andrey Bushman Jan 30 '15 at 09:23
  • @Bush As long as you want to put your code as an illustrative example in the book, it might be fine. If you want to write actual computer programs that are going to be run, you've yourself noticed that non-ascii identifiers bring in a lot of problems for basically no reasonable gains. And they're harder to type. – Bartek Banachewicz Jan 30 '15 at 09:25
  • @Bush For real world code, I would suggest not to use unicode symbols. It is certainly not a widespread naming convention and none of the popular libraries in Hackage use them. – Sibi Jan 30 '15 at 09:34
  • It surprised me that the unicode-font, doesn't support some unicode-characters (The `Alt + 955` works fine in MS Word, but doesn't work in the cmd.exe or powershell.exe, despite Unicode font and code chart). – Andrey Bushman Jan 30 '15 at 09:36
  • 2
    I think it's worth clarifying that even when you see mathematical symbols in papers and books about Haskell programs, the original source code is usually plain ASCII text. We use tools like lhs2TeX to apply systematic and hopefully attractive typesetting conventions to produce the rendering of our ASCII code that you see in print. – pigworker Jan 30 '15 at 09:37
  • Those Unicode problems really aren't so bad on platforms other than Windows, and they should get ever less of an issue in the future. Harder to _type_? I don't really see that in a decent editor, though it's certainly a point in GHCi. Workarounds like Hasklig are cute, but they also bring problems with them (horizontal alignment, bad extendability...). IMO, real unicode identifiers are in many senses more consistent. And you can always supply a `YourModule.ASCII` for compatibility with non-unicode-capable systems, though I agree such redundancy isn't really nice either. – leftaroundabout Jan 30 '15 at 12:58