Re: [q-lang-users] This feels like a dumb question, but...
Brought to you by:
agraef
From: Andrew B. <and...@ya...> - 2006-11-24 23:48:04
|
=0AAlbert Graef wrote:=0A>=0A>> =0A>> -f F =3D true if (close (open F 0 0))= =3D ();=0A>> =3D false otherwise;=0A>=0A>Note that -f is not a val= id function name in Q. What you actually define=0A>here is a rule for unary= minus applied to the constructor term f F.=0A>=0AI had recognized it as an= invalid function name already, but I rather liked the look of "-f" rather = than "_f" or (eww) "is_regular_file". And besides, the interpreter let me = get away with it!=0A=0A>> I understand that when the file does not exist, t= he "(close (open F 0 =0A>> 0))" part does not reduce, but why does "=3D ()= " part not look at =0A>> "close (open "fetch.qaz" 0 0)" and say, "nope, no= t equal"? "eq" does =0A>> the same thing. Is there something that will f= orce the equality =0A>> check to happen?=0A>=0A>eq and (=3D) are different= things in Q. The former checks for syntactic=0A>equality (which is always = defined), the latter is a comparison operator=0A>which only applies to cert= ain data types (much like (<) and (<), etc.).=0A>See Section 7.2 of the man= ual,=0A>http://q-lang.sourceforge.net/qdoc/qdoc_7.html#SEC27=0A>=0A>Of cour= se, you can add your own rules for (=3D) to extend equality to any=0A>data = types you need, but it is generally undesirable to have a very=0A>broad def= inition of (=3D) as such rules might interfere with the specific=0A>cases.= =0A=0AI (think that I) understand the =3D vs eq difference. I was surprise= d that when I tried to apply either one to an expression where the "open F = 0 0" had failed, all I got was a bigger expression.=0A=0A>So you'll have to= use eq instead. Or you could use a pattern match like so:=0A>=0A>-f F =3D = true where () =3D close (open F 0 0);=0A> =3D false otherwise;=0A=0A= Aah, that's probably the version I was looking for. So, if the evaluation = fails in a "where" clause, the containing equation fails also. Somehow I d= id not manage to learn that from my reading of the manual.=0A=0A>But I thin= k that the most convenient way to do these checks would be to=0A>use the st= at/lstat functions from clib. For instance:=0A=0A... snip a bunch...=0A=0AI= t's kind of amusing, I started out trying to write this way, but the only v= ersion of stat that I could find in the manual seemed to take a FD, which r= equired calling "open" anyhow, so since I only needed "-f" just then I sett= led on a really dumb implementation.=0A=0AThanks for the advice,=0A=0A-andr= ew=0A=0A=0A=0A=0A |