Erlang: List an understanding of an existing list

I am trying to create a new list through an understanding of the list, but I want these new values ​​to be included in the existing list.

In particular, I am trying to create a string from a date and will have some string formatting between the values ​​(dash -). The existing list will be a template if you are with a dash.

Here is what I still have:

{Date, Time} = erlang:universaltime().
DateList = tuple_to_list(Date).
DateListString = [ integer_to_list(X) || X < DateList ].
DateListStringConcatenate = lists:flatten(DateListString).

The result should be something like "20101121"

But I want "2010-11-21"

So, I am thinking about understanding the DateListString of "comprehending" an existing list with a "-" after the first and second elements.

Any suggestions followed by specific code examples are greatly appreciated.

+3
3
1> {{Y,M,D},_} = erlang:universaltime().
{{2010,11,21},{16,42,56}}
2> lists:flatten(io_lib:format("~p-~p-~p", [Y,M,D])).
"2010-11-21"
+7

, :

{Date, Time} = erlang:universaltime().
DateList = tuple_to_list(Date).
DateListString = [ [$- | integer_to_list(X)] || X <- DateList ].
[_ | DateListStringConcatenate] = lists:flatten(DateListString).

Roberto - / , , , .

+4

This is a possible solution, but I feel that it is not elegant. In addition, it does not use list comprehension.

1> {Date, Time} = erlang:universaltime().
{{2010,11,21},{14,51,23}}
2> DateList = tuple_to_list(Date).
[2010,11,21]
3> DateListString = lists:zipwith(fun(X,Y) -> integer_to_list(X) ++ Y end, DateList, ["-","-",""]).           
["2010-","11-","21"]
4> DateListStringConcatenate = lists:flatten(DateListString).
"2010-11-21"
+1
source

Source: https://habr.com/ru/post/1776075/


All Articles