Erlang function that returns two values - list

I have Erlang function that returns the last element of a list
lastElement([H|[]]) ->H;
lastElement([H|T]) ->lastElement(T).
and a function that returns a list without last element
withoutLastElement([H|[]], Result) ->Result;
withoutLastElement([H|T]) ->withoutLastElement(T, [H|Result]).
so i browse the same list for two times, and for more efficiency i want to do only a function that returns the last element of a list and returns this list without this element in one browse, this is easy in another language but i can't do that in Erlang so any help and thank you all.

You'd usually return the two values in a tuple:
list_and_last(List) ->
list_and_last(List, []).
list_and_last([H], Result) ->
{H, Result};
list_and_last([H|T], Result) ->
list_and_last(T, [H|Result]).
Calling list_and_last with [1,2,3] returns {3,[2,1]}.

In Erlang you can return a tuple, so you simply define a tuple with the last element and the list:
... -> {last_element, list}
You retrieve the value with pattern matching:
{Element, List} = last_element_and_new_list(The_List)
The last element and list will be bound to the respective variables. Note that you can return more than two values in a tuple.

You can simply reverse the list:
List = [1, 2, 3, 4, 5, 6],
[Last | Rest] = lists:reverse(List)

Related

Apply function to one element of tuple in Scala

I'm using Scala and I have a tuple (String, List[Int]) like this:
("tom", [1,2,3])
I need to apply a function to the second element of this tuple and to return a list of tuples. Say, I want to multiply the elements in the list by 2, so the output should be:
[("tom", 2), ("tom", 4), ("tom", 6)]
How to do this?
As #Luis says, the idea is that you use map.
First you get your list out of the input tuple. Here's one way to do it.
val name, values = ("tom", List(1,2,3))
Next you iterate over that list. For each element, you convert it into a new tuple with a constant name and the value multiplied by 2.
val result = values.map(x => (name, 2*x))

How do I add the output of a for-loop to a list in Scala?

I'm looking to iterate over a list of tuples and extract the second value from each tuple. I'd then like to add these values to a new list.
Each tuple in my list consists of a string and an int:
y = List((this, 1), (is, 2), (a, 3), (test, 4)
I can successfully iterate over each tuple and extract the int using:
for (x <- y) {
val intValue = x._2
println(intValue)
}
This gives me
1
2
3
4
I'm then looking to add those values to a list. Any help would be appreciated.
There is no need to use for here, a simple map will do:
y.map(_._2)
The map function creates a new List from the old one by calling a function on each element of the List.
The function _._2 is a shorthand for x => x._2. This just returns the second value in the tuple, which is the Int you want to extract.
You can use map function as #Tim explained or just use yield like in:
val list = for ((_,b) <- y) yield b

Create a hashing list in scala using Map

I am trying to create a map in scala
var map1:Map[String,List[String]] from a list of tuples
input : List(("a1",1),("a1",2),("b1",3),("c1",2),("c1",3),("c1",4))
def fn(List[String]): which takes input as List of tuples and globally declared Map is updated and finally result Map with Value Lists.
output : Map(a1->[1,2], b1->[3], c1->[2,3,4])
How to create a function that can achieve the same by first searching key and appending in the value list.
Here is a code for your logic, which accepts list of tuples as input and returns value Map with key and value list.
import scala.collection.immutable._
//function which accepts List of Tuples and returns Map
def convertListToMap(inputParam:List[(String, Int)]) : Map[String,List[Int]]={
inputParam.groupBy(_._1).mapValues(_.map(_._2))
}
//List of Tuples as inputs
val input=List(("a1",1),("a1",2),("b1",3),("c1",2),("c1",3),("c1",4))
//List((a1,1), (a1,2), (b1,3), (c1,2), (c1,3), (c1,4))
//invoking convertListToMap
convertListToMap(input)
//Output: Map(a1 -> List(1, 2), c1 -> List(2, 3, 4), b1 -> List(3))
// remaining logic to proceed further

Recursive list predicate in prolog

I'm having difficulty with my assignment, I have to write 2 predicates:
remove_all/3 which removes all instances of a given element out of a list and output the list without the given variable, e.g.:
remove_all(a, [b,a,c,a,d], X)
gives
X = [b,c,d])
and
remove_list/3 which removes all elements of a given list out of another list and outputs the resulting list, e.g.:
remove_list([a,b], [b,a,c,a,d], X)
gives
X=[c,d].
This is what I have:
remove_all(Rema,[],[]).
remove_all(Rema,[Rema|X],Res) :-
remove_all(Rema,X,Res).
remove_all(Rema,[L|P],Res) :-
remove_all(Rema,P,NR), Res=[L|NR].
remove_list([],ListB, ListRes).
remove_list([H|Taila],ListB,ListRes) :-
member(H,ListB),
remove_all(H,ListB,ListRes),
remove_list(Taila,ListRes,ListRes) .
remove_list([S|Tailb],ListB,ListRes) :-
remove_list(Tailb, ListB, ListRes).
Now, my remove_all works fine, but not when I use it in my remove_list predicate, it will then only remove all the instances of the first element of the list which specifies which element are to be removed, for example:
?- remove_list([1,2],[1,2,3,2,1],F).
F = [2, 3, 2]
It only removes the 1's.
Anybody have any idea what to do?

Recursively create list in Prolog

I have code in PROLOG:
vals(_,[],_).
vals([H|T],[(H,C)|L],K) :- vals([H|T],L,(K,C)).
This code receivers list and list of tuples, for example:
vals([1],[(1,4),(1,2)],X).
I check if element from first list is equal to some tuples first element from the other list. In this case foundValues will return true, because 1 is equal to each tuples first element. This works fine, but instead of returning true/false, in resulting list I want to return all second element of each tuple where its first element is equal to the element from list. In this case X should be [4,2]. I am trying to do this with (K,C), but no success.
So, the question is - How to return list?
Here's an example on how to append to the list, just for 1 element.
Three cases:
For an empty list
When the element matches the first entry of the tupple
When the element not matches
Starting from this you should be able to create your example.
vals(_,[],[]).
vals(H,[(H,C)|L],[C|K]) :- vals(H,L,K).
vals(H,[(H2,_)|L],K) :-
H \= H2,
vals(H,L,K).
Example:
vals(1,[(1,2),(1,3)],X).
X = [2, 3] ;
false.
Extra:
Consider placing a cut in case 2.
Consider rewrite this with an accumulator