If I have a list [1;2;3;4;5;6] and I want to return a list of the odd indices [2;4;6], could I do this with List.map and some function? I'm having difficulty figuring this out.
What List.map does is return a function (call it f say) of each of the elements of the list. For each element x of the input list, it returns f x in the resulting list. Hence, the returned list is always the same length as the one it is passed.
Since you want a shorter list, you can't use List.map.
As #UnholySheep says, you could use List.filteri. It's specifically intended for returning only some of the elements of the list based on their position in the list.
i'm trying to find a way in ocaml to apply functions to matrices.
In my implementation, a matrix is an int list of an int list (as in the matrix is a int list, which holds other int lists)
My idea right now is:
let valid_matrix x =
match x with
[] -> true
| (map length x) ->
;;
(Map and length are separate functions which find the number of elements in a list and perform a map function in which a function is applied to all elements of a list, i cannot use a library).
I know this code is incomplete but i am unsure how to compare the number of elements in each matrix to each other and I don't know either how I would access each individual int list (row) within the matrix if I wanted to do further manipulation to it.
Kind regards.
"i cannot use a library" => so this is homework. No code for you, just hints.
Split your problem in two:
1) Change the rows of the matrix into their length. For this apply the length function to each row of the matrix (your map function). This then gives you a int list containing the length of each row.
2) Check that all values in the list are the same.
I'm coding in haskell and want to know how find a certain element in mutiple list.
Here an example let say:
x = [(1,2,3,4,5),
(3,4,5,6,6),
(5,6,2,1,1),
(1,2,5,6,2)];
Let say I want to find the 3rd element of each list.
So the program will print out 4,6,1,6
I know about the !! but when I do something like x !! 3, it prints out the third row(1,2,5,6,2).
I want it so it print out the 3rd element of each list.
What you've provided is not actually a list of lists, but a list of tuples. Tuples have a special type based on the number and type of their elements, so the type of your x above is [(Int,Int,Int,Int,Int)].
Unlike lists, which allow us to extract values by index with the !! operator (ex. [1,2,3] !! 1 = 2), in order to extract specific values from a tuple we must pattern match the entire tuple, giving some name to the value we wish to extract and using it in our return value. To extract the fourth value from a tuple of holding 5 values, we could write a function like this:
f (a,b,c,d,e) = d
Or, as an anonymous function (because, if we are only going to use it when mapping over the list, it's nice to not bother assigning it a name):
(\(a,b,c,d,e) -> d)
Since we only care about the fourth value, we can choose to discard all others (you said third but meant index 3 -> 4th term above?):
(\(_,_,_,x,_) -> x)
Now we have a list of such tuples, and we'll want to apply it to each. We can do this with map, which will apply the function to each and return a list of the third value from each tuple:
f xs = map (\(_,_,_,x,_) -> x) xs
Or, with eta-reduction:
f = map (\(_,_,_,x,_) -> x)
Example usage:
gchi>> f [(1,2,3,4,5),(3,4,5,6,6),(5,6,2,1,1),(1,2,5,6,2)]
[4,6,1,6]
I'm very new to prolog. I have a size of list predicate. I'm trying to make another predicate that takes two arguments (an int, and a list) and it calls the size of list predicate on the passed in list and returns true if the list size matches the int passed in.
This is what I have so far.
size([],0).
size([H|T],N) :- size(T,N1), N is N1+1.
isEqual[X,Y] :- X/size[Y,N]. %X is an int representing expected length of list, Y is the list
Advice is welcomed.
let concat (l : string list) : string = fold_right (fun a x -> a ^ x) l ""
so to go through it, I see that
let concat takes a list with strings, and returns a string.
fold right takes 3 parameters,
1 the first being a function that concatenates two strings, it takes a and x, and then concatenates them using ^.
2 the second argument is the list,
3 and finally the third is the accumulator that gets each pass added to it.
BUT, how does the func a x know that a is the first element of the list, and x is the second.
And when it passes through again, how does it know that a is the third element of the list, and x is the fourth, and so forth?
The accumulator is the first argument, a, to the fold function, and x is set to each element of the list in turn as the fold walks over it.
The return value of the fold function is the new value of the accumulator.
The initial value of the accumulator is the empty string, so the result of the first concatenation, which becomes the new accumulator, is the same value as the first element of the list.
Then the next element is concatenated to that, and so on until the end of the list is reached, at which point the fold returns final value of the accumulator, the fully concatenated string.