Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
Hello I have this String
"(33,18,109)"
How do I extract the integers from this string to apply functions on them ?
Thank you.
The most straight forward way is to use read "(33,18,109)" :: (Int,Int,Int), but this is not considered a good way since it does not handle failure (for example read "(21,3,not valid)" and (21,43) would result in error.
A better way is to use the readMaybe "(1,2,3) :: Maybe (Int,Int,Int)". The result uses a Maybe to represent possible failure:
import Text.Read
ans :: String -> Maybe (Int,Int,Int)
ans = readMaybe
ans "(12,53,29)" == Just (12,53,29)
ans "not a valid string" == Nothing
However, you might want to see how you ended up with a string in the first place. Is it because of performing IO operations (e.g. reading from a file)? If not, usually you don't have to directly deal with a string representation of something then operate on it (since this violates the layers of abstraction).
Also, there is a topic called Parser Combinator that deals with parsing in Haskell. But this is hugely irrelevant if your task is to only parse 3 numbers instead of more complicated things like parsing arithmetic expressions (e.g. 1+2*3).
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 months ago.
Improve this question
In the case of a it works. However in case of `b it doesn't. Why does this happen?
a = [0,1]
b = [0,1]
any(map(lambda _:a.append(sum(a[-2:])), range(2,8)))
map(lambda _:b.append(sum(b[-2:])), range(2,8))
print(a)
print(b)
This is extremely obfuscated code. Nobody would write code like this without an ulterior motive (such as Code Golf, or trying to wedge everything onto a single line), as it's doing a whole bunch of strange things that can much more simply be done some other way.
To address the thing you're asking about, any is called to consume all the values being produced by map. The return value of the map call is an iterator that lazily invokes the function it was given (a lambda expression) on successive values of the iterator it was given (a range). However, this code doesn't actually care about the values, which are all None. It wants the lambda function to be run for its side effects, which are appending values to the a or b lists. All of the values are None, since that's what list.append returns. Because None is falsey, the any function consumes all of them without stopping early (as it would do for any truthy value).
A much more natural way to write this code would be:
a = [0, 1]
for _ in range(2, 8):
a.append(sum(a[-2:]))
Here it is much more clear that we don't care about the return values from a.append. That's because that method is something we're calling to modify a in place.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 months ago.
Improve this question
I need to extract substring. I have found that unlike Python, it is not possible to use indices to extract a substring from a string variable. I have also found that MATLAB does not allow use of negative indices to access elements at end of a vector like Python does.
Looking into MATLAB documentation I have found this function:
https://uk.mathworks.com/help/stateflow/ref/substr.html
However, when I enter this into the MATLAB, I get this:
>> substr
Unrecognized function or variable 'substr'.
I am using MATLAB R2022a. However, this function is not recognized. What could be the reason for this?
Also, are strings in MATLAB closer to the tedious strings of C language and not like the convinient entity they are in Python?
The documentation of substr says:
The operator substr is supported only in Stateflow® charts that use C
as the action language.
so this is not applicable in your case.
It is possible to get substrings of a string using indices. For example, you could use:
test_str = 'thisismyteststring'; % character array
test_str(9:12) % yields 'test'
test_str = "thisismyteststring"; % string array
extractBetween(test_str, 9, 12) % yields "test"
For more information on string handling, check out the documentation here.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
I am new to Haskell. I have a function as:
readData = (map read) . words
I need to match certain constraints and accordingly return data. Is there any way I can do this. Thanks.
I'm going to assume, as seems likely, this function takes a string, splits it into words, and then parses them into integers and you need those integers to be restricted to between 2 and 10,000.
So you can just wrap a check around the list you're already producing. However, to do that you're going to need to change to explicit parameters so that you can talk about them.
readData input = map read (words input)
Now we can filter it
readData input = filter (\n -> n >= 2 && n <= 10000) $ map read (words input)
So any out of range numbers just get dropped from the list.
It's best to remember that while point-free style (not using explicit parameters) can be really nice, it's often not possible to express more complex logic with it. I tend to start out by writing my functions with parameters and then taking them out if I realise I can, and if the resulting code looks easier to understand.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
# Change this program to output each letter of the text entered on a separate line
# with space in front of each letter, increasing to the right for each letter.
# For example, 'Python' input should output:
# P
# y
# t
# h
# o
# n
text = input('Enter text: ')
for letter in text:
print(letter)
I already tried to look online for the solution, there are none.
This code is for homework but i cant figure it out help wold be appreciated
As you haven't told us what you have tried to do so far, or what you have tried to learn to figure it out, I'm not going to post the Python code. Instead, lets think about what the program should be doing. Your assignment statement gives a broad overview, but as a programmer you need to take this overview and turn it into a set of smaller instructions. These smaller steps do not have to be in code. They can be in whatever form you like, even plain english.
For functional analysis (which is what you are doing for this problem) start with the inputs and outputs then fill in the stuff in-between.
1) Input: a string
X) Output: multiple lines with a single character and whitespace
Now how do you want to get from 1 to X. You already have the code to loop through each letter and print it, but you are missing two things to get to your required output.
A) way to place a string on a new line
B) way to add whitespace to a string
I'll give you a couple of hints. A) is something that is extremely common in almost any programming language. In fact, it is done the exact same way is any language that you are likely to use. Another hint. Your final output will be a single string that spans multiple lines. Ask yourself how a word processor or text editor gives works with blank lines.
B is a little more tricky, as there are a couple of nifty Python tricks that makes it simpler to do than in other languages. I'm guessing that you already know what happens when you add two numbers together such as 3 + 5. But what happens when you add two strings together such as "foo" + "bar". Notice how the behavior for adding numbers with the + operator is completely different than the behavior is for adding strings together with the same operator? That difference in behavior applies to other common operators as well. Play around with the other three common mathematical operators on both string and numbers to see what happens
This question already has answers here:
Closed 13 years ago.
Possible Duplicate:
Open file in ML(SMLNJ)
I have a string value which has value like this:
"[(1,2,3),(2,3),(6,8)]" -> string
but I want to have these values in int type like this:
[(1,2,3),(2,3),(6,8)] -> (int list) list
what should I do?is there any function which can help me? or I have to do it myself?
This problem is perfectly suited to parsing combinators, which you can steal from my friend Greg Morrisett at Harvard.
If you want to understand the underlying ideas, read Graham Hutton's paper Higher-Order Functions for Parsing. If you want to know how to implement I/O in Standard ML, consult the TextIO module in the Standard Basis Library. If you want someone to write the code for you, you may have reached the wrong web site.
You can use stringint from the String module.
See for example http://wwwcgi.rdg.ac.uk:8081/cgi-bin/cgiwrap/wsi14/poplog/ml/help/string#Character%20Transformation%20Functions