Snipplr
http://snipplr.com/language/haskell
Recent snippets posted on Snipplr.comen-usTue, 24 May 2016 18:31:19 GMT(Haskell) console watch - scrambledeggs
http://snipplr.com/view/91183/console-watch/
console watch in haskell - print local time once a second
press Ctrl-C to stop
use clock_nanosleep to wait for next second ]]>Sun, 22 Feb 2015 04:11:04 GMThttp://snipplr.com/view/91183/console-watch/(Haskell) Circa comparison of fractional numbers - kosenko
http://snipplr.com/view/88264/circa-comparison-of-fractional-numbers/
This module provides a couple of functions and an operator to compare approximately fractional numbers. ]]>Fri, 23 Jan 2015 20:44:54 GMThttp://snipplr.com/view/88264/circa-comparison-of-fractional-numbers/(Haskell) Page Structure in HTML5 - apphp-snippets
http://snipplr.com/view/70352/page-structure-in-html5/
This example is a basic HTML5 page structure that you can use for developing of your HTML5 website layout. ]]>Mon, 11 Mar 2013 17:42:07 GMThttp://snipplr.com/view/70352/page-structure-in-html5/(Haskell) Linked List in C - rtperson
http://snipplr.com/view/68865/linked-list-in-c/
I bit of overkill for USACO's "Milk2" algorithm training problem. This problem would be better suited to an interval tree, but here it is. ]]>Sun, 02 Dec 2012 12:32:20 GMThttp://snipplr.com/view/68865/linked-list-in-c/(Haskell) Binary Search in Haskell - rtperson
http://snipplr.com/view/68840/binary-search-in-haskell/
Improved version, using lenses instead of the dangerous (!!) operator for list access. ]]>Fri, 30 Nov 2012 14:16:50 GMThttp://snipplr.com/view/68840/binary-search-in-haskell/(Haskell) Simultaneous filter and map - deepsoul
http://snipplr.com/view/59474/simultaneous-filter-and--map/
The `filtermap` functional below allows to perform the standard `map` and `filter` operations in one go. The mapping function has to return a `Maybe` monad, and values of `Nothing` are filtered out. ]]>Thu, 06 Oct 2011 07:53:01 GMThttp://snipplr.com/view/59474/simultaneous-filter-and--map/(Haskell) Haskell 99 Problems - Problem 23 - rtperson
http://snipplr.com/view/58582/haskell-99-problems--problem-23/
problem 23, Extract a given number of randomly selected elements from a list.
Example:
Prelude System.Random>rnd_select "abcdefgh" 3 >>= putStrLn
"eda"
Two problems: 1) How to return a list, and 2) how to sample without duplication ]]>Sat, 10 Sep 2011 07:27:49 GMThttp://snipplr.com/view/58582/haskell-99-problems--problem-23/(Haskell) Haskell 99 Problems, numbers 1 through 9 - rtperson
http://snipplr.com/view/58509/haskell-99-problems-numbers-1-through-9/
I had originally started these problems from #10 (Run-length encoding). I went back and did 1-8 for completeness. ]]>Thu, 08 Sep 2011 00:23:54 GMThttp://snipplr.com/view/58509/haskell-99-problems-numbers-1-through-9/(Haskell) Haskell 99 Problems - Number 20, Arrowed! - rtperson
http://snipplr.com/view/58144/haskell-99-problems--number-20--arrowed/
problem 20, (*) Remove the K'th element from a list
*Main> removeAt 1 "abcd"
"acd"
Trivial using a pure function. A bit more challenging if you use this problem to work up your Arrow-fu. ]]>Thu, 25 Aug 2011 01:54:30 GMThttp://snipplr.com/view/58144/haskell-99-problems--number-20--arrowed/(Haskell) Haskell 99 Problems - Number 18 and 19 - rtperson
http://snipplr.com/view/58064/haskell-99-problems--number-18-and-19/
Problem 18: Extract a slice from a list.
Given two indices, i and k, the slice is the list containing the elements between the i'th and k'th element of the original list (both limits included). Start counting the elements with 1.
Example:
*Main> slice ['a','b','c','d','e','f','g','h','i','k'] 3 7
"cdefg
9 Problem 19 - Rotate a list N places to the left.
*Main> rotate ['a','b','c','d','e','f','g','h'] 3
"defghabc"
*Main> rotate ['a','b','c','d','e','f','g','h'] (-2)
"ghabcdef"
(This one is so easy it feels like cheating...) ]]>Sun, 21 Aug 2011 01:58:02 GMThttp://snipplr.com/view/58064/haskell-99-problems--number-18-and-19/(Haskell) Haskell 99 Problems - Number 17 - rtperson
http://snipplr.com/view/57981/haskell-99-problems--number-17/
problem 17, Split a list into two parts; the length of the first part is given.
Do not use any predefined predicates. (Meaning no splitAt or take or drop)
*Main> split "abcdefghik" 3
("abc", "defghik") ]]>Thu, 18 Aug 2011 06:50:09 GMThttp://snipplr.com/view/57981/haskell-99-problems--number-17/(Haskell) Haskell 99 Problems - Numbers 15 and 16 - rtperson
http://snipplr.com/view/57971/haskell-99-problems--numbers-15-and-16/
problem 15, Replicate the elements of a list a given number of times.
> repli "abc" 3
"aaabbbccc"
problem 16, Drop every N'th element from a list.
*Main> dropEvery "abcdefghik" 3
"abdeghk" ]]>Thu, 18 Aug 2011 03:59:30 GMThttp://snipplr.com/view/57971/haskell-99-problems--numbers-15-and-16/(Haskell) Haskell 99 Problems - Number 14 - rtperson
http://snipplr.com/view/57921/haskell-99-problems--number-14/
problem 14, Duplicate the elements of a list
> dupli [1, 2, 3]
[1,1,2,2,3,3] ]]>Thu, 18 Aug 2011 01:04:04 GMThttp://snipplr.com/view/57921/haskell-99-problems--number-14/(Haskell) Haskell 99 Problems - Solutions to 11 and 12 - rtperson
http://snipplr.com/view/57309/haskell-99-problems--solutions-to-11-and-12/
problem 11, modified run-length encoding
========================================
If an element has no duplicates, mark it as such.
Example:
encodeModified "aaaabccaadeeee"
[Multiple 4 'a',Single 'b',Multiple 2 'c',
Multiple 2 'a',Single 'd',Multiple 4 'e']
problem 12, Decode a run-length encoded list.
=============================================
Given a run-length code list generated as specified in problem 11. Construct its uncompressed version.
Example:
decodeModified
[Multiple 4 'a',Single 'b',Multiple 2 'c',
Multiple 2 'a',Single 'd',Multiple 4 'e']
"aaaabccaadeeee" ]]>Fri, 29 Jul 2011 08:03:55 GMThttp://snipplr.com/view/57309/haskell-99-problems--solutions-to-11-and-12/(Haskell) Run-Length Encoding in Haskell - rtperson
http://snipplr.com/view/57300/runlength-encoding-in-haskell/
Problem 10 of the famous 99 Problems. I got 99 problems, but a Lisp ain't one. ]]>Fri, 29 Jul 2011 06:08:49 GMThttp://snipplr.com/view/57300/runlength-encoding-in-haskell/(Haskell) Towers of Hanoi by Bit Twiddling - rtperson
http://snipplr.com/view/56194/towers-of-hanoi-by-bit-twiddling/
Credit where it's due: I got this from udpn over at codereview.stackexchange.com. I'm just putting it here for further study. ]]>Thu, 07 Jul 2011 01:45:07 GMThttp://snipplr.com/view/56194/towers-of-hanoi-by-bit-twiddling/(Haskell) Towers of Hanoi in Haskell - rtperson
http://snipplr.com/view/56191/towers-of-hanoi-in-haskell/
]]>Thu, 07 Jul 2011 01:05:06 GMThttp://snipplr.com/view/56191/towers-of-hanoi-in-haskell/(Haskell) Real World Haskell Exercise - Ch 4 - rtperson
http://snipplr.com/view/54660/real-world-haskell-exercise--ch-4/
The first fold exercise from RWH, Chapter 4 ]]>Wed, 01 Jun 2011 01:00:47 GMThttp://snipplr.com/view/54660/real-world-haskell-exercise--ch-4/(Haskell) Real World Haskell Exercise - Ch 4, Ex 2-4 - rtperson
http://snipplr.com/view/54659/real-world-haskell-exercise--ch-4-ex-24/
I'm posting the rest of these, mostly because I'm still patting myself on the back for my point-free implementation of exercise 3, and because I give myself about two months before I entirely forget how I did it... ]]>Wed, 01 Jun 2011 00:59:27 GMThttp://snipplr.com/view/54659/real-world-haskell-exercise--ch-4-ex-24/(Haskell) Fibonacci List One-Liner in Haskell - rtperson
http://snipplr.com/view/54657/fibonacci-list-oneliner-in-haskell/
I didn't invent this, but it's too cool a snippet not to capture. Here's a one-liner that will generate a list of Fibonacci numbers in linear time.
EXPLANATION: the colon operator is used to add elements to a list, so it starts out adding on the two base cases, zero and one. After you have these two, you can mathematically generate the rest of the list. The zipWith function takes two lists and combines them using a function (in this case, addition). The two lists in question are fiblist (that is, the list you're currently generating) and "tail fiblist" (that is, every element of the list after the first element).
The only reason this works is because Haskell's laziness gives it the ability to define infinite lists. Haskell will never even attempt to calculate the nth Fibonacci number until it is asked to do so. ]]>Wed, 01 Jun 2011 00:44:37 GMThttp://snipplr.com/view/54657/fibonacci-list-oneliner-in-haskell/