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 9 years ago.
Improve this question
I am looking for a way that when input a Char, then the function could match the list, like "QAZXSWEDCVFRTGBNHYUJMKIOPL", and then return the index of the matching one
For example, when input 'S', then it could find the 'S' is the 5th element in the list then return integer 4 (starting with 0).
You're looking for the function elemIndex from Data.List:
elemIndex :: Eq a => a -> [a] -> Maybe Int
-- Example
main = do
let i = elemIndex 'S' "QZAXSWED"
case i of
Just idx -> print idx
Nothing -> putStrLn "'S' not found in list"
There's also the function elemIndices that returns all the indices of an element in a list, but it's going to take longer to run since it has to scan the entire list every time.
Prelude Data.List> elemIndex 'S' "QAZXSWEDCVFRTGBNHYUJMKIOPL"
Just 4
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 2 years ago.
Improve this question
type 'a tree = Leaf of 'a
|Fork of 'a tree * 'a tree
can someone show the right way to do this?
The compiler is telling you the following. You declared Fork like this:
| Fork of 'a tree * 'a tree
(Fixing a typo in your code btw.)
This indeed has two parameters, each of type 'a tree.
If you look at any of your Fork constructors you'll see that you're giving them three values. Here's one from the middle of your expression:
Fork(4, Leaf, Leaf)
You either need to change your definition of Fork or you need to supply only two values when using the Fork constructor.
If you want values in your internal nodes you should modify your definition of Fork to include a value (of type 'a presumably). If the values are all supposed to be at the leaves, you need to change your expression.
Update
Here is a working definition of a tree using your current definitions:
# let (t: int tree) = Fork (Leaf 1, Leaf 2);;
val t : int tree = Fork (Leaf 1, Leaf 2)
I should also point out that you're using Leaf with zero parameters, but you have declared it to take one parameter (as in my example 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 8 years ago.
Improve this question
I'm just playing with C++ sort & lambda and wondering if it would possible to sort elements by sum?
For example:
std::sort(my_vector.begin(), my_vector.end(), [&, sum](int a, int b){
return a + b == sum; });
I tried the method above, it gives a runtime error: "Expression: Invalid operator <".
Update1
The example:
Sum = 4
1 ,2 ,5, 3, 2
I need to group elements the sum of both would equal Sum e.g 4
So it must be grouped like so:
1,3,2,2,5
Can anybody suggest an alternative solution? I'm just lazy to achieve it using a dumb iteration.
Thanks.
std::sort requires a predicate that compares elements. That predicate must establish strict weak ordering, one important property of which is if pred(a, b) returns true, pred(b, a) must return false. Your lambda does not satisfy this property.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 8 years ago.
Improve this question
I'm new to Haskell and need an function to intersect two lists, but remove double elements.
If I load the Data.List
import Data.List
I can use this function:
intersect "abbcd" "abbe"
"abb"
The outcome is "abb", but what I need is:
intersectFunction "abbcd" "abbe"
"ab"
So the double char from the lists should display only once.
Any ideas?
perhaps the "unique" function named nub can help:
import Data.List
intersectFunction a b = nub $ intersect a b
You can use the intersect function you have, but then you need to remove the duplicate elements. It seems likely that there's a library function to do this, but how can we find it if we don't know the name?
The best way to answer questions of the form "Is there a Haskell function to do X" is usually:
Figure out what the type signature of the function you want would be.
In this case, we want a function that takes a list and produces a list. So the type signature is [a] -> [a]
Search hoogle or hayoo. If you don't find it in one, try the other. Usually it doesn't matter much whether you get the order of the input parameters exactly right, but you may need to experiment.
In this case, there are quite a few functions with that type signature. But if you go to the second page of results, you'll find the nub function, which does what you need.
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 9 years ago.
Improve this question
I have a box made up of a list of lists in Scala and I need to check if a particular cell contains an element that is not 0 or a 0 (given the row and column index to access that cell). If the element found is not a 0, an empty set should be returned else another value which I managed to code should be returned. How can I check if a particular cell contains 0 or not?
Code so far:
if(//cell does not contain 0)
Set()
else
{
//this part of code is ready
}
as mentioned in a comment, just use box(i)(j).
Moreover, I would suggest to create at least a type alias for your box
type Box = List[List[Int]]
or a case class if you want to add methods like getValue(row: Int, col: Int) : Int
case class Box(val l: List[List[Int]])
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 9 years ago.
Improve this question
In Haskell, I am going to construct 2 list of chars; one is the 26 chars in the alphabet by the original(A,B,C,D,....Z), all in uppercase. And the other one is the same list but the order of letters is changed, like(B,H,A,I......S). And now I am also going to make a substitution between these two lists, such as when the input is B then returns H, C returns A.
Can anyone could help me out of this?
List literals in Haskell use square brackets [], first of all.
Second of all, I don't know the wording of your assignment, but free of constraint, I would use an association list instead of two lists. An association list is a list of pairs of the form [(a,b)]. The key operation on an association list lookup, defined in Data.List. Look at the type signature and see if you figure out what it does.
If you start with two lists, you can zip them up.
import Data.List
import Data.Maybe
codec = zip "ABCDEF..." "BHAI..." -- String ~ [Char]
encode = map $ flip lookup codec
plainText = "The secret fox"
encodedText = encode plainText
Notice that this gives you a list of [Maybe Char]. I leave it to you to figure out how to extract the chars, since it's actually a design choice. (Do you want to just omit characters that don't show up in the codex, or insert a '!' or something so that the user knows data has been lost? Lots of options here. Notice how the Maybe monad is forcing you to explicitly handle this case).
Assume that the modified alphabet is:
"ORXBMDTCIGJYKAVLSWFNUQEHZP"
The code should look like:
import Data.List
import Data.Maybe
alphabet = ['A'..'Z']
mapping = zip alphabet "ORXBMDTCIGJYKAVLSWFNUQEHZP"
according letter = (snd . fromJust . (find (\t -> f t letter))) mapping
where
f (x, y) letter = x == letter
main = print $ according 'H'
mapping would create the correspondence between alphabet and a modified version. And according would return letter from a modified, based on the original one.