Hey guys I am having a problem with my code. The code below supposed to remove the first 2 in the list and then concatenate them.So the result answer would be 1,2.
first = [1,2,4,5,6,7] !! 0
second = [1,2,4,5,6,7] !! 1
newans = first ++ second
You can not remove elements from a list: Haskell is declarative meaning once you construct a list a, a will always work with the same list.
You can however construct a new list without the first two elements, and create a new list with the first two elements. For example:
get_remove_2 :: [a] -> ([a],[a])
get_remove_2 (a:b:cs) = ([a,b],cs)
We thus construct a new list with the first two elements with the [a,b] expression.
This function will take as input a list [a] and return a 2-tuple with as first element a list with two elements: the first two elements of the original list, and as second element the list where the first two elements are not present.
Note that this function will only work if the given list contains at least two elements. Otherwise it will error.
Related
I've been seeing this syntax, but I'm not quite sure what it means. It's when two square brackets are next to the name of one list. I'm assuming this does some type of list slicing?
mylist[x][y]
mylist[][]
These are just some examples of what I've seen. (I've used variables x&y to represent an arbitrary number)
This notation can be used when the list contains some other lists as elements, which is helpful to represent the matrices. For example:
a=[[1,2,3],[4,5,6],[7,8,9]]
a[0][0] #This gives the number 1.
In this case, a[0] (the first index) chooses the 1st element, which is [1,2,3]. Then the second index (a[0][0]) chooses the first element of the list defined by a[0], thus giving the answer 1.
The top line just indexes into a list within a list. So for example, you could have
mylist = [
[1,2,3],
[4,5,6],
[7,8,9],
]
value = mylist[1][2]
which will get the value 6.
The bottom line doesn't look like valid Python to me.
EXPLANATION:
Consider that mylist[1] just extracts the second element from mylist (second because of 0-based indexing), which is [4,5,6]. Then adding [2] looks up the third item in that list, which is 6. You could also write
inner_list = mylist[1]
value = inner_list[2]
or
value = (mylist[1]) [2]
which both do the same thing.
I haven't found a way to solve this. I have a list of integer, where which element of the list is a binary digit (0 or 1) so I need to design a function which transforms this list of integers into the proper decimal number.
Example:
Input: [0,1,0]
Output: 2
But there is a specific condition, it is neccesary to use list of comprehension and you can't use recursivity.
The problem it is, when I need to know the position of the digit for apply the transform because I can't save the position in the list of comprehension.
Thank you
The problem it is, when I need to know the position of the digit for apply the transform because I can't save the position in the list of comprehension.
You can, by using zip and a range, you generate 2-tuples that carry the index, like:
[(idx, val) | (idx, val) <- zip [0..] bin]
will produce a list of 2-tuples: the first element containing the element, and the second the element of data at that position.
So if bin = [0,1,0], then the above list comprehension will result in:
Prelude> [(idx, val) | (idx, val) <- zip [0..] bin]
[(0,0),(1,1),(2,0)]
Since this seems to be the "core problem", I propose that you aim to solve the rest of the problem with the above strategy, or ask a question (edit this one, or ask a new one) if you encouter other problems.
I have a list of subjects and weekdays, on which the subject is taught.
subjectDays = [("maths", ["mon", "tue"]),
("science", ["mon", "wed"])]
Now I would like to generate a list of combinations.
The result should look like as follows.
combinations = [[("maths", "mon"), ("science", "mon")],
[("maths", "mon"), ("science", "wed")]
-- etc.
]
Could anyone please help me to write a function in Haskell to produce the above result?
Thank you very much in advance.
From the comment below, I now realize that you want another output than the one I initially understood.
Essentially, you want each sublist to contain each subject (exactly once), paired with one of its days. If we still take combinations as defined below (the "wrong" output) we realize that we want to take the "cartesian product" of all the lists inside combinations. This cartesian product can be obtained using Control.Monad.sequence.
wantedCombinations = sequence combinations
Here's a hint.
First, write a function to expand a single pair of yours into a list.
combs :: (a,[b]) -> [(a,b)]
combs (subject, days) = ...
This could be solved using a list comprehension, for instance. Or map.
Then, we can apply combs to each pair in the list
combinations = map combs subjectDays
write an expand function to work on a single pair
expand :: (a,[b]) -> [(a,b)]
expand (x,[]) = []
expand (x,(y:ys)) = (x,y): expand (x,ys)
map to your structure and take the transpose (Data.List)
transpose $ map expand subjectDays
So i am currently working with Haskell at my college, but kinda struggling with pattern-matching and to be more specific i'll give the program, that i am to solve:
My function is awaiting a list of lists ( with each list containing at least 3 elements ) and in each list the second element is replaced with the number of the third element.
Example:
[[1,2,3],[4,5,6,7],[8,9,10]] should become [[1,3,3],[4,6,6,7],[8,10,10]
So far I've made the following:
f [] = []
f [[x]] = []
f [[x,y]] = []
f [[x,y,z]] = [[x,z,z]]
f ([x,y,z]:xs) = [x,z,z]:f(xs)
My questions are:
How can i identify, that some lists may contain more than 3 elements and that the list must remain the same, only the 2nd element changes.
How can i make the recursion, so that it handles exceptions (for example the first list has 4 elements).
Thank you in advance!
It may help to first write the function that swaps the second value in a list with the third.
swap (x:y:z:rest) = x:z:z:rest
swap xs = xs
In the above, x:y:z:rest matches a list of at least length 3. The value of rest, since it is at the end of the pattern, will match any kind of list, both empty and full. The second pattern match of xs is a catch-all for any other type of list, which will just return the same list if it is less than 3 items long.
From there, you can write f in terms of mapping over the outer list and applying swap:
f = map swap
I am new to SML programming and I have a problem to create a function to remove occurrences of an atom A from a list of integers. This list can be nested to any levels,
means we can have list like [1,2,3] and we can have list like [[1,2],[2,3]] as well as list like [[[1,2],[1,2]],[[2,3],[2,3]]].
So my problem is how can I check if the given item is a list or an atom as I have not found any such function in SMLNJ so far?
I have created a function that checks if the list is empty or not and then it calls a helper iterative function to check if the head of the list is a list or an atom. If it's an atom then replace it with another atom and continue with the rest of the tail.
Inside the helper function if I check that tail of the head of list is empty then it gives an error as tail function can have a list only.
So I have to do it like
tl([hd(a)), and if I do that, then it will always be empty.
If I apply it on the first list I get head as 1 and wrapping it in [] results in [1], so tail of this will be []. Same way if I get head of second list it will be [1,2] and wrapping it in [] will result in [[1,2]], so tail of this is again [].
So is there any way how I can check if the given item is an atom or again a list?
Thanks in advance for all responses.
"This list can be nested to any levels" is not possible in SML, because it's statically typed, and a list type has a specific element type. You either have an int list, which is a list whose elements are all int, or int list list, which is a list whose elements are all int list. You can't have a mixture.
The closest to what you are talking about would be to make an algebraic datatype with two cases, a leaf, or a nested list of elements of this datatype again. Then you can use pattern matching to deconstruct this datatype.
As stated in the other answer:
You could define your own data type
datatype 'a AtomList = Atom of 'a | List of 'a AtomList list
Then, with this data type you could define all the atom lists you mentioned above:
val x = List([Atom(1),Atom(2),Atom(3)])
val y = List([List([Atom(1),Atom(2)]),List([Atom(3),Atom(4)])])
val z = List([
List([
List([Atom(1),Atom(2)]),
List([Atom(1),Atom(2)]),
List([
List([Atom(2), Atom(3)]),
List([Atom(2), Atom(3)])
])
])
])
Then to go over your atom list, you would use pattern matching, as in:
fun show xs =
case xs of
Atom(x) => (*do something with atom*)
| List(ys) => (*do something with list of atoms *)