How to pack multiple statements - autoconf

I'd like to regroup multiple statements in a []:
AC_ARG_WITH(
[float],
[AS_HELP_STRING(
[--with-float],
[use float instead of doubles to store polynoms coefficients])],
[real=float], <--- here I'd like to add an AC_DEFINE
[real=double])
My problem is, I'm not sure of the best way to do that. I guess I can use ;, but this does not seem really idiomatic for a m4sh script.

Use newlines:
AC_ARG_WITH(
[float],
[AS_HELP_STRING(
[--with-float],
[use float instead of doubles to store polynoms coefficients])],
[real=float]
[AC_DEFINE....],
[real=double])
It's also common to write it as:
AC_ARG_WITH([
float
],[
AS_HELP_STRING(
[--with-float],
[use float instead of doubles to store polynoms coefficients])
],[
real=float
AC_DEFINE....
],[
real=double
])

Related

IO Fortran format command issue [duplicate]

I would like to have a Fortran write statement formatted to depend on some variable. For example, I could write:
write(*,'(3f15.3,3f9.2)') x,y,z,(var(i),i=1,nvari)
where nvari = 3. But, what if, in some cases, I actually have 4 variables (i.e. nvari = 4). I would like to write something like this:
write(*,'(3f15.3,nvari(f9.2))') x,y,z,(var(i),i=1,nvari)
Now, nvari can be anything and the output will work as I like. How can I make something like this work?
If you are using Intel fortran, it has a proprietary extension for this -- you can include an existing variable in angle brackets to act as a specifier:
write(*,'(3f15.3,<nvari>f9.2)') x,y,z,(var(i),i=1,nvari)
If you compiler supports it, '(3f15.3, *(f9.2))'
If you have an older compiler, just use a larger number than you will have items to output, e.g., '(3f15.3, 999(f9.2))'. You don't have to use up the format.
For the most complicated cases you can write a format to a string and use that as your format:
write (string, '( "(3f15.3, ", I4, "(f9.2))" )' ) nvari
write (*, string ) x,y,z, (array(i), i=1,nvari)
With the understanding of formats, including format reversion, the use of string formats is rarely necessary.
Instead of writing the format directly in the write statement, it's also possible to use a character variable.
character(len=32) :: my_fmt
my_fmt = '(3f15.3,3f9.2)'
write(*, my_fmt) x, y, z, (var(i), i = 1, nvari)
Now it is possible to manipulate the character variable to contain the wanted repeat count before the write statement, using a so-called internal write, or write to internal file.
write(my_fmt, '(a, i0, a)') '(3f15.3,', nvari, 'f9.2)'
(Just make sure the declared length of my_fmt is long enough to contain the entire character string.)
You wanted to write something like this:
write(*,'(3f15.3,nvari(f9.2))') x, y, z, (var(i), i=1,nvari)
In fact, there is an old trick in the Fortran standard that allows you to omit the nvari, thus:
write(*,'(3f15.3,(f9.2))') x, y, z, (var(i), i=1,nvari)
or even thus:
write(*,'(3f15.3,f9.2)') x, y, z, (var(i), i=1,nvari)
The standard says that the last descriptor in the format is implicitly repeated as often as is necessary to accommodate all of the variables in the list. That 'last descriptor' could be parenthesized such that the last group of descriptors is implicitly repeated, for example:
write(*,'(3f15.3,(2x,f9.2))') x, y, z, (var(i), i=1,nvari)

Problem with the output of an exponential calculation

I tried to calculate an exponential calculation, I tried both ways, writing it with the normal ** operator and the pow() function.
If I directly use numbers, everything works completely fine. But as soon as I use variables which get their value from input() functions the result is rounded, although I use the float argument.
I am quite new to coding, so please don't go to hard on me.
Code below:
pow(1.05,5), everything fine, result is 1.2762815625 and so on
float(pow(int(a),int(b)) the result is just 1.0, although it should be the same as above.
it is because you are doing the power operation on int i.e first you are converting the 1.05 to 1 by doing int(1.05) and then calculating the power(). you need to apply pow() on float()
print(float(pow(float(1.05),float(5))))
or either
print(pow(float(1.05),float(5)))
The reason the float function returns 1.0 is because of how it deals with integers.
For example:
integer = 5.5236734
print(float(integer))
The above code's output will be:
5.5236734
Now lets say you make some changes:
integer = 5.5236734
print(int(integer))
First, we made integer a decimal number, and then we said to print the int() form of the decimal.
This will be the result:
5
So, to fix your code, you just need to do this:
a = 1.05
b = 5
print(float(pow(a, b)))
Which will output:
1.2762815625000004
Hope this helps!

how to enable string and numeric comparison temporarily?

Given this simplified example to sort:
l = [10, '0foo', 2.5, 'foo', 'bar']
I want to sort l so that numeric is always before strings. In this case, I'd like to get [2.5, 10, '0foo', 'foo', 'bar']. Is it possible make numeric and string temporarily comparable (with strings always larger than numeric)?
Note it is not easy to provide a key function to sorted if you are thinking about it. For example, converting numeric to string won't work because "10" < "2.5".
A way that you might do this does involve passing a key to sorted. it looks like this:
sorted(l, key=lambda x:(isinstance(x str), x))
This works because the key returns a tuple with the type of x and its value. Because of the way tuples are compared. Items at index 0 are compared first and if it they are the same, then next two items are compared and so on if they also are the same. This allows for the values to be sorted by type (string or not a string), then value if they are a similar type.
A more robust solution that also can handle further types might use a dictionary in the key function like this:
sorted(l,key=lambda x:({int:0, float:0, str:1, list:2, set:3}[type(x)], x))
further types can be added as necessary.

Erlang: DRYing up string+binary concatenation

I am currently forming strings from strings and binaries like this:
X = string:join(io_lib:format("~s~s~s", ["something1", "something2",<<"something3">>]), "") %X is now something1something2something3
This seems painful and messy. Because in order to dry this up with another such string with a different number of "~n":
Y = string:join(io_lib:format("~s~s", ["something1", <<"something2">>]), "")
I essentially have to write a function that counts the size of the argument list, and forms ~n[that many times] and plugs it into this.
Is there a better way to be doing this?
Eshell V8.0.2 (abort with ^G)
1> F = <<"asdf">>,
1> string:join(io_lib:format("~s~s~s", ["something1", "something2", F]),"").
"something1something2asdf"
2> lists:flatten(["something1", "something2", F]).
[115,111,109,101,116,104,105,110,103,49,115,111,109,101,116,
104,105,110,103,50,<<"asdf">>]
3>
I'm confused as to why you need the call to io_lib:format at all. It's not doing any work in this case.
string:join(["something1","something2","something3"], "").
Would give you the same result. You can simplify even further if there's really no separator character (and taking advantage of the fact that strings are just lists in Erlang):
lists:flatten(["something1", "something2", "something3"]).
Update
I see now that you're working with a list of different data types. While a one-liner may look pretty, you can see that they're not always flexible. In your case, I would create some mapper functions to handle mapping different types to strings. Maybe something like:
-module(string_utils).
-export([concat/1]).
to_string(Value) when is_binary(Value) -> binary_to_list(Value);
to_string(Value) -> Value.
concat(List) ->
lists:flatten(lists:map(fun to_string/1, List)).
And then your calling code would be:
string_utils:concat(["something1", "something2", <<"something3">>]).

Using perfectly formatted input as list in Haskell

I'm doing a program in Haskell (on the Haskell platform), and I know I'm getting perfectly formatted inputs, so the input may look like
[ ['a'], ['b'], ['c'] ]
I want Haskell to be able to take this and use it as a list of it's own. And, I'd like this list to be over multiple lines, i.e., I want this to also work:
[
['a'],
['b'],
['c']
]
I can parse this input, but I've been told there's a way to do this easily - this is supposed to be the 'trivial' part of the assignment, but I don't understand it.
read "[ ['a'], ['b'], ['c'] ]" :: [[Char]]
will return [ ['a'], ['b'], ['c'] ]. If you assign the result of read to a variable that can be inferred to be of type [[Char]], you don't need the :: [[Char]] bit.
There is an instance of the Read class for Haskell lists, meaning you can use the read function to effectively parse strings formatted like Haskell lists, which is exactly what you have.

Resources