Haskell "let" syntax in a way that it spans multiple rows - haskell

Is it possible to write Haskell's "let" statement in a way that it spans multiple rows ?
let a = " something in this row.
something else in this row "
Or else , is there any other way to create a string which spans multiple rows ?

To break up a string literal across multiple lines, use a string break like this:
let a = " something in this row.\
\something else in this row\
\ and more in this row\
\ and yet more in this row "
You put a backslash at the end of each line you want to continue, and then another one at the start of the next line where the text will continue from.

Yes. You can use multiple row expression if you use right indention.
For example,
multiLine = let a = if 1 == 2
then True else False
in a

Related

Excel returning a False response

=IF(B12<>"",(IF(U12="Subscription","Subscribe",IF(U12="T.E.H.","Subscribe",IF(U12="ESA","Subscribe",IF(U12="Perpetual","Buy",IF(U12=" "," ")))))))
I get a "False" value in the cell with this formula in it when the value in U12 is not one of the options and I want to get a blank
Appreciate any help
Thanks
Change this: IF(U12=" ", " ") to this: IF(U12=" ", " ", ""). Here's a demo in Google Sheets, but the formulas are the same.
https://docs.google.com/spreadsheets/d/10iFqq0PNt9VhGKMPLCxsI1df0x4nDSxCaCGSdEEktO8/edit#gid=0
However, I don't think this is exactly right. For one thing, it looks like this clause:
IF(U12=" ", " ")
is only there to try to generate the blank you are looking for. If that's the case, then change it to:
IF(U12=" ", " ", "")
However, I think I would probably use some array constants, something like this:
=IF(B12<>"", CHOOSE(SUM(--(U12={"Subscription","T.E.H","ESA"})) + IF(U12="Perpetual", 2) +1, "", "Subscribe", "Buy"))
To break this down a little, let me reformat so it's easier to see:
=IF(
B12 <> "",
CHOOSE(
SUM(--(U12={"Subscription","T.E.H","ESA"})) +
IF(U12="Perpetual", 2) + 1
"", "Subscribe", "Buy"
)
)
I'm going to use CHOOSE, which takes an index number N and a list of choices, and it returns the Nth element in the choice list. To generate the index, I'm going to use a couple of techniques. First look at this:
U12 = {"Subscription", "T.E.H", "ESA"})
This is going to test U12 against all of the choices in the list, and return an array of TRUE/FALSE values. If U12 matches any of the elements in the array constant, then one of those values will be TRUE. We use -- to coerce that array from TRUE/FALSE to 0/1, and then we use SUM to get the array into a single value. It will either be 0 if U12 doesn't match any of the options, or 1 if it does.
Then I'm going to use a standard IF to check if U12 = Perpetual, and return 2 if it does. Adding that result to the previous sum will give us a final number that is either 0, 1 or 2.
CHOOSE is 1 indexed, meaning that it expects the index to be 1 or greater, so we add 1 to the number we just generated and pass it to CHOOSE along with a list of options.
The advantage here is that if the Subscribe options change you can just change the list, instead of having to alter a bunch of nested IFs. Also, if you need to support multiple buy options, you can do it with a similar construction:
SUM(--(U12 = {"Subscription", "T.E.H", "ESA"})) +
IF(SUM(--(U12 = {"Perpetual", "Buy Now"})), 2) + 1
and if you needed to add return values just keep extending:
CHOOSE(
SUM(--(U12 = {"Subscription", "T.E.H", "ESA"})) +
IF(SUM(--(U12 = {"Perpetual", "Buy Now"})), 2) +
IF(SUM(--(U12 = {"Release", "Dropping"})), 3) + 1,
"", "Subscribe", "Buy", "Sell"
)
If you think this is something that might need to be maintained or the options might change, I would set it up like this, as opposed to nested IFs.
Is this a little clearer?
=IF(B12=""," ",IfError(VLookup(U12,{"ESA","Subscribe";"Perpetual","Buy";"Subscription",Subscribe";"T.E.H.","Subscribe"},2,false)," "))
This is really just a table lookup, hence the use of VLookup. The lookup array is a two dimensional array in curly brackets - the commas and semicolons are very important for defining a 2x4 array for VLookup. The IFError function tells how to return your desired " " result if the lookup fails.

Python: Insert space between every element in a list with one line description in for loop

I am trying to write a simple Encrype and Decrype system. I have a syntax question like the topic above, please take a look.
def en_num(pw):
en1 = [int(x) for x in pw]
for i in en1:
numstr = "".join(bin(int(i))[2:])
numstr += " "
return numstr
For example, input is "1 2", the output will be "1 10"
This can geve me the right output, however, I am trying to write this for loop in one line, like this
def en_num(pw):
en1 = [int(x) for x in pw]
numstr = "".join(bin(int(i))[2:] for i in en1)
return numstr
I don't know how to add the space between in this syntax, the result is "110"
Please take a look, thanks!
Try adding a space between the quotes on your join statement:
numstr = " ".join(bin(int(i))[2:] for i in en1)
That will separate each number.

how to find two different strings in the same line in matlab

I have a cell obtained from text scan and I want to find the index of lines containing particular string,
fid = fopen('data.txt');
E = textscan(fid, '%s', 'Delimiter', '\n');
and I wanted to know the line numbers (index) of those lines which have a specific text, e.g. I wanted to find the rows that have the keyword "2016":
rows = find(contains(E{1},"2016" );
but I want to find the index of those lines which have two keywords "2016" and "Mathew Perry" (only those lines which have both the keywords).
I tried using this code but does not work
rows = find(contains(E{1},"2016" && contains(E{1},"Mathew Perry");
the error I get is:
Operands to the || and && operators must be convertible to logical scalar values.
To find a single string:
idx = strfind(E{1}, '2016');
idx = find(not(cellfun('isempty', idx)));
Use strfind instead of find. YOu may try the above with and/or. If it works fine, then no problem; if not, get the indices separately for each word and get the intersection of the indices.

Lua Carrying over a value through line wrap code

Previously, I received help in the following link:
Lua Line Wrapping excluding certain characters
Short description of the above is that I was looking for a way to be able run a line wrap function while ignoring character count of certain characters.
Now I've come across another issue. I want to be able to carry the last colour code over to the new line. For example:
If this line #Rwere over 79 characters, I would want to #Breturn the last known colour code #Yon the line break.
Running the function I have in mind would result in:
If this line #Rwere over 79 characters, I would want to #Breturn the last known
#Bcolour code #Yon the line break.
instead of
If this line #Rwere over 79 characters, I would want to #Breturn the last known
colour code #Yon the line break.
I wish for it to do so because in many cases, the MUD will default back to the #w colour code, so it would make colourizing text rather difficult.
I've figured the easiest way to do that would be a reverse match, so I've written a reverse_text function:
function reverse_text(str)
local text = {}
for word in str:gmatch("[^%s]+") do
table.insert(text, 1, word)
end
return table.concat(text, " ")
end
and it turns:
#GThis #Yis #Ba #Mtest.
to
#Mtest. #Ba #Yis #GThis
The issue I'm running into with creating the string.match is the fact that colour codes can be in one of two formats:
#%a or #x%d%d%d
Additionally, I don't want it to return a colour code that doesn't colour, which is indicated as:
##%a or ##x%d%d%d
What's the best way to accomplish my end goal without compromising my requirements?
function wrap(str, limit, indent, indent1)
indent = indent or ""
indent1 = indent1 or indent
limit = limit or 79
local here = 1-#indent1
local last_color = ''
return indent1..str:gsub("(%s+)()(%S+)()",
function(sp, st, word, fi)
local delta = 0
local color_before_current_word = last_color
word:gsub('()#([#%a])',
function(pos, c)
if c == '#' then
delta = delta + 1
elseif c == 'x' then
delta = delta + 5
last_color = word:sub(pos, pos+4)
else
delta = delta + 2
last_color = word:sub(pos, pos+1)
end
end)
here = here + delta
if fi-here > limit then
here = st - #indent + delta
return "\n"..indent..color_before_current_word..word
end
end)
end

printf() - Insufficient arguments error

I'm trying to read an input from a inputdialog and put them in a printf() command.
p.e.
inputdialog variable:
b = "this is my number list %d and %02f"
1)
First I don't know how many "%" items there are in "b".
I want to count them by counting all characters and removing all "%" items and count the difference (= nr. of "%" items):
let totlength = strlen(b)
let tempsubst = strlen(substitute(b, '%\ze\S', "","g"))
let NrPercentages = totlengte - tempsubst
I can't find out what is the right regex to substitute all characters which aren't '%\ze\S' (the negative way)
Can anyone help me?
2)
If I know the number of "%" items from the inputdialog field I can create my printf() for a certain range "i"
let nrOfi = 'i'
if NrPercentages > 0
let nrOfi = nrOfi.repeat(',i', NrPercentages-1)
endif
for i in range(1,10,2)
put=printf(''.b.'',eval(nrOfi))
endfor
This gives an error.. Insufficient arguments for printf()
What did I wrong?
For the first point, you're looking for this:
let tempsubst = strlen(substitute(b, '[^%]', "","g"))

Resources