If length is 2 do Haskell [closed] - haskell

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
I'm new to Haskell and I'm not sure how to work around the If-Else, for example:
function str = if ((length str) = 2) then (....)
In java we would:
if (str.length =2){
str = "2"}
else { str ="1"}
How do you write it in haskell?

You can use Guards:
fnc :: String -> String
fnc s | length s == 2 = ...
| otherwise = ...
More to Guards
Or conditions
fnc :: String -> String
fnc s = if length s == 2 then ... else ...
It is also possible to use pattern matching, more here.
There are several ways to achieve conditions (e.g. case of) in Haskell.

Related

How to read two named inegers as a tuple in native Rust? [closed]

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 months ago.
Improve this question
In Python I can do this:
a, b = map(int, input().split(" "))
... to get exactly 2 integers from user input.
How can I achive the exact same result in Rust (get 2 integers as a named tuple) without any extern crates?
For the sheer fun of it, I translated your code as literally as possible to Rust:
// replace with some read from stdin
let input = "1 2";
let (a, b) = if let &[a, b] = &input.split(' ').map(|c| c.parse::<u32>().unwrap()).collect::<Vec<_>>()[..] {
(a, b)
} else {
panic!("ValueError: too many/few values to unpack (expected 2)");
};
println!("a = {}, b = {}", a, b);

Using value itself without the name of value in Python 3 [closed]

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 2 years ago.
Improve this question
Is there any way to use variable itself inside without it's name?
For example, I have a string like this:
someStuffVariableName = "abcdefghijklmnop..."
If I want to manipulate it, I need to write every time name of this var but it's so long:
someStuffVariableName = someStuffVariableName[0:-1]
But,anyway,can I do like this:
someStuffVariableName = self[0:-1] or someStuffVariableName = this.value[0:-1]?
There is not.
Your best options are:
Use a more concise variable name (but don't give up readability!)
Just deal with the length
Note that in some cases, the answer is actually yes. For instance, you can often write x += y instead of x = x + y, and x /= y instead of x = x / y.
But this is for assignment operators only.

Haskell : Check for an exact substring [closed]

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 years ago.
Improve this question
I want to write a function which will check for an EXACT substring in the given string.
All I am using right now is isInfixOf but my friend just pointed out that it doesn't check for an EXACT word.
Example, if I am writing
`"hi " `isInfixOf` "hi you"`
then this will return True. But I don't want that. All I want is it should return True only if it contains "hi " i.e. exactly one space. How can I do that?
It seems like you are looking for a somewhat modified version of isInfixOf:
import Data.Maybe
import Data.List
isInfixOf' :: String -> String -> Bool
isInfixOf' xs ys = any p [stripPrefix xs zs | zs <- tails ys]
where
p Nothing = False
p (Just (' ' : _)) = False
p _ = True
The idea is that we first collect all strings that follow a matching substring and then test whether or not they start with a space.
For example:
> "hi " `isInfixOf'` "hi you"
False
> "hi " `isInfixOf'` "hi you"
True

Haskell: Type to represent a sudoku line? [closed]

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 years ago.
Improve this question
In Haskell, how can I create a type to represent a list of length 9 which each elements are an Int between 0 and 9?
You could use smart constructors:
module Sudoku(SudokuSquare, sudokuSquare) where
import Data.Traversable(traverse)
data SudokuSquare = SSquare Int
sudokuSquare :: Int -> Maybe SudokuSquare
sudokuSquare i = if i >= 0 && i <= 9 then Just (SSquare i) else Nothing
buildRow :: [Int] -> Maybe [SudokuSquare]
buildRow = traverse sudokuSquare

Access external variable in Haskell [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 8 years ago.
Improve this question
I am trying to convert the following pseudo-code to Haskell:
stringA = "ABCD"
stringB = "EFGH"
stringC = "ICJK"
function myFunction(String x) {
otherFunction(x)
}
Now, in Haskell I have
stringA = "ABCD";
stringB = "EFGH";
stringC = "ICJK";
test :: Int
test x = if x == 1 then otherFunction(??) else ...
How can I ensure that otherFunction takes stringA as a paramter when test is called with x = "stringA"?
Thanks! :)
test :: Int
test x = if x == 1 then otherFunction stringA else ...
Of course, this is wrong, because test takes a parameter, so it's type must always contain (at least) one (->). But that's not the issue at hand. Strangely you've claimed that your pseudocode function takes a String parameter, which would look like test :: String -> ... in Haskell. But you're clearly giving it an Int as its first parameter, meaning its type should be test :: Int -> ...
Here's my translation of your pseudocode:
stringA = "ABCD"
stringB = "EFGH"
stringC = "ICJK"
test x = otherFunction x
test "stringA" = otherFunction stringA
test "stringB" = otherFunction stringB
test "stringB" = otherFunction stringB
-- etc...
As you can imagine this will be a pain to do for more than 3 or 4 cases. What about storing your strings as key/value pairs in a list?
test strIn = (liftM otherFunction) lookup strIn dict
where dict =
[("stringA", "ABCD"), ("stringB", "EFGH"), ("stringC", "ICJK")]
In general there is no way to convert a string to a function reference at runtime.

Resources