POV-Ray : Newsgroups : povray.off-topic : Why is Haskell interesting? : Re: Why is Haskell interesting? Server Time
4 Sep 2024 19:22:56 EDT (-0400)
  Re: Why is Haskell interesting?  
From: Orchid XP v8
Date: 27 Feb 2010 18:05:33
Message: <4b89a53d$1@news.povray.org>
Warp wrote:

>   [] is used almost universally for indexing in most languages.

Granted.

> Is there a logical reason why Haskell chooses an odd syntax of
> "x ! y" for the same thing than most other languages express as "x[y]"?

Yeah. "x[y]" would mean a function call, where "x" is the function name, 
and the function's argument is a 1-element list containing "y".

To put it another way, Haskell uses square brackets for lists. So you 
can't use it for indexing as well.

(Well, OK, if you changed the parsing rules of the language, you could. 
You could make it so that if you want "x[y]" to be a function call, you 
*must* put a space in there, otherwise it's an index. They already do 
this with dots; "x . y" is function composition, but "x.y" is a 
qualified name.)

>   Using ! for the nth element is not very expressive. In mathematics ! is
> usually used for factorials, in many programming languages it's used to
> express logical negation (granted, not a very logical choice per se, but
> quite established), and in natural languages it's used to express shouting
> or to emphasize the importance of something (like "WARNING!").

For what it's worth, "!" is used for arrays and dictionaries. For lists 
you use "!!". This is because indexing a list is obviously an O(n) 
operation, which is bad. So if you find yourself writing "!!", you 
should probably go check that there isn't some more performant way to do 
what you're trying to do. (Like use an array or something.)

Thus, using "!!" for a list sort-of makes sense. But for an array, "!" 
is O(1), so... um...

(Small footnote: You can't have unary operators, so there's no chance of 
using "!" for factorial. Unfortunately.)

>   So why choose ! as "the nth element"? Did they run out of obfuscated
> one-character operators and it was the only one left?

...pretty much, yes.

Arguably "@" would be more logical - but that's a resurved symbol. I 
suppose you could use "@@", but that's not terribly elegant.

I suspect what _really_ happened here is that when Haskell was first 
designed, it didn't *have* indexing at all. And then later, when Haskell 
became real-world enough to need it, they wrote it as a library, and 
didn't want to change the whole syntax of the language just to support 
arrays, so they just picked an available symbol name.

>   I know Haskells undying love for obfuscating brevity, but would it have
> hurt to actually use a descriptive *name* for that function? I don't know,
> like "c element_at 2" (or even "c at 2") or whatever.

You can't do "c at 2", but there's no reason you couldn't do "c `at` 2".

(Hell, if you utter the simple incantation "at = (!)" then the above 
will suddenly work. But that's obviously not the same thing has having 
the standard libraries implement it that way...)

>   Btw, what is logical not in Haskell?

You're going to love this...

It's "not". As in "not (x == y)".

(Of course, any sane person would write "x /= y" instead. Since most 
binary comparison operations have negated versions, you don't often need 
the not operator itself.)

For completeness, AND is "&&" and OR is "||". (Since "|" is already used 
for pattern guards.) Not too unusual.

Oh, and bitwise operations [which aren't available until you import the 
necessarily library] are ".&." and ".|.". No specific reason. Bitwise 
not is "complement".

-- 
http://blog.orphi.me.uk/
http://www.zazzle.com/MathematicalOrchid*


Post a reply to this message

Copyright 2003-2023 Persistence of Vision Raytracer Pty. Ltd.