Evaluating expressions - erlang

I'm doing some past-papers and need to know if I am correct here.
Give step-by-step evaluations of the following expressions:
foo(0,[2,3,1])
foo(0,[4,0,1])
where foo is defined like this:
foo(_,[]) -> [];
foo(Y,[X|_]) when X==Y -> [X];
foo(Y,[X|Xs]) -> [X | foo(Y,Xs) ].
My answers:
1.
Foo(0, [2, 3, 1])
[2 | foo(0, 3, 1) ]
[2, 3| foo(0, 1) ]
[2, 3, 1 | foo (0)]
[2, 3, 1]
2.
Foo(0, [4, 0, 1])
[4 | foo(0, 0,1])
[4, 0]
Am I correct here?

At least the function parameter are wrong, I would say:
1.
foo(0,[2,3,1])
[2|foo(0,[3,1])] % 3rd clause
[2|[3|foo(0,[1])]] % 3rd clause
[2|[3|[1|foo(0,[])]]] % 3rd clause
[2|[3|[1|[]]]] % 1st clause
[2,3,1]
2.
foo(0,[4,0,1])
[4|foo(0,[0,1])] % 3rd clause
[4|[0]] % 2nd clause
[4, 0]

Related

recursive function for 2D list

I'm very new to f# so please forgive my ignorance. I have a 2D list of int. I'm trying to make a function that will return another 2D list which will contain only the lowest tail items. Something like this:
[[2, 3]
[2, 4]
[2, 5] [[2, 3]
[3, 8] => [3, 2]
[3, 6] [4, 1]]
[3, 2]
[4, 1]]
I can find min value of 1D list with
let findMin items =
match items with
| [] -> ()
| head :: tail ->
let rec recMin minSoFar items =
match items with
| [] -> minSoFar
| head :: tail ->
if head < minSoFar then
recMin head tail
else
recMin minSoFar tail
recMin head tail
which I copied from this answer. Any help would be appreciated.
You are talking about 2D lists, but your sample input is actually a list of lists of tuples (where each list contains just a single tuple value). I assume this is a typo and you wanted to write ; rather than ; and create an actual 2D list:
let input =
[ [2; 3]
[2; 4]
[2; 5]
[3; 8]
[3; 6]
[3; 2]
[4; 1] ]
Now, the three items that you want to get as the result seem to be the three sub-lists with the smallest last element in the list. The easiest way for obtaining those would be to sort the list of lists by the last element of sub-lists and then take top 3:
input
|> List.sortBy List.last
|> List.take 3
This returns the three sub-lists you want, but not in the original order. If you wanted them in the original order, then you could first find the third smallest last element and then use that to filter the original list:
let last =
input
|> List.map List.last
|> List.sort
|> List.item 2
input
|> List.filter (fun l -> List.last l <= last)
Note that my code does not correctly handle cases such as input smaller than 3 or empty lists, but it should give you an idea about one way of solving this problem.

How to use whole list function argument in Erlang

I can understand most of the [H|T] examples I read in the docs. I usually means that I want to use either the H or the T part of the list. What if I want to use the whole list instead. Sample code:
-module(module_variable).
-export([main/0, list_suffix/1]).
variable() -> [1, 2, 3, 4, 5].
list_suffix([_H|T]) ->
lists:suffix(variable, T).
main() ->
io:fwrite("~p~n", [list_suffix([4, 5])]).
The error I get is:
6> module_variable:list_suffix([1,[4, 5]]).
** exception error: bad argument
in function length/1
called as length(variable)
in call from lists:suffix/2 (lists.erl, line 205)
Help is appreciated.
You can use multiple values from the front of the list. You cannot skip an arbitrary number of values in the middle. Since in your code you don't know how many elements from the head you want to match ahead of time, pattern matching can't do this for you.
Some examples:
Setup:
1> A = [1, 2, 3, 4, 5].
[1,2,3,4,5]
Matches first 2 elements of list
2> [1, 2 | _ ] = A.
[1,2,3,4,5]
% Can pattern match to extract values
3> [B, C | _ ] = A.
[1,2,3,4,5]
4> B.
1
5> C.
2
Can match some constant values and also assign
6> [1, 2, D | _ ] = A.
[1,2,3,4,5]
Can match whole list
7> [1, 2, 3, 4, 5] = A.
[1,2,3,4,5]
% Can't skip over elements in the middle
8> [1, 2| [4, 5]] = A.
** exception error: no match of right hand side value [1,2,3,4,5]
% This works, though not useful most of the time:
9> [1, 2, 3 | [4, 5]] = A.
[1,2,3,4,5]
% Can assign every element
10> [B, C, D, E, F] = A.
[1,2,3,4,5]
11> E.
4
12> F.
5
% If you don't use a pipe, the length has to match exactly
13> [B, C, D, E] = A.
** exception error: no match of right hand side value [1,2,3,4,5]

Erlang, Function which return the lenghts of the lists?

I'm trying to implement a function
llen(ListOfLists)
which returns a list containing the lengths of the lists in ListOfLists. Function should use lists:map/2.
Example:
1> funs:llen([[1, 2, 3], [], [4, 5]]).
[3, 0, 2]
I know how to calculate length for one list:
list([]) -> 0;
list([_|T]) -> 1+list(T).
but i don't know how to do this for multiple lists using funs and lists.
lists:map/2 is a higher order function that applies a function for each element of a list. erlang:length/1 is a function that returns the length of a list.
Apply erlang:length/1 on each element of your list using lists:map/2:
lists:map(fun erlang:length/1, [[1, 2, 3], [], [4, 5]])
And when you get to list comprehensions:
53> L = [[1, 2, 3], [], [4, 5]].
[[1,2,3],[],[4,5]]
54> [length(X) || X <- L].
[3,0,2]
A list comprehension is like a for-loop in other languages, and this one reads like:
length(X) for X in L
length(X) || X <- L
The outer [ ] serves to gather up all the results into a list.

Explain Parsing Table at http://hackingoff.com/compilers/ll-1-parser-generator

I am using the following grammar at http://hackingoff.com/compilers/ll-1-parser-generator :
E -> T E'
E' -> + T E'
E' -> EPSILON
T -> F T'
T' -> * F T'
T' -> EPSILON
F -> ( E )
F -> id
The output Parsing Table is
[
[0, "+", "*", "(", ")", "id", "$"],
[0, 0, 0, 0, 0, 0, 0],
[0, 10, 10, 1, 9, 1, 9],
[0, 2, 10, 10, 3, 10, 3],
[0, 9, 10, 4, 9, 4, 9],
[0, 6, 5, 10, 6, 10, 6],
[0, 9, 9, 7, 9, 8, 9]
]
Can somebody explain the parsing table? In particular, what is the meaning of 9 and 10 given that there are only 8 lines in the production rules
It's explained in the page itself, just above the table:
If a terminal is absent from a non-terminal's predict set, an error code is placed in the table. If that terminal is in follow(that non-terminal), the error is a POP error. Else, it's a SCAN error.
POP error code = # of predict table productions + 1
SCAN error code = # of predict table productions + 2
So in the case of a grammar with eight production rules, the values 9 and 10 are POP error and SCAN error, respectively.

How to print a nested list in Erlang: [[1], [1, 1], [1, 1, 1]]?

I have a list which is similar to [[1], [1, 1], [1, 1, 1], [1, 1, 1, 1]] and I need to print it something like this:
1
1 1
1 1 1
1 1 1 1
How do I do this? Any help is appreciated.
If you store the list in L you can use something like this:
lists:foreach(
fun (L2) ->
lists:foreach(
fun(X) -> io:format("~p ", [X]) end,
L2
),
io:format("~n") end,
L
).
I guess you can optimize a bit on the io:format parts.

Resources