The language is an infinite set of chains that are defined by the next conditions.
Conditions:
1) The language chains may consist of symbols from the set {1,a,b}.
2) The language chains always start from subchain '1a'.
3) Every languange chain has to include at least one subchain 'aa'.
For example:
1aa, 1abaa, 1aaab, 1aab1a, ... etc.
The regular expression in formal language seems to be like this: 1a ((1+b)* a)* (a (1+b)) a (1+b+a)*
What will the correct regular grammar be for this language?
I've thought of many ways but it seems to be too complex for me. This solution isn't correct too, I guess.
G ({1,a,b}, {A,S}, P, S)
P:
S -> 1S|bS|aA
A -> 1A|bA|1a
The regular expression should be easier than you're making it; what about: 1a (1+a+b)* aa (1+a+b)*
Given this, the regular grammar is also not too bad:
S := 1R
R := aT
T := 1T
T := bT
T := aU
U := 1T
U := bT
U := aV
V := 1V
V := aV
V := bV
V := 1
V := a
V := b
Something like that ought to do it, if I didn't make a silly mistake.
Related
I have three integer values, say
h := 3.
m := 19.
s := 8.
I would like to produce the string '03:19:08'. I know how to turn a number into a string, and even pad it with a zero if necessary. So as a first pass I wrote this absolutely horrific code:
h < 10 ifTrue: [hs := '0', (h asString)] ifFalse: [hs := h asString].
m < 10 ifTrue: [ms := '0', (m asString)] ifFalse: [ms := m asString].
s < 10 ifTrue: [ss := '0', (s asString)] ifFalse: [ss := s asString].
Transcript show: hs, ':', ms, ':', ss.
Transcript nl.
Now obviously I need to clean this up and so was wondering, among other things what the most idiomatic Smalltalk approach would be here. Could it be something like (not legal Smalltalk obviously):
aCollectionWithHMS each [c | padWithZero] join ':'
I found a discussion on streams with a print method taking a separatedBy argument but wouldn't there be a simpler way to do things just with strings?
Or perhaps there is a more elegant way to pad the three components and then I could just return hs, ':', ms, ':', ss ?
Or, is there an interface to POSIX time formatting (or something similar) common to all Smalltalks? I know GNU Smalltalk can link to C but this is way too much overkill for this simple problem IMHO.
EDIT
I got a little closer:
z := {h . m . s} collect: [:c | c < 10 ifTrue: ['0', c asString] ifFalse: [c asString]].
(Transcript show: ((z at: 1), ':', (z at: 2), ':', (z at: 3))) nl.
But the direct access of collection elements makes me sad. I found a page documenting the joining method asStringWith but that method is unsupported, it seems in GNU Smalltalk.
Here is a way to do this in Pharo:
String streamContents: [:stream |
{h.m.s}
do: [:token | token printOn: stream base: 10 nDigits: 2]
separatedBy: [stream nextPut: $:]]
Explanation:
The streamContents: message answers with the contents of the WriteStream represented by the formal block argument stream.
The do:separatedBy: message enumerates the tokens h, m and s evaluating the do: block for each of them and inserting the evaluation of the second block between consecutive tokens.
The printOn:base:nDigits: message dumps on the stream the base 10 representation of the token padded to 2 digits.
If the dialect you are using doesn't have the printOn:base:nDigits: method (or any appropriate variation of it), you can do the following:
String streamContents: [:stream |
{h.m.s}
do: [:token |
token < 10 ifTrue: [stream nextPut: $0].
stream nextPutAll: token asString]
separatedBy: [stream nextPut: $:]]
Finally, if you think you will be using this a lot, I would recommend adding the message hhmmss to Time (instance side), implemented as above with self hours instead of h, etc. Then it would be a matter of sending
(Time hour: h minute: m second: s) hhmmss
assuming you have these three quantities instead of a Time object, which would be unusual. Otherwise, you would only need something like
aTime hhmmss
ADDENDUM
Here is another way that will work on any dialect:
{h.m.s}
inject: ''
into: [:r :t | | pad colon |
pad := t < 10 ifTrue: ['0'] ifFalse: [''].
colon := r isEmpty ifTrue: [''] ifFalse: [':'].
r , colon, pad, t asString]
The inject:into: method builds its result from the inject: argument (the empty String in this case) and keeps replacing the formal block argument r with the value of the previous iteration. The second formal argument t is replaced with the corresponding element of each iteration.
ADDENDUM 2
time := '00:00:00' copy.
{h asString. m asString. s asString} withIndexDo: [:t :i |
time at: i - 1 * 3 + 2 put: t last.
t size = 2 ifTrue: [time at: i - 1 * 3 + 1 put: t first]].
^time
The copy is necessary to make sure that the literal is not modified.
just a short question regarding how to use TThreadList. Is it safe to use it with "with" statement as follows:
with FEngineList.DAQEngines.LockList do
begin
try
for Idx := 0 to Count - 1 do
Items[idx].Param1 := cos(2*pi*I/Count);
...
...
finally
FEngineList.DAQEngines.UnlockList;
end;
end;
or should I explicitly do it like here:
uEngines := FEngineList.DAQEngines.LockList;
try
with uEngines do
begin
for Idx := 0 to Count - 1 do
Items[idx].Param1 := cos(2*pi*I/Count);
...
...
end;
finally
FEngineList.DAQEngines.UnlockList;
end;
Thanks!
It's upon you which variant you choose. with only tells the compiler where to get members you write in your code. So yes, it is safe, as long as you're accessing members you wanted to. It doesn't affect the runtime.
I would prefer the first way, just without that begin..end block (if I'd be forced to use with), but it's just my personal preference and you are free to write it as you wish:
with FEngineList.DAQEngines.LockList do
try
...
finally
FEngineList.DAQEngines.UnlockList;
end;
Neither variant is to be recommended. If you had to choose between these two, the former is preferable since there is no real need for an extra local variable.
However, with is to be avoided. The problem is that is introduces potential for scope overlap. If the original scope and the object that is the subject of the with have members with the same name, then the with scope hides the outer scope. This catches you out when you add a new member to the subject of the with that happens to have the same name as a member of the outer scope. At best your program won't compile. At worst it compiles and you have a defect. Quite possibly a defect that you don't readily spot.
Code it like this:
uEngines := FEngineList.DAQEngines.LockList;
try
for Idx := 0 to uEngines.Count - 1 do
uEngines.[Idx].Param1 := Cos(2*pi*Idx/uEngines.Count);
....
end;
finally
FEngineList.DAQEngines.UnlockList;
end;
Related: Is Delphi "with" keyword a bad practice?
In perl there are hashes, key-value pairs. Does Isabelle/HOL have a builtin such function with the corresponding theorems?
Typically in Isabelle/HOL, you would just use a function of type 'key ⇒ 'value. For instance:
definition "num_animals
≡ (λ_. 0)(''dog'' := 3, ''cat'' := 42, ''mouse'' := 12)"
lemma "num_animals ''dog'' = 3"
by (simp add: num_animals_def)
Here, (λ_. 0) is a function that returns 0 for all input values, while the syntax (''dog'' := 3) modifies the existing function so that the input dog returns 3.
If you want to be able to determine if a function does not contain a particular key, you can use Isabelle/HOL's option type:
definition "num_animals
≡ (λ_. None)(''dog'' := Some 3, ''cat'' := Some 42, ''mouse'' := Some 12)"
lemma "num_animals ''elephant'' = None"
by (simp add: num_animals_def)
There are numerous differences between this and a hash-table. For example, Isabelle will have to perform at least O(n) steps to look up a value in the function in this format. Also, no hashing is going on: key values are stored and compared explicitly. Usually, though, because you are reasoning about these things, and not typically trying to execute these things, this is not a problem.
If you are dealing with larger proof terms, you may need to look into other representations of functions, such as Binary Search Trees in the AFP, though these will be harder to work with.
Suppose i want to know what unions(referencing environment) are active in the point marked with (*), how do i acknowledge that ? Which unions are in fact active ?
procedure P(A,B ; real)
X: real
procedure Q(B,C : real)
y : real
...
procedure R(A,C : real)
Z:real
........ --(*)
It's basic nesting basically. But you don't specify the full blockstructure (with begin..end pairs) to fully fixate the structure.
Assuming from indentation that the begin end; block of P is at the end, and of Q and R is directly after resp. the y and z declarations, then in Q: Q is searched first, then P, then the scope above P (mainprogram/unit or another procedure), in R R, P,unit etc.
Using GNU's make, I'd like to extract the first character of a variable. Currently I'm using the shell function to have bash perform the substring. I'm wanting to know if there is a way using gmake's built-ins to do the same.
DIR=/user/$(shell echo "$${USER:0:1}")/$(USER)/
It's not very satisfying, and you'd have to add to $(INITIALS) until you were happy, but:
INITIALS := a b c d e f g h i j k l m n o p q r s t u v w x y z
U := $(strip $(foreach a,$(INITIALS),$(if $(USER:$a%=),,$a)))
DIR = /user/$(U)/$(USER)/
Perhaps the sensible approach would be to take note of the := usages in the above, and amend your simple version to DIR := ...$(shell ...)... so that the shell command is only invoked once.
http://www.gnu.org/software/make/manual/make.html#Functions is a comprehensive list of everything you can do with gmake builtins.
It does not appear to be possible to extract the first character without $(shell), unfortunately.