Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

kelan's scratchpad

by kelan (Deacon)
on Jun 02, 2004 at 13:12 UTC ( [id://359400]=scratchpad: print w/replies, xml ) Need Help??

Typing Haskell

I've recently begun learning Haskell. This is my first foray into functional programming, and I must say it's fascinating. However, I had some trouble getting a good grasp on Haskell's typing system. The differences between types, type classes, type instances, type constructors, and data constructors, when to use each, and even just a good idea of what each of them are, did not want to sink in. I went through A Gentle Introduction to Haskell as my basic intro to the language, and was satisfied with the things I could do right away. But finding a good, easy to understand tutorial or explanation on the different aspects of the type system was eluding me. Then I found a link to "Tour of the Haskell Syntax" under the "Learning Haskell" page. It's written as a quick reference on the actual language syntax, with brief explanations of each rule. The section labelled "Data Types" gives a nice simple and straightfoward description of how data types are setup and used, and helped me get the glimmer of insight I needed.

Edited 2005-01-09
Added 2004-12-13


JAPHs

s iir ejkucsath alnroetph eri;split$+;print$_[$-++]until($-++>$#_);pr +int$_[$---]until($---<1);
s iir ejkucsath alnroetph eri;split$+;print$_[$-++]while($-++-$#_);pr +int$_[$---]while($----())
s oohaa ctkseurjogis; s ii rpeehrtlo nisom; +$ -= + split $ +; ; print + splice @ ## ; _ => + $ ---, $ ++ 1 while + $ ---- $ ++ @ _


Odd Ball Challenge http://rec-puzzles.org/new/sol.pl/logic/weighing/balance
{- Given the number of weighings, prints instructions to solve the foll +owing riddle: You have (3^N - 3) / 2 identical looking objects, one of which is +lighter or heavier than the others (but you don't know which). In N weighi +ngs, determine which is the odd one, and whether it is lighter or heavi +er. -} module Main where import IO import System.Environment main :: IO () main = do args <- getArgs let w :: Int w = read $ head args -- number of weighings l = labels w -- list of object labels n = length l -- number of objects you can use putStr $ " You can find the odd one out of " ++ show n ++ " objects wit +h " ++ show w ++ " weighings, as follows:\n\n"++ " 1. Assign the following \"magic number\" labels to the objec +ts:\n" ++ " " ++ unwords l ++ "\n\n" ++ " 2. Keep track of two strings of numbers, LIGHT and HEAVY.\n\ +n" ++ " 3. Record weighing results as follows:\n" ++ " If pans balance, append 1 to both LIGHT and HEAVY.\n" ++ " If left side is lighter, append 0 to LIGHT and 2 to HEAVY.\ +n" ++ " If right side is lighter, append 2 to LIGHT and 0 to HEAVY. +\n\n" ++ " 4. Perform weighings in this order:\n" ++ weighingOrder 1 w ++ "\n" ++ " 5. After " ++ show w ++ " weighings, either LIGHT or HEAVY w +ill " ++ "match one of the object labels. That object is the odd one, a +nd " ++ "its fault is shown by whether LIGHT or HEAVY matched.\n" weighingOrder :: Int -> Int -> String weighingOrder current total = " #" ++ show current ++ ":\n" ++ " Left : " ++ unwords (weighing current total '0') ++ "\n" ++ " Right: " ++ unwords (weighing current total '2') ++ "\n" ++ if current == total then "" else weighingOrder (current+1) total weighing i w lr = filter (\l -> l !! (i-1) == lr) $ labels w labels = filter (isValidLabel) . labelsOfLength -- if entire string is the same character, or it's a bad sequence, -- this label is not valid isValidLabel l = not $ or [ allSame l, badSequence l ] allSame l@(x:_) = l == (replicate (length l) x) badSequence [] = True badSequence (x:[]) = False badSequence (x:y:ys) = if x == y then badSequence (y:ys) else badChange x y where badChange '0' '1' = False badChange '1' '2' = False badChange '2' '0' = False badChange _ _ = True -- list of all strings of length n consisting of '0', '1', and '2' labelsOfLength n = foldr ($) [""] ( replicate n $ cartesia "012" ) -- Gives the cartesion product of a list and a list of lists cartesia :: [a] -> [[a]] -> [[a]] cartesia xs = concat . map ( \ys -> map ( \x -> ys ++ [x] ) xs )

<html> <head> <style type="text/css"> body { position: relative; font-family: arial; background: #ddd; } .header { border: solid 0.05em #000; background: #888; color: #ffd; padding: 1em; margin-bottom: 1em; } .main { position: relative; } .content { position: relative; border: solid 0.05em #448; background: #ddf; padding: 1em; margin-left: 8em; } .menu { position: absolute; top: 0; left: 0; border: solid 0.05em #844; background: #fdd; padding: 1em; width: 5em; height: 5em; } .menu a { display: block; } .corner { position: relative; float: left; top: -1.05em; left: -1.05em; border-left-color: transparent; border-top-color: transparent; border-right-color: inherit; border-bottom-color: inherit; border-style: solid; border-width: inherit; background: #ddd; width: 3em; height: 3em; } </style> </head> <body> <div class="header"> <h1>Website Title</h1> </div> <div class="main"> <div class="content"> <div class="corner"></div> <p> Lorem ipsum dolor sit amet, consectetuer adipiscing el +it. Phasellus ultricies nonummy nunc. Maecenas sagittis tellus vitae +diam. Nam bibendum augue non nulla. Nam vehicula urna vel arcu. Nulla + in enim. Donec neque magna, pellentesque non, consequat sit amet, pl +acerat quis, mauris. Integer nec elit ac metus pulvinar tincidunt. Ma +uris vel elit. Etiam feugiat. Etiam posuere. Integer volutpat nulla e +u leo. Cum sociis natoque penatibus et magnis dis parturient montes, +nascetur ridiculus mus. Curabitur eleifend, dolor rhoncus molestie pe +llentesque, nisl magna eleifend felis, scelerisque sodales leo turpis + at enim. Duis sapien nisi, feugiat in, facilisis sed, congue eu, orc +i. Ut ac sem. Maecenas vel dolor eget eros euismod rutrum. Curabitur +sed libero a lectus imperdiet fermentum. </p> <p> Integer eget mi ut ipsum elementum ultrices. Pellentes +que habitant morbi tristique senectus et netus et malesuada fames ac +turpis egestas. Vivamus ac augue. Aenean dapibus pretium sapien. Viva +mus orci lorem, tincidunt ut, fringilla ut, placerat viverra, lorem. +Cras a augue. Aenean eu lacus eget lectus feugiat lobortis. Nulla ull +amcorper rutrum nunc. Phasellus ullamcorper, elit quis sodales facili +sis, lectus tortor pharetra turpis, eu luctus risus nisi eu sem. Sed +eget neque. Sed viverra. Etiam dapibus, justo sed aliquam tempor, dia +m sem pulvinar nulla, eu malesuada metus sem ut nibh. Suspendisse ac +justo ac neque placerat pharetra. Duis mattis laoreet magna. Etiam me +tus nulla, interdum sed, elementum in, luctus et, ante. </p> <p> In sodales bibendum risus. Morbi quis diam. Aliquam no +n lectus. Integer vitae mi. Suspendisse sagittis, ligula at consectet +uer ultricies, pede mi rhoncus est, in iaculis mi eros eget felis. Do +nec dignissim, nisl mollis vehicula tristique, eros mauris sagittis v +elit, sit amet gravida lacus enim suscipit libero. Donec interdum tem +por nulla. Sed aliquet nulla ut massa. In vel augue. Nunc leo nulla, +tristique eget, commodo ac, gravida ac, leo. Donec velit nulla, ornar +e et, varius et, dictum eget, felis. Etiam aliquet odio tincidunt pur +us. Cum sociis natoque penatibus et magnis dis parturient montes, nas +cetur ridiculus mus. Cras tincidunt. Etiam tortor purus, tristique a, + rutrum eu, vestibulum eu, arcu. Quisque sit amet felis non ligula da +pibus feugiat. </p> <p> Curabitur at erat nec lacus tempus condimentum. Sed si +t amet ipsum. Praesent velit risus, sagittis a, vulputate eu, blandit + venenatis, quam. Aenean quis nibh id risus commodo aliquam. Suspendi +sse nec tellus eu mauris hendrerit pretium. Nullam vitae libero. Etia +m mi risus, lobortis a, eleifend id, venenatis nec, felis. Ut blandit + vestibulum libero. Nullam ultricies condimentum turpis. Nulla facili +si. Suspendisse tincidunt sagittis arcu. Donec ultrices sem ac felis +venenatis egestas. Proin faucibus lectus pharetra ante. Donec egestas +, arcu sed luctus egestas, dolor nunc pharetra odio, in laoreet pede +lectus eu quam. Quisque vitae dui at leo rutrum sagittis. Nam pretium + enim sit amet sem. Maecenas quis nisl. </p> </div> <div class="menu"> <a href="">Link One</a> <a href="">Link Two</a> <a href="">Link Three</a> <a href="">Link Four</a> </div> </div> </body> </html>
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others about the Monastery: (5)
As of 2024-04-23 21:08 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found