Erlang/OTP Forums

Author Message

<  Erlang  ~  String: equal -How to compare input with "[]" ??

andersbranderud
Posted: Wed Oct 28, 2009 7:22 pm Reply with quote
Joined: 26 Oct 2009 Posts: 2
Hello!
I am trying to design a function that has as input an list with lists. One sublist can be empty: [] and a sublist can contain " {}" . For example:
[ [], [ {} ], [ {one,1}, {one,0} ] ]

I want this function to append all the arguments in the list. The result should be this: [ {one,1}, {one,0} ]

The functionality of lists: append gives the result: "[{},{one,1},{one,0}]"


Here follows a sub problem in my problem:

I call the following function with this call:
test: append_remove_empty_list ( [ [] ] ).

I want the function to compare X (in the above call "[]") with the string "[]".

Here is the function:
append_remove_empty_list ( [ X | T]) ->
erlang: display (X ),
case string: equal ("[]", "X") of
true -> io: fwrite ("true... \n\n"); %Evaluated true
false -> io:fwrite ("false \n\n")
end.

In the above case the resul in the console window is false, but I want it to be true.

Thanks for your help!

Anders Branderud
bloganders.blogspot.com
View user's profile Send private message
uwiger
Posted: Thu Oct 29, 2009 12:30 pm Reply with quote
User Joined: 03 Jul 2006 Posts: 604 Location: Sweden
andersbranderud wrote:
Hello!
I am trying to design a function that has as input an list with lists. One sublist can be empty: [] and a sublist can contain " {}" . For example:
[ [], [ {} ], [ {one,1}, {one,0} ] ]

I want this function to append all the arguments in the list. The result should be this: [ {one,1}, {one,0} ]


The simplest way to do this is to flatten the list:

Code:
1> lists:flatten([[], [{}],[{one,1},{one,0}]]).           
[{},{one,1},{one,0}]


andersbranderud wrote:

The functionality of lists: append gives the result: "[{},{one,1},{one,0}]"


yes... except for the quotes. When you write something as "..." in Erlang, it means a list of characters, i.e. a string, which is not what you have in this case.


andersbranderud wrote:
I call the following function with this call:
test: append_remove_empty_list ( [ [] ] ).

I want the function to compare X (in the above call "[]") with the string "[]".

Here is the function:
Code:
append_remove_empty_list ( [ X | T]) ->
      erlang: display (X ),
      case string: equal ("[]", "X") of
            true -> io: fwrite ("true... \n\n"); %Evaluated true
            false -> io:fwrite ("false \n\n")
      end.


In the above case the resul in the console window is false, but I want it to be true.


The expression
Code:
case string:equal("[]","X") of...
compares the constant string "[]" with the constant string "X", and they will of course never be equal.

What you want is to either write
Code:
case [] == X of true -> ...; false -> ... end
or match already in the function head:

Code:

append_remove_empty_list( [ [] | T]) ->
    true;
append_remove_empty_list([ H | T]) ->
    false.

_________________
http://www.erlang-consulting.com
View user's profile Send private message Visit poster's website
andersbranderud
Posted: Thu Oct 29, 2009 10:44 pm Reply with quote
Joined: 26 Oct 2009 Posts: 2
Uwiger,

Thanks for your help!

(I knew that this was wrong: case string: equal ("[]", "X") I didn't mean to post that; I intended X without "" )

Have a nice weekend!

Anders Branderud
bloganders.blogspot.com
View user's profile Send private message
wuji
Posted: Fri Sep 14, 2012 7:30 am Reply with quote
User Joined: 10 Aug 2012 Posts: 654
Michigan passed a ban on the procedure.In one contentious case, he helped helped cheap authentic jordans helped Hugh Gale, a 70-year-old with emphysema and congestive heart disease, to
but investigators reportedly found papers that showed Kevorkian altered the account of of buy real jordans of the death, deleting Gale's request to halt the procedure.But it was
1998 episode of CBS's "60 Minutes," showing Kevorkian giving a lethal injection injection authentic jordans injection to Thomas Youk, 52, who suffered from Lou Gehrig's disease, or
lateral sclerosis, that led to Kevorkian's conviction on second degree murder charges."He charges."He cheap polo shirts charges."He was a complex man, the smartest man I ever met," said
Fieger, the lawyer who got Kevorkian acquitted eight times."We had a love-hate love-hate knockoff designer *beep* love-hate relationship," Fieger told ABCNews.com last year. "It was a father-son relationship
me being the father and he being the son. I was up up cheap designer *beep* up against the governor, politicians, police and the prosecutors, but the biggest
View user's profile Send private message
dongdongwu
Posted: Wed Sep 19, 2012 7:48 am Reply with quote
User Joined: 19 Sep 2012 Posts: 236
Girls would refuse to even leave their replicas behind. specifically when the product beats the complete meaning of the Christian Louboutin men outlet; in conditions of good quality and detailing.If Cinderella was residing in your twenty primary century as opposed to the aged a single then there would not be any 'Happy actually after'. properly appears like girls nowadays are as well fond of the strong;Christian Louboutin Men Shoes to leave them in your center of nowhere.Christian Louboutin for men Shoes would arrive true handy being a excellent handbag using the glimpse and really feel belonging to the authentic but at a very much lesser price tag adding as very much as types picture in your process.
View user's profile Send private message

Display posts from previous:  

All times are GMT
Page 1 of 1
This forum is locked: you cannot post, reply to, or edit topics.

Jump to:  

You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You cannot download files in this forum