 |
 |
|
 |
|
 |
|  |
|  |
|
 |
|
 |
|  |
|  |
|
 |
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
|
 |
|  |
|  |
|
 |
|
 |
|  |
|  |
|
 |
Darren New wrote:
> I would guess it makes the thing into an infix operator, while x[y] is
> neither prefix, postfix, infix, or ... whatever.
The word you're looking for is apparently "mixfix".
> Plus, list literals are [ ... ], so I'm guessing that x [ y ] is
> ambiguous as to whether you're calling function x with an argument
> htat's a one-element list containing y.
Yeah. Although like I said in my other reply, they already made
whitespace significant in the case of the "." character. I'm guessing
this one's a historical accident.
> Now, why they picked ! instead of (say) @ is beyond me.
That would have been a better choice, but it's already taken. (And not
even by another library function - it's actually *reserved* in the
syntax rules.)
You could have had "@@", but they chose "!" for whatever reason...
Then again, this is the same language that gives you "^" and "**". Wanna
take a guess what the difference is?
Haskell is a brilliant language, but I never said it was perfect. ;-)
--
http://blog.orphi.me.uk/
http://www.zazzle.com/MathematicalOrchid*
Post a reply to this message
|
 |
|  |
|  |
|
 |
|
 |
|  |
|  |
|
 |
Orchid XP v8 <voi### [at] dev null> wrote:
> You can't do "c at 2", but there's no reason you couldn't do "c `at` 2".
If this were C++, it would actually be "c.at(2)" (in fact, with indexable
STL containers it not only *would* be, but actually *is* like that; well, at
least if you want boundary checks that is.)
But I suppose that since Haskell is not an OO language and its dot operator
probably means something else altogether, making it like that would probably
be difficult (and would probably require a complete syntax redesign).
> > Btw, what is logical not in Haskell?
> You're going to love this...
> It's "not". As in "not (x == y)".
Why suddenly have one operator be a comprehensible word? Were they
uninspired when they thought up that one? :P
--
- Warp
Post a reply to this message
|
 |
|  |
|  |
|
 |
|
 |
|  |
|  |
|
 |
On 02/27/10 14:39, Warp wrote:
> Darren New <dne### [at] san rr com> wrote:
>> I think c ! 2 is the third element of c, not unlike c[2].
>
> [] is used almost universally for indexing in most languages (not only
> for random access of arrays, but also eg. accessing relational sets (where
> the thing inside the square brackets is the key and the return value is the
> data)). 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]"?
>
> 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!").
>
> So why choose ! as "the nth element"? Did they run out of obfuscated
> one-character operators and it was the only one left? Or is there a logical
> reason to choose that symbol out of all possible that I'm not aware of?
> (Well, I wouldn't actually be surprised if that symbol had been used for
> that purpose in some weird mathematical notations eg. in some lambda
> calculus or the like since the 1930's or such.)
>
> 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.
Now you know how lots of people feel when they have to learn UNIX
(command line). At some point, you just tell them, "Hey, that's the way
it is. Weird, obscure, but works, and doesn't really get in your way
once you know it".
--
Depend on the rabbit's foot if you will, but remember, it didn't help
the rabbit.
Post a reply to this message
|
 |
|  |
|  |
|
 |
|
 |
|  |
|  |
|
 |
Warp <war### [at] tag povray org> wrote:
> Darren New <dne### [at] san rr com> wrote:
> > I think c ! 2 is the third element of c, not unlike c[2].
>
> [] is used almost universally for indexing in most languages (not only
> for random access of arrays, but also eg. accessing relational sets (where
> the thing inside the square brackets is the key and the return value is the
> data)). 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]"?
>
> 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!").
Like Scheme using it to warn about bad imperative constructs, like (set! v 1) :)
> So why choose ! as "the nth element"?
v!1 is briefer than v[1] :)
Post a reply to this message
|
 |
|  |
|  |
|
 |
|
 |
|  |
|  |
|
 |
>> You can't do "c at 2", but there's no reason you couldn't do "c `at` 2".
>
> If this were C++, it would actually be "c.at(2)".
OK.
> But I suppose that since Haskell is not an OO language and its dot operator
> probably means something else altogether, making it like that would probably
> be difficult (and would probably require a complete syntax redesign).
The dot can mean two different things. "x . y" means function
composition. "x.y" is a qualified name. (It means the variables "y" from
module "x".)
>> You're going to love this...
>
>> It's "not". As in "not (x == y)".
>
> Why suddenly have one operator be a comprehensible word? Were they
> uninspired when they thought up that one? :P
Heh. If you mean "why isn't it some random symbol?" then the answer is
"you can only use symbols for *binary* functions, not *unary* functions
such as NOT".
Oh, you're going to love this... The logical AND and OR functions are
"&&" and "||" respectively, but Haskell *also* has functions named "and"
and "or". They take the logical AND or OR of a list of booleans.
Hey, I said Haskell was a great language. I never said the standard
libraries were perfect. ;-) I'm sure you can probably name a few
unfortunately design choices of the C++ libraries too...
--
http://blog.orphi.me.uk/
http://www.zazzle.com/MathematicalOrchid*
Post a reply to this message
|
 |
|  |
|  |
|
 |
|
 |
|  |
|  |
|
 |
Neeum Zawan wrote:
> Now you know how lots of people feel when they have to learn UNIX
ls, cp, mv, rn and friends. Becuase, let's face it, adding two octets to
cp to make copy would be a crime...
--
http://blog.orphi.me.uk/
http://www.zazzle.com/MathematicalOrchid*
Post a reply to this message
|
 |
|  |
|  |
|
 |
|
 |
|  |
|  |
|
 |
nemesis wrote:
>> So why choose ! as "the nth element"?
>
> v!1 is briefer than v[1] :)
So is v@1. But that's not what was chosen.
Personally, I'd kinda like to be able to use v[1], but hey.
--
http://blog.orphi.me.uk/
http://www.zazzle.com/MathematicalOrchid*
Post a reply to this message
|
 |
|  |
|  |
|
 |
|
 |
|  |
|  |
|
 |
>> case size c of
>> 3 -> case (size (c ! 0), size (c ! 1), size (c ! 2)) of
>> (1, 2, 3) -> (c ! 0 ! 0) + (c ! 1 ! 0) + (c ! 2 ! 0)
>> _ -> 0
>> _ -> 0
>
> I don't know why, but I got an irresistible urge to write some faux haskell
> after seeing that.
>
> case closed in d by
> x -> case (open, not!, closed, yes!, 2) because
> (1, 2, 3) -> (one, two, three) + x
> hence -> yes
> hence -> no
>
> To me, it makes exactly as much sense. :P
It makes sense to me, but it's awfully ugly. At present I can't think of
a way to do better...
--
http://blog.orphi.me.uk/
http://www.zazzle.com/MathematicalOrchid*
Post a reply to this message
|
 |
|  |
|  |
|
 |
|
 |
|  |
|  |
|
 |
Captain Jack wrote:
> I know it's interesting to *me* because I'd never heard of it before I
> started reading this newsgroup. Always like learning something new... :-)
Heh, yeah. People claim it's the most talked-about language on the
Internet, but I'd never heard of it either until I had a random
encounter one day. And I've never looked back... ;-)
--
http://blog.orphi.me.uk/
http://www.zazzle.com/MathematicalOrchid*
Post a reply to this message
|
 |
|  |
|  |
|
 |
|
 |
|  |
|
 |