https://wiki.haskell.org/api.php?action=feedcontributions&user=Quicksilver&feedformat=atomHaskellWiki - User contributions [en]2024-03-28T21:30:52ZUser contributionsMediaWiki 1.35.5https://wiki.haskell.org/index.php?title=Vim&diff=44817Vim2012-03-09T10:35:34Z<p>Quicksilver: </p>
<hr />
<div>[[Category:Development tools]] <br />
This page intended Haskell vim-users.<br />
<br />
= Indentation =<br />
<br />
The following setup from merijn @ #haskell ensures you use spaces not tabs for indentation for generally sane behaviour:<br />
<br />
<pre><br />
" Tab specific option<br />
set tabstop=8 "A tab is 8 spaces<br />
set expandtab "Always uses spaces instead of tabs<br />
set softtabstop=4 "Insert 4 spaces when tab is pressed<br />
set shiftwidth=4 "An indent is 4 spaces<br />
set smarttab "Indent instead of tab at start of line<br />
set shiftround "Round spaces to nearest shiftwidth multiple<br />
set nojoinspaces "Don't convert spaces to tabs<br />
</pre><br />
<br />
= Plugins =<br />
Put code in file <code>~/.vim/plugin/Haskell.vim</code>, or in multiple files in that directory. <br />
<br />
== Module Sections ==<br />
The following code prompts for a name, and places a section with that name at current position, when key sequence "--s":<br />
<pre><br />
let s:width = 80<br />
<br />
function! HaskellModuleSection(...)<br />
let name = 0 < a:0 ? a:1 : inputdialog("Section name: ")<br />
<br />
return repeat('-', s:width) . "\n"<br />
\ . "-- " . name . "\n"<br />
\ . "\n"<br />
<br />
endfunction<br />
<br />
nmap <silent> --s "=HaskellModuleSection()<CR>gp<br />
</pre><br />
Like so:<br />
<haskell><br />
<br />
--------------------------------------------------------------------------------<br />
-- my section<br />
<br />
</haskell><br />
<br />
<br />
== Module Headers ==<br />
The following code prompts for module name, a note, a description of module, and places a module comment at top, when key sequence "--h":<br />
<pre><br />
let s:width = 80<br />
<br />
<br />
function! HaskellModuleHeader(...)<br />
let name = 0 < a:0 ? a:1 : inputdialog("Module: ")<br />
let note = 1 < a:0 ? a:2 : inputdialog("Note: ")<br />
let description = 2 < a:0 ? a:3 : inputdialog("Describe this module: ")<br />
<br />
return repeat('-', s:width) . "\n" <br />
\ . "-- | \n" <br />
\ . "-- Module : " . name . "\n"<br />
\ . "-- Note : " . note . "\n"<br />
\ . "-- \n"<br />
\ . "-- " . description . "\n"<br />
\ . "-- \n"<br />
\ . repeat('-', s:width) . "\n"<br />
\ . "\n"<br />
<br />
endfunction<br />
<br />
<br />
nmap <silent> --h "=HaskellModuleHeader()<CR>:0put =<CR><br />
</pre><br />
like so:<br />
<haskell><br />
--------------------------------------------------------------------------------<br />
-- | <br />
-- Module : MyModule<br />
-- Note : This is a preview<br />
-- <br />
-- This is an empty module, to show the headercomment produced. <br />
-- <br />
--------------------------------------------------------------------------------<br />
<br />
<br />
</haskell></div>Quicksilverhttps://wiki.haskell.org/index.php?title=Haskell_Cafe_migration&diff=21812Haskell Cafe migration2008-07-17T18:40:05Z<p>Quicksilver: added self to list</p>
<hr />
<div>Often people post wonderful material to the mailing lists, hpaste.org or<br />
on #haskell. This can later be hard to find. The goal of this page is to<br />
collect a list of people who are happy for their contributions to the<br />
Haskell community, in any media, to be added directly to the Haskell wiki.<br />
<br />
If you are happy for your contributions (both new and old posts) on <br />
''any media that are part of the Haskell community'', including:<br />
<br />
* [[Mailing_lists|The mailing lists]] (haskell-cafe@, libraries@ and others)<br />
* [[IRC_channel|The IRC channel]]<br />
* [http://hpaste.org/ The Haskell Paste Bin]: hpaste.org<br />
* [[HaWiki_migration|The Old Haskell Wiki]]<br />
* And the new wiki.<br />
<br />
that ''are not specifically licensed'' to be treated as having been<br />
released under a [[HaskellWiki:Copyrights|Simple Permissive License]].<br />
please add your name to this list, so that others may move your<br />
contributions around haskell.org without fear.<br />
<br />
''Contributions will be licensed specifically under a<br />
[[HaskellWiki:Copyrights|Simple Permissive License]]''.<br />
<br />
* Albert Y. C. Lai (monochrom)<br />
* Andrew Bromage (aka Pseudonym)<br />
* Arthur van Leeuwen (aka earthy)<br />
* Bernie Pope<br />
* [[User:b7j0c | Brad Clawsie]]<br />
* Brandon Allbery<br />
* [[User:BrettGiles | Brett Giles]]<br />
* Bulat Ziganshin<br />
* Cale Gibbard<br />
* Chris Smith (cdsmith; blog also fair game)<br />
* Conor McBride<br />
* Conrad Parker<br />
* Dan Doel<br />
* Dan Piponi (aka sigfpe)<br />
* Derek Elkins<br />
* Dominic Steinitz<br />
* Don Stewart<br />
* Eric Kow (kowey; blog too)<br />
* Eric Mertens (glguy)<br />
* Henk-Jan van Tuyl<br />
* Henning Thielemann<br />
* Ian Lynagh (Igloo)<br />
* Jan-Willem Maessen<br />
* Jonathan Cast<br />
* Jules Bean<br />
* Neil Mitchell (ndm)<br />
* Richard Kelsall<br />
* Richard O'Keefe<br />
* [[User:SamB | Samuel Bronson]] (SamB)<br />
* ShaeErisson<br />
* Stefan O'Rear<br />
* Sterling Clover (sclv)<br />
* Thorkil Naur<br />
* Tim Chevalier (aka Kirsten)<br />
* Tom Conway<br />
* Twan van Laarhoven (twanvl)<br />
* Simon Peyton Jones<br />
* Johannes Ahlmann (jethr0)<br />
<br />
[[Category:Community]]</div>Quicksilverhttps://wiki.haskell.org/index.php?title=Foldable_and_Traversable&diff=16771Foldable and Traversable2007-11-10T22:18:40Z<p>Quicksilver: First draft</p>
<hr />
<div>=Notes on Foldable, Traversable and other useful classes=<br />
<center>'' or "Where is Data.Sequence.toList?"''</center><br />
<br />
Data.Sequence is recommended as an efficient alternative to lists,<br />
with a more symmetric feel and better complexity on various<br />
operations.<br />
<br />
When you've been using it for a little while, there seem to be some<br />
baffling omissions from the API. The first couple you are likely to<br />
notice are the absence of "map" and "toList".<br />
<br />
The answer to these lies in the long list of instances which Sequence<br />
has. The Sequence version of map is "fmap", which comes from the<br />
Functor class. The Sequence version of toList is in the Foldable<br />
class.<br />
<br />
When working with Sequence you also want to refer to the documentation<br />
for at least Foldable and Traversable. Functor only has the single<br />
method, so we've already covered that.<br />
<br />
==What do these classes all mean? A brief tour:==<br />
<br />
===Functor===<br />
<br />
A functor is simply a container. Given a container, and a function<br />
which works on the elements, we can apply that function to each<br />
element. For lists, the familiar "map" does exactly this.<br />
<br />
Note that the function can produce elements of a different type, so we<br />
may have a different type at the end.<br />
<br />
Examples:<br />
<br />
<haskell><br />
Prelude Data.Sequence> map (\n -> replicate n 'a') [1,3,5]<br />
["a","aaa","aaaaa"]<br />
Prelude Data.Sequence> fmap (\n -> replicate n 'a') (1 <| 3 <| 5 <| empty)<br />
fromList ["a","aaa","aaaaa"]<br />
</haskell><br />
<br />
===Foldable===<br />
<br />
A Foldable type is also a container (although the class does not<br />
technically require Functor, interesting Foldables are all<br />
Functors). It is a container with the added property that its items<br />
can be 'folded' to a summary value. In other words, it is a type which<br />
supports "foldr".<br />
<br />
Once you support foldr, of course, you can be turned into a list, by<br />
using <hask>foldr (:) []</hask>. This means that all Foldables have a<br />
representation as a list; however the order of the items may or may<br />
not have any particular significance. In particular if a Foldable is<br />
also a Functor, toList and fmap need not perfectly commute; the list<br />
given ''after'' the fmap may be in a different order to the list<br />
''before'' the fmap. In the particular case of Data.Sequence, though,<br />
there *is* a well defined order and it is preserved as expected by<br />
fmap and exposed by toList.<br />
<br />
A particular kind of fold well-used by haskell programmers is<br />
<hask>mapM_</hask>, which is a kind of fold over<br />
<hask>(>>)</hask>, and Foldable provides this along with the<br />
related <hask>sequence_</hask>.<br />
<br />
===Traversable===<br />
<br />
A Traversable type is a kind of upgraded Foldable. Where Foldable<br />
gives you the ability to go through the structure processing the<br />
elements (foldr) but throwing away the shape, Traversable allows you<br />
to do that whilst preserving the shape and, e.g., putting new values<br />
in.<br />
<br />
Traversable is what we need for <hask>mapM</hask> and<br />
<hask>sequence</hask> : note the apparently surprising fact that the<br />
"_" versions are in a different typeclass.<br />
<br />
== Some trickier functions: concatMap and filter ==<br />
<br />
Neither Traversable nor Foldable contain elements for concatMap and<br />
filter. That is because Foldable is about tearing down the structure<br />
completely, while Traversable is about preserving the structure<br />
exactly as-is. On the other hand <hask>concatMap</hask> tries to<br />
'squeeze more elements in' at a place and <hask>filter</hask> tries to<br />
cut them out.<br />
<br />
You can write concatMap for Sequence as follows:<br />
<br />
<haskell><br />
concatMap :: (a -> Seq b) -> Seq a -> Seq b<br />
concatMap = foldMap<br />
</haskell><br />
<br />
But why does it work? It works because sequence is an instance of<br />
Monoid, where the monoidal operation is "appending". The same<br />
definition works for lists, and we can write it more generally as:<br />
<br />
<haskell><br />
concatMap :: (Foldable f, Monoid (f b)) => (a -> f b) -> f a -> f b<br />
concatMap = foldMap<br />
</haskell><br />
<br />
And that works with lists and sequences both. Does it work with any<br />
Monoid which is Foldable? Only if the Monoid 'means the right<br />
thing'. If you have <hask>toList (f `mappend` g) = toList f ++ toList g</hask> then it definitely makes sense. In fact this easy to write<br />
condition is stronger than needed; it would be good enough if they<br />
were permutations of each other.<br />
<br />
<hask>filter</hask> turns out to be slightly harder still. You need<br />
something like 'singleton' (from Sequence), or <hask>\a -> [a]</hask><br />
for lists. We can use <hask>pure</hask> from Applicative, although<br />
it's not really right to bring Applicative in for this, and get:<br />
<br />
<haskell><br />
filter :: (Applicative f, Foldable f, Monoid (f a)) => <br />
(a -> Bool) -> f a -> f a<br />
filter p = foldMap (\a -> if p a then pure a else mempty)<br />
</haskell><br />
<br />
It's interesting to note that, under these conditions, we have a candidate<br />
to help us turn the Foldable into a Monad, since concatMap is a good<br />
definition for <hask>>>=</hask>, and we can use pure for return.<br />
<br />
== Generalising zipWith ==<br />
<br />
Another really useful list combinator that doesn't appear in the<br />
interfaces for Sequence, Foldable or Traversable is zipWith. The most<br />
general kind of zipWith over Traversables will keep the exact shape of<br />
the Traversable on the left, whilst zipping against the values on the<br />
right. It turns out you can get away with a Foldable on the right, but<br />
you need to use a Monad (or an Applicative, actually) to thread the<br />
values through:<br />
<br />
<haskell><br />
import Prelude hiding (sequence)<br />
<br />
import Data.Sequence<br />
import Data.Foldable<br />
import Data.Traversable<br />
import Control.Applicative<br />
<br />
<br />
data Supply s v = Supply { unSupply :: [s] -> ([s],v) }<br />
<br />
instance Functor (Supply s) where <br />
fmap f av = Supply (\l -> let (l',v) = unSupply av l in (l',f v))<br />
<br />
instance Applicative (Supply s) where<br />
pure v = Supply (\l -> (l,v))<br />
af <*> av = Supply (\l -> let (l',f) = unSupply af l<br />
(l'',v) = unSupply av l'<br />
in (l'',f v))<br />
<br />
runSupply :: (Supply s v) -> [s] -> v<br />
runSupply av l = snd $ unSupply av l<br />
<br />
supply :: Supply s s<br />
supply = Supply (\(x:xs) -> (xs,x))<br />
<br />
zipTF :: (Traversable t, Foldable f) => t a -> f b -> t (a,b)<br />
zipTF t f = runSupply (traverse (\a -> (,) a <$> supply) t) (toList f)<br />
<br />
zipWithTF :: (Traversable t,Foldable f) => (a -> b -> c) -> t a -> f b -> t c<br />
zipWithTF g t f = runSupply (traverse (\a -> g a <$> supply) t) (toList f)<br />
<br />
zipWithTFM :: (Traversable t,Foldable f,Monad m) => <br />
(a -> b -> m c) -> t a -> f b -> m (t c)<br />
zipWithTFM g t f = sequence (zipWithTF g t f)<br />
<br />
zipWithTFA :: (Traversable t,Foldable f,Applicative m) => <br />
(a -> b -> m c) -> t a -> f b -> m (t c)<br />
zipWithTFA g t f = sequenceA (zipWithTF g t f)<br />
</haskell></div>Quicksilver