Haskell Stacks : Two Different Ways
I am starting to attempt to understand how Haskell can persist functions sans the variables. You can do it with lists, already. There are plenty of functions that demonstrate the concept. But I want to expand further. I want to try implementing a stack with a list, and then do it with a recursive type. This is what I have for the first one:
I thought the reverse function was neat, but seemingly costly. But, from the Prelude:
We can see a really slick way of doing reverses. "reverse" is merely a function curry of a foldl waiting for the last parameter. Let's see if it is costly:
So, the ones with the same indentation are the same, except the second line is the flip function evaluated. It is what I expected, though. It takes O(n) (?) time (there are two operations on the data: one operation to flip, and another operation to add to the new list. So, maybe it is O(2n)? I forget my algorithm lessons.) It seems though, that the original implementation I provided performs the same though. Maybe expressing it via reverse then is easier.
Lists are, still probably still efficiently implemented, and those operations I am doing are turned into an iteration (tail-call recursion), plus some other magic I don't know about. I have to think of a nifty way of handling it faster though.
Anyway, the other idea I had was to make a stack a recursive data type:
What a wacky idea. I have to think about this one, and read some common examples on this.
putAtEnd [] item = [item]
putAtEnd (x:xs) item = x : putAtEnd xs item
push lst item = putAtEnd lst item
-- or
-- push lst item = reverse (item : (reverse lst))
pop lst = pop_ lst ((length lst) - 1)
pop_ _ 0 = []
pop_ (x:xs) len = x : pop_ xs (len - 1)
-- or
-- pop lst = reverse (tail (reverse lst))
I thought the reverse function was neat, but seemingly costly. But, from the Prelude:
reverse :: [a] -> [a]
reverse = foldl (flip (:)) []
flip f x y = f y x
foldl f z [] = z
foldl f z (x : xs) = foldl f (f z x) xs
We can see a really slick way of doing reverses. "reverse" is merely a function curry of a foldl waiting for the last parameter. Let's see if it is costly:
reverse [1,2,3,4]
-> foldl (flip (:)) [] [1,2,3,4]
-> foldl (flip (:)) (flip [] 1) [2,3,4]
-> foldl (flip (:)) (1 : []) [2,3,4]
-> foldl (flip (:)) (flip [1] 2) [3,4]
-> foldl (flip (:)) (2 : [1]) [3,4]
-> foldl (flip (:)) (flip [2,1] 3) [4]
-> foldl (flip (:)) (3 : [2,1]) [4]
-> foldl (flip (:)) (flip 4 : [3,2,1]) []
-> foldl (flip (:)) [4,3,2,1] []
-> [4,3,2,1]
So, the ones with the same indentation are the same, except the second line is the flip function evaluated. It is what I expected, though. It takes O(n) (?) time (there are two operations on the data: one operation to flip, and another operation to add to the new list. So, maybe it is O(2n)? I forget my algorithm lessons.) It seems though, that the original implementation I provided performs the same though. Maybe expressing it via reverse then is easier.
Lists are, still probably still efficiently implemented, and those operations I am doing are turned into an iteration (tail-call recursion), plus some other magic I don't know about. I have to think of a nifty way of handling it faster though.
Anyway, the other idea I had was to make a stack a recursive data type:
data Stack n m = Item m | StackFrame ((Stack n) (Item m))
-- so ...
(StackFrame (StackFrame (Item 1) (Item 2)) (Item 3))
-- looks like
-- 3
-----
-- 2
-----
-- 1
-----
What a wacky idea. I have to think about this one, and read some common examples on this.

29 Comments:
Hi Brian
Your Stack datatype is essential equal to the normal [] (List) datatype, except that you have ruled out the possibility of an empty stack.
But why don't you just use the following implementation of stacks:
empty = []
push stack elem = elem : stack
pop (elem : stack) = stack
top (elem : stack) = elem
All operations are cheap constant time operations.
Thanks for the info, ken. :)
O(2n) is equivalent to O(n). The function runs in time linearly proportional to the size of the list (n)
;)
[B]NZBsRus.com[/B]
Skip Crawling Downloads Using NZB Downloads You Can Quickly Find HD Movies, Games, Music, Applications & Download Them @ Dashing Speeds
[URL=http://www.nzbsrus.com][B]Usenet[/B][/URL]
[B]NZBsRus.com[/B]
Lose Slow Downloads With NZB Files You Can Instantly Find High Quality Movies, Console Games, MP3 Albums, Software and Download Them @ Dashing Rates
[URL=http://www.nzbsrus.com][B]Usenet[/B][/URL]
Hit to the zoological with two backs casinos? pass over this youthful [url=http://www.realcazinoz.com]casino[/url] advisor and safe conduct up online casino games like slots, blackjack, roulette, baccarat and more at www.realcazinoz.com .
you can also discontinuation our fresh [url=http://freecasinogames2010.webs.com]casino[/url] by at http://freecasinogames2010.webs.com and flourish in trustworthy folding shin-plasters !
another remote [url=http://www.ttittancasino.com]casino spiele[/url] stance is www.ttittancasino.com , because german gamblers, array not involved online casino bonus.
Scrutinize Our Risqu‚ Prices at www.Pharmashack.com, The Noted [b][url=http://www.pharmashack.com]Online Chemist's [/url][/b] To [url=http://www.pharmashack.com]Buy Viagra[/url] Online ! You Can also Take in Dignified Deals When You [url=http://www.pharmashack.com/en/item/cialis.html]Buy Cialis[/url] and When You You [url=http://www.pharmashack.com/en/item/levitra.html]Buy Levitra[/url] Online. We Also Natter a Stupendous Generic [url=http://www.pharmashack.com/en/item/phentermine.html]Phentermine[/url] As a remedy to Your Nutriment ! We Vend Deal mark [url=http://www.pharmashack.com/en/item/viagra.html]Viagra[/url] and Also [url=http://www.pharmashack.com/en/item/generic_viagra.html]Generic Viagra[/url] !
Someone deleted several links from letitbit and shared servers.
From now, we will use www.tinyurlalternative.com as our default [url=http://www.tinyurlalternative.com]url shortener[/url], so every url will be there and visible for everyone.
You can pick out from many great [url=http://kfc.ms]short url[/url] address like:
kfc.ms easysharelink.info jumpme.info megauploadlink.info megavideolink.info mygamelink.info myrapidsharelink.info mytorrentlink.info myurlshortener.com mywarezlink.info urlredirect.info urlshrinker.info weblinkshortener.com youtubelink.info and many others.
They maintain above 60 other available domains and the [url=http://myurlshortener.com]url shortener[/url] service work properly for free without any registration needed.
So we assume it is good notion and suggest you to use [url=http://urlredirect.info]url redirect[/url] service too!
Thank you.
[b]Website Traffic[/b]
A new way to get traffic to your website. Not only will you get loads of [url=http://www.fastpixeltraffic.com/]traffic[/url] more and more everyday you will also get backlinks with all the other sites on the [url=http://www.fastpixeltraffic.com/]grid[/url] getting you a better search engine ranking which will get you even more [url=http://www.fastpixeltraffic.com/]website traffic[/url].Get on the pixel grid today and let the traffic come to you.Taking the internet by storm.The new way to advertise.
Visit: http://www.fastpixeltraffic.com/
You could easily be making money online in the hush-hush world of [URL=http://www.www.blackhatmoneymaker.com]blackhat seo software[/URL], You are far from alone if you don't know what blackhat is. Blackhat marketing uses not-so-popular or little-understood avenues to build an income online.
It isn't hard at all to start making money online in the hush-hush world of [URL=http://www.www.blackhatmoneymaker.com]blackhat blog[/URL], You are far from alone if you haven’t heard of it before. Blackhat marketing uses not-so-popular or little-understood methods to produce an income online.
About casinos? wrench this advanced [url=http://www.realcazinoz.com]casino[/url] counsel and shtick evasively online casino games like slots, blackjack, roulette, baccarat and more at www.realcazinoz.com .
you can also disappoint our up to the two shakes of a lamb's tail log [url=http://freecasinogames2010.webs.com]casino[/url] orientate at http://freecasinogames2010.webs.com and pick up chief lolly !
another voguish [url=http://www.ttittancasino.com]casino spiele[/url] change situation is www.ttittancasino.com , during german gamblers, visit unrestrained online casino bonus.
[B]NZBsRus.com[/B]
Replace Idle Downloads Change To NZB Files You Can Quickly Find High Quality Movies, Console Games, MP3s, Applications and Download Them at Maxed Out Speeds
[URL=http://www.nzbsrus.com][B]Binaries Usenet[/B][/URL]
Greetings im new to this forum i hope i will be able to help and contribute here because iv learned allot myself.
Cheers
[URL=http://plasteringcoursesuk.co.uk][B]plastering courses essex[/B][/URL]
these programming techniques are very good, the weather had seen them but I forgot, thanks for sharing information
EARN GLOBAL MONEY shows you different earning programs from all over the globe and help people to earn money from different earning program owned by different organizations.
http://www.earnglobalmoney.info
Online cash loans are for anyone who needs to borrow funds and is not limited depending on credit. If you have bad credit there are online cash loans that will fit your needs but anyone can apply online including those with shining credit. click Here
SMARTS - that's the name of today's online income affiliate game. You already have "smarts?" Good, that gives you a head start. Yet, the catch is, knowing exactly when, how, where, and what strategies to implement with the God-given smarts you possess. Many a well-intended online income affiliate has landed on the side-shores of this vast Internet sea
click here
HI friends, this information is very interesting, I would like read more information about this topic, thanks for sharing. homes for sale in costa rica
Earning money online never been this easy and transparent. You would find great tips on how to make that dream amount every month. So go ahead and click here for more details and open floodgates to your online income.
All the best.
Get Genuine Ways To Earn Money Online With Payment Proofs. You Can Learn How To Make Money With Traffic Improvement Tips
and make $100 every day .So That You Don't Waste Your Time And Money.for details visit,clickhere
Hello .. firstly I would like to send greetings to all readers. After this, I recognize the content so interesting about this article. For me personally I liked all the information. I would like to know of cases like this more often. In my personal experience I might mention a book called Generic Viagra in this book that I mentioned have very interesting topics, and also you have much to do with the main theme of this article.
When Li was 19, he appeared in his first film, Shaolin Temple. Li was already regarded as a national hero for his athletic
accomplishments, and the film (the first modern kung-fu movie made in China) shot him to superstardom in China...clickhere check out the full information
EARN GLOBAL MONEY gives you instant access to a dynamic, scalable, dedicated and responsible development program - a committed to meeting the highest standards, committed to delivering on promises, and committed to ensuring every program success. make $200 per day
Make $3000-$40000 A Month by Using internet At Home, Internet is quickly growing into a world-wide marketplace, which provides a unique opportunity for everyday people like you and me to cash in on it and start making money online! make $400 per day
Bill stared hard at the freshly fucked beaver, then dropped to his knees andstared right into it with his hands framing the spread. Thanks Taking the cake box he turned to leave when he felt asmall hand on his back.
underage animal sex stories
bdsm branding stories
super young teen sex stories
hot porn stories
bdsm bull whipping stories
Bill stared hard at the freshly fucked beaver, then dropped to his knees andstared right into it with his hands framing the spread. Thanks Taking the cake box he turned to leave when he felt asmall hand on his back.
Very good afternoon to everyone! My name is Richard, I would like to express my joy to have read this fabulous article, I think it's spectacular, it contains what is necessary to keep the reader entertained!
how to unlock iphone 4
unlock iphone 4
unlock iphone 4
http://www.gpucomputing.net/node/6013?q=node/1280 http://www.columbusneighborhoods.org/content/summer-fun-events-columbus?page=1
It was awhile ago when I saw it, but there is apparently something you can download and burn to a disk and it will delete just about everything from your computer to make reformatting easier. Anybody know what the website is? There are missing, necessary files on our desktop or there may have been a virus and now its all screwy. I just got a laptop so was able to transfer all the stuff I needed from the desktop to the laptop, so it'd be better just to reformat since I already have everything backed up here so to speak.
iphone 4 unlock unlock iphone 4
unlock iphone 4 [url=http://unlockiphone44.com]how to unlock iphone 4[/url] how to unlock iphone 4 unlock iphone 4
I really liked your article. cardiovascular Read a useful article about tramadol tramadol
Post a Comment
<< Home