Re: [q-lang-users] null problemo
Brought to you by:
agraef
From: <Dr....@t-...> - 2004-08-30 20:59:10
|
Hi, Tim! Tim Haynes wrote: > I'm trying to write a little utility to read & (manipulate &) write PNM > bitmap image files. Have you considered using Q's ImageMagick module for that purpose? > Reading is OK but I've had to declare that `ord ""=0' around my reader > function[1]; I'm having serious problems writing the ascii NUL byte out - > have tried declaring that `chr 0="\0"' and other variations on a theme, but > every time I try to fwritec the thing, the file truncates prematurely; > running hexdump shows that no byte in the output is set to 00. Yes, Q strings are 0-terminated, as in C, therefore you can't have a \0 in a string (if you do, the string will be truncated there). What you probably need are byte strings. These are especially useful for encoding arbitrary binary data. See Section 12.3 (Clib -> Byte Strings) of the manual. Also see the section on binary files for operations on how to read and write such binary data from/to a file. > Footnotes: > [1] I had to do this before, as in a `where' clause it had no effect... > this also felt untidy. Note that where clauses in Q are always *variable* definitions (similar to Haskell's pattern bindings), there is no such thing like a local function definition in Q. Thus a clause like ... where ord "" = 0; will be interpreted as: "match 0 against ord "" and assign all variables in ord "" (none in this case) to their corresponding values." Which fails, because the source term 0 doesn't match the target term ord "", causing the entire rule containing that where clause to fail. More info about where clauses in Q can be found in Section 7.4 (Equations and Expression Evaluation -> Local Variables) of the manual. NB: It's not just my laziness that there are no "local equations" in Q (we discussed this on the list a while ago). There are some deep semantic difficulties with this in a language based on general term rewriting. Incidentally, your example is one such "bad usage" of a local equation: If such a local equation would be possible, it would presumably mean that the normal form of ord "" should be 0 in the context of the hosting rule (which also raises the question, which context? Dynamic? Lexical?), but should remain to be ord "" itself outside of it. Hence the normal form of ord "" wouldn't be well-defined any more, wreaking havoc on equational semantics. :( Cheers, Albert -- Dr. Albert Gr"af Dept. of Music-Informatics, University of Mainz, Germany Email: Dr....@t-..., ag...@mu... WWW: http://www.musikwissenschaft.uni-mainz.de/~ag |