Haskell intersect two lists and remove double elements [closed] - list

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.

Related

Reading automatically generated documentation for DOLFIN c++ library [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 3 years ago.
Improve this question
I am trying to read the documentation of the DOLFIN c++ library for finite element modelling located on this link:
https://fenicsproject.org/olddocs/dolfin/1.3.0/python/programmers-reference/index.html
but the documentation is hard to read, so for someone without c++ knowledge how you will read the following specification of parameters for the c++ method eval_cell() of the Expression class (https://fenicsproject.org/docs/dolfin/2017.2.0/python/programmers-reference/cpp/function/Expression.html):
Parameters:
double > & values (Array<) – (Array<double>) The values at the point.
Array< double > & x (const) – (Array<double>) The coordinates of the point.
ufc::cell & cell (const) – (ufc::cell) The cell which contains the given point.
After taking a look at the page t.niese linked in the comments I think this is a automatically generated documentation, with a really bad generator (like really really bad).
So, if we fix the butchered first line, realign some braces here and there and fix the position of const it might become clearer:
Parameters
const Array<double>& values1 – The values at the point.
const Array<double> &x – The coordinates of the point.
const ufc::cell &cell – The cell which contains the given point.
Meaning
You are dealing with a function that takes three parameters, the first and second are of type Array<double>, which seems to be generic container. The third parameter is of type ufc::cell, whatever this is. All three parameters are passed by reference (see the & before each variable name) and not by value. But they are not just passed as reference but actually as const reference (see the const), meaning that the function can't modify the objects you give to it.
I can't however say much about the comments for each parameter.
1 I assume the first parameter is also const, because it got the brackets, where the const is noted in the other two parameters, but this is just guessing.

what does this error mean in general? and how I fix it in this case? [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
The error message appears on Xcode that says "invalid operands to binary expression.
in my code I'm using an array of a struct, i'm trying to sort input data in an ascending order, and i'm getting this error message at the "if" condition shown in the print screen at this link:
https://www.dropbox.com/s/0mch2gbxcif0a20/Screen%20Shot%202016-04-27%20at%2012.45.45%20PM.png?dl=0
The Code
if (studentsInfo[i] > studentsInfo[i + 1]) {}
The Error
Invalid operands to binary expression ('students' and 'students')
What do you compare in your program? As I see, you have to compare names, but all you do is compare an array element which is a struct data type.
If you are trying to compare names, you have to use dot "." operator to reach names. After yo compare names, you can change the elements's place.
The error means that > only takes two arguments and you are using it for something else. In this case you are comparing an entire data structure that does not have an override for > operator and is an undefined behavior. StudentsInfo[i] is a data structure that has more than one element in it. Replace the StudentsInfo[i] with StudentsInfo[i].GPA or another element whose data type has a defined > operator.

C++ : How to arrange 4 values in an ascending order? [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
Hello I know it may be a beginners question but I need help.
I need to compare between 4 values added by the user and arrange them in am ascending order by using a function that takes 2 inputs and return the smaller one. I know it can be done by arrays but I must not do it. I already have the function but I don't know how to use to do the trick without having a very long code. Thanks
This seems to me to be an obvious "homework question," so let me answer it cryptically in order to maybe push you in the right direction.
First, the hint: divide and conquer.
Second hint: the "Towers of Hanoi" problem.
You have a function that can compare two values. Okay, then: "four elements" can be viewed as "two groups of two values each." Given that either of the two input to your comparison-function can be the result obtained by a nested call to the same function . . . you can, indeed, solve this problem, in one line of code, without using arrays.
I'm trying here to "teach you to fish," so I'm not handing you the fish on a platter.
If you know c++ then you can use sort function. But for this you have to include algorithm as:
#include <algorithm>
and sort function will be used as:
sort(array, array+N);
where array is the array name and N is the size of array.After this operation you will get a sorted array in ascending order and return first element.Now the function will look like as:
int smallest(int *array) {
int size = sizeof(array) / sizeof(array[0]);
sort(array, array+size);
return (array[0]);
}
And now call this function from main()

Haskell about matching a Char with a list [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 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

In Haskell, how to construct lists and make substitution between it [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 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.