Run Code
|
API
|
Code Wall
|
Users
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Patreon
My "tying the knot"
--ghc 8.0.1 /opt/ghc/8.0.1/lib/ghc-8.0.0.20160127/ -- This program converts a list into a ring. -- Basically a less confusing "tying-the-knot" example -- See https://wiki.haskell.org/Tying_the_Knot data Node a = Node { prev :: Node a, value :: a, next :: Node a } x = Node z 1 y y = Node x 2 z z = Node y 3 x since :: Node a -> [Node a] since node = iterate next node xAndAfter = since x listToRingWithBeginEnd :: [a] -- lst -> Node a -- begin -> Node a -- end -> (Node a, Node a) -- (nextOfBegin, prevOfEnd) listToRingWithBeginEnd [] begin end = (end, begin) listToRingWithBeginEnd (n:ns) begin end = let hd = Node begin n noh (noh, poe) = listToRingWithBeginEnd ns hd end in (hd, poe) listToRing :: [a] -> Node a listToRing [] = error "list cannot be empty" listToRing (n:ns) = let hd = Node poh n noh (noh, poh) = listToRingWithBeginEnd ns hd hd --(hd, tl) = listToRingWithBeginEnd (n:ns) tl hd -- see https://wiki.haskell.org/Tying_the_Knot in hd myRingHdr = listToRing [1,2,3,4,5] myRingInfLst = since myRingHdr main = do print $ map value $ take 20 $ myRingInfLst
run
|
edit
|
history
|
help
0
Please
log in
to post a comment.
haskell fibonacci
Search
Pascal's triangle in 10 lines
Anonymous recursion. Why doesnt compile?
first haskell program
New wall
hard 1.1
FizzBuzzkell
Haskell Prime Factorizer
boolean functions of zero arguments in haskell
Please log in to post a comment.