| Author |
Message |
< Erlang ~ recursion excercise |
| x00 |
Posted: Thu Mar 03, 2011 11:44 am |
|
|
|
Joined: 03 Mar 2011
Posts: 2
|
I am new to functional programming, however I've always quite liked pattern matching type things.
I have been following learn you some Erlang for the great good
I have been finding it quite a helpful tutorial, filled in quite few gaps I didn't get from the getting started guide.
Anyway I'm at recursion.
http://learnyousomeerlang.com/recursion
It was suggested try I make the following two examples tail recursive
Code: zip([],[]) -> [];
zip([X|Xs],[Y|Ys]) -> [{X,Y}|zip(Xs,Ys)].
Code: lenient_zip([],_) -> [];
lenient_zip(_,[]) -> [];
lenient_zip([X|Xs],[Y|Ys]) -> [{X,Y}|lenient_zip(Xs,Ys)].
This is what I cam up with
Code: tail_zip(A,B) -> zip(A,B,[]).
tail_zip([],[], Acc) -> Acc;
tail_zip([X|Xs],[Y|Ys], Acc) ->
tail_zip(Xs,Ys, Acc++[{X,Y}]).
And
Code: lenient_zip(A,B) -> lenient_zip(A,B,[]).
tail_lenient_zip([],Y,Acc) -> Acc++Y;
tail_lenient_zip(X,[],Acc) -> Acc++X;
tail_lenient_zip([X|Xs],[Y|Ys], Acc) ->
tail_lenient_zip(Xs,Ys, Acc++[{X,Y}]).
Note the second isn't meant to be exactly the same function otherwise I could do it like so:
Code: tail_lenient_zip(A,B) -> lenient_zip(A,B,[]).
tail_lenient_zip([],_,Acc) -> Acc;
tail_lenient_zip(_,[],Acc) -> Acc;
tail_lenient_zip([X|Xs],[Y|Ys], Acc) ->
tail_lenient_zip(Xs,Ys, Acc++[{X,Y}]).
Anyway it worked however his code is like so:
Code: tail_zip(X,Y) -> reverse(tail_zip(X,Y,[])).
tail_zip([],[],Acc) -> Acc;
tail_zip([X|Xs],[Y|Ys], Acc) ->
tail_zip(Xs,Ys, [{X,Y}|Acc]).
Code:
tail_lenient_zip(X,Y) -> reverse(tail_lenient_zip(X,Y,[])).
tail_lenient_zip([],_,Acc) -> Acc;
tail_lenient_zip(_,[],Acc) -> Acc;
tail_lenient_zip([X|Xs],[Y|Ys], Acc) ->
tail_lenient_zip(Xs,Ys,[{X,Y}|Acc]).
Note reverse was done in another example. I Know there is already a reverse for lists.
Anyway is the way I did it wrong? Advantages/disadvantages? |
|
|
| Back to top |
|
| zajda |
Posted: Thu Mar 03, 2011 6:40 pm |
|
|
|
User
Joined: 22 Aug 2009
Posts: 83
|
| the second set of your answers (with reverse) is better. It is better to add element in front of the list. |
|
|
| Back to top |
|
| x00 |
Posted: Fri Mar 04, 2011 12:37 am |
|
|
|
Joined: 03 Mar 2011
Posts: 2
|
Yep I'm kind of getting that now.
However in that example he could have used his tail recursive reverse, but he used the initial one. |
|
|
| Back to top |
|
| dongdongwu |
Posted: Thu Sep 20, 2012 6:08 am |
|
|
|
User
Joined: 19 Sep 2012
Posts: 236
|
His good friend Diane said: "Christian Louboutin Men Shoes was a magician, he make shoes is immediately put his female people with legs and advantage. He understands women wanted to do and can make them into beautiful Cinderella." Madonna often in its concert wearing Christian louboutin high heels , and some famous superstar like Angelina jolie, mariah Carey, beyonce Knowles, the famous Japanese singer YaYouMei Hamasaki helps Christian Louboutin Men Shoes set up its powerful position. The youngest customers will count Tom cruise's daughter sully cruz. Louboutin made for only a pair of handmade Christian Louboutin high heel Shoes! Want to be more fashion? Put on Christian Louboutin Outlet !
Candy colors of the chalaza high-heeled shoes with lolita type allure, set full finely gem blue "neon shoes" is to need to use "sexy" to describe. Each pair are worth careful appreciation of lithe and graceful fairy ludaoli, what kind of most let you move?Christian Louboutin Men Shoes that one brush red is always cannot resist the temptation, Christian Louboutin men outlet continue to use the days of high 8cm above slender heel proclaim the sexy and luxuriant. The bowknot on black pointed high-heeled shoes with sharp rivet concomitant, wild python met enchanting color printing grain, It is that pairs of high-heeled shoes lets Carrie more feminine flavour. Like Christian Louboutin for men her word. |
|
|
| Back to top |
|
|
|
All times are GMT
|
|
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
|
|
|