简体   繁体   中英

Erlang lists:filter returns "\n\f"

I'm working on a school project in a functional programming class. The project is about determining if a set of dominos (represented as a list of tuples of two numbers from 1-6) can be put end to end. I'm ok on the problem, but I'm running into an issue where lists:filter is returning the string "\n\f" instead of a list like it says in the documentation .

I couldn't find anything online, and was wondering if any of you had any ideas.

Thanks!

Here is my code. The issue is in the check_dominos() function.

-module(challenge).
-export([test/0, check_dominos/1]).


% If there is an even number of each number, true
% else, false

extract_numbers([]) -> [];
extract_numbers([{First, Second} | T]) -> [First] ++ [Second] ++ extract_numbers(T). 

add_matching_numbers(_Previous, []) -> [];
add_matching_numbers(Previous, [First | T]) when Previous =:= First-> [Previous + First | add_matching_numbers(First, T)];
add_matching_numbers(_Previous, [First | T]) -> add_matching_numbers(First, T).


check_dominos(Dominos) -> 
    All_Numbers = extract_numbers(Dominos),
    Sorted_Numbers = lists:sort(All_Numbers),
    Accumulated_Numbers = add_matching_numbers(0, Sorted_Numbers) ,

    Filter_Lambda = fun(Num) -> Num rem 2 == 0 end,
    Result = lists:filter(Filter_Lambda, Accumulated_Numbers),

    Result.

    % Still working on the logic of this part
    %case length(Accumulated_Numbers) =:= length(Result) of 
    %    true -> true;
    %    _ -> false
    %end.

test() ->
    Test_1 = [{1, 3}, {3, 2}, {2, 1}], % Already in order
    Test_2 = [{5, 2}, {5, 6}, {6, 3}, {1, 4}], % Shouldn't work
    Test_3 = [{2, 6}, {3, 5}, {1, 4}, {3, 4}, {6, 1}, {2, 5}], % Should work

    true = check_dominos(Test_1),
    false = check_dominos(Test_2),
    true = check_dominos(Test_3).

Erlang strings are lists of character codes, and by default Erlang shell tries to display lists of integers as strings. To change this behaviour call shell:strings(false). before running your program.

The previous answerer is correct. Any list containing only numbers that correspond to printable characters, will display as a string. The result of your

Accumulated_Numbers = add_matching_numbers(0, Sorted_Numbers)

on Test_2 is "\n\f" , but displayed as a list it is [10, 12] . Try typing [10, 12]. in an interactive erlang shell and you will indeed see it displays "\n\f" . Try:

[7].

in an interactive Erlang shell. For me it displays:

[7]

Try:

[8].

Displays:

"\t"

NB The numbers 8 through 13 are printable characters, as are (some of?) the numbers 32 to 255. Might be some gaps in there. If you want to see the numeric value of a printable character, use a dollar sign, eg $\n. prints 10 .

That said, with your current way of going about things, you won't be able to get an answer with add_matching_numbers as it stands. It drops a value whenever it doesn't match the next item in the sorted list, which doesn't tell you if you had any unmatched items. The result of [10,12] on List_2 tells of this: it is a list of even numbers, like the other results.

Good luck on finding your solution.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM