Re: [q-lang-users] This feels like a dumb question, but...
Brought to you by:
agraef
From: Albert G. <Dr....@t-...> - 2006-11-24 08:20:15
|
Andrew Berg wrote: > ...I'd sure appreciate a bit of help. > > I'm trying to write some file test functions, like the -f and -d > functions in perl or shell scripts. Here is what I have so far: > > -f F = true if (close (open F 0 0)) = (); > = false otherwise; Note that -f is not a valid function name in Q. What you actually define here is a rule for unary minus applied to the constructor term f F. > I understand that when the file does not exist, the "(close (open F 0 > 0))" part does not reduce, but why does "= ()" part not look at > "close (open "fetch.qaz" 0 0)" and say, "nope, not equal"? "eq" does > the same thing. Is there something that will force the equality > check to happen? eq and (=) are different things in Q. The former checks for syntactic equality (which is always defined), the latter is a comparison operator which only applies to certain data types (much like (<) and (<), etc.). See Section 7.2 of the manual, http://q-lang.sourceforge.net/qdoc/qdoc_7.html#SEC27 Of course, you can add your own rules for (=) to extend equality to any data types you need, but it is generally undesirable to have a very broad definition of (=) as such rules might interfere with the specific cases. So you'll have to use eq instead. Or you could use a pattern match like so: -f F = true where () = close (open F 0 0); = false otherwise; But I think that the most convenient way to do these checks would be to use the stat/lstat functions from clib. For instance: ftype NAME = MODE and S_IFMT where MODE:Int = st_mode (stat NAME); = -1 otherwise; This will give you the type of the file if it exists (which can then be tested against the corresponding constants in clib.q to implement the specific checks that you need), and -1 otherwise. ==> hex ==> (S_IFBLK, S_IFCHR, S_IFIFO, S_IFREG, S_IFDIR, S_IFLNK, S_IFSOCK) (0x6000,0x2000,0x1000,0x8000,0x4000,0xa000,0xc000) ==> ftype "doesnt-exist" -0x1 ==> ftype "test.q" // a regular file 0x8000 ==> ftype "src" // a directory 0x4000 Greetings, Albert -- Dr. Albert Gr"af Dept. of Music-Informatics, University of Mainz, Germany Email: Dr....@t-..., ag...@mu... WWW: http://www.musikinformatik.uni-mainz.de/ag |