Skip to main content

Home/ Haskell/ Group items tagged Haskell

Rss Feed Group items tagged

Javier Neira

Monads as containers - HaskellWiki - 0 views

  • A monad is a container type together with a few methods defined on it.
  • all the elements which a monadic container holds at any one time must be the same type (it is homogeneous).
  • map (fmap), return and join,
  • ...15 more annotations...
  • map, (but called fmap in Haskell 98) actually comes from the definition of a functor
  • That is, if f is a functor, and we are given a function of type (a -> b), and a container of type (f a), we can get a new container of type (f b). This is expressed in the type of fmap: fmap :: (Functor f) => (a -> b) -> f a -> f b If you will give me a blueberry for each apple I give you (a -> b), and I have a box of apples (f a), then I can get a box of blueberries (f b). Every monad is a functor.
  • The second method, return, is specific to monads. If m is a monad, then return takes an element of type a, and gives a container of type (m a) with that element in it. So, its type in Haskell is return :: (Monad m) => a -> m a If I have an apple (a) then I can put it in a box (m a).
  • takes a container of containers m (m a), and combines them into one m a in some sensible fashion. Its Haskell type is join :: (Monad m) => m (m a) -> m a
  • If I have a box of boxes of apples (m (m a)) then I can take the apples from each, and put them in a new box (m a).
  • bind or extend, which is commonly given the symbol (>>=)
  • liftM :: (Monad m) => (a -> b) -> m a -> m b liftM f xs = xs >>= (return . f) -- take a container full of a's, to each, apply f, -- put the resulting value of type b in a new container, -- and then join all the containers together.
  • The function that does this for any monad in Haskell is called liftM -- it can be written in terms of return and bind as follows:
  • Well, in Haskell, IO is a monad.
  • Lists are most likely the simplest, most illustrative example
  • The reason that bind is so important is that it serves to chain computations on monadic containers together.
  • You might notice a similarity here between bind and function application or composition, and this is no coincidence.
  • What bind does is to take a container of type (m a) and a function of type (a -> m b). It first maps the function over the container, (which would give an m (m b)) and then applies join to the result to get a container of type (m b). Its type and definition in Haskell is the
  • xs >>= f = join (fmap f xs)
  • bind (>>=)
Javier Neira

Leksah - Haskell IDE in Haskell - 0 views

  •  
    Leksah is a Haskell IDE (Integrated Development Environment) written in Haskell based on Gtk+ and gtk2hs. Leksah is a practical tool to support the Haskell development process. It is platform independent and runs on any platform where GTK+, gtk2hs and GHC can be installed. It is currently been tested on Windows and Linux but it should work on the Mac. It works with the Glasgow Haskell Compiler (GHC). Leksah is completely free and distributed under the Gnu Public License 2.0
Javier Neira

Existential type - HaskellWiki - 0 views

  • First of all, it is now impossible for a function to demand a Worker having a specific type of buffer. Second, the type of foo can now be derived automatically without needing an explicit type signature. (No monomorphism restriction.) Thirdly, since code now has no idea what type the buffer function returns, you are more limited in what you can do to it.
  • This illustrates creating a heterogeneous list, all of whose members implement "Show", and progressing through that list to show these items: data Obj = forall a. (Show a) => Obj a   xs :: [Obj] xs = [Obj 1, Obj "foo", Obj 'c']   doShow :: [Obj] -> String doShow [] = "" doShow ((Obj x):xs) = show x ++ doShow xs With output: doShow xs ==> "1\"foo\"'c'"
  • Existential types in conjunction with type classes can be used to emulate the dynamic dispatch mechanism of object oriented programming languages. To illustrate this concept I show how a classic example from object oriented programming can be encoded in Haskell.
Javier Neira

Bluish Coder: Dynamic Compilation and Loading of Modules in Haskell - 0 views

  •  
    The Haskell system GHC has libraries that provide the ability to compile Haskell code and dynamically load it into a running Haskell program. A library that provides this functionality is hs-plugins. Unfortunately hs-plugins doesn't work with the latest GHC release, 6.10.1.
Javier Neira

99 questions/1 to 10 - HaskellWiki - 0 views

  • data NestedList a = Elem a | List [NestedList a]   flatten :: NestedList a -> [a] flatten (Elem x) = [x] flatten (List x) = concatMap flatten x
  • compress :: Eq a => [a] -> [a] compress = map head . group We simply group equal values together (group), then take the head of each. Note that (with GHC) we must give an explicit type to compress otherwise we get:
Javier Neira

http://haskell.on-a-horse.org - 1 views

  •  
    Haskell on a Horse (HoH) is a combinatorial web framework for the programming language Haskell.
Javier Neira

A Neighborhood of Infinity: The IO Monad for People who Simply Don't Care - 0 views

  • Many programming languages make a distinction between expressions and commands.
  • Like other languages it makes the distinction, and like other languages it has its own slightly idiosyncratic notion of what the difference is. The IO monad is just a device to help make this distinction.
  • There is no room for anything like a print command here because a print command doesn't return a value, it produces output as a side-effect
  • ...18 more annotations...
  • It's easy to use: you just write do and then follow it by an indented list of commands. Here's a complete Haskell program:
  • Note also that commands take arguments that can be expressions. So print (2*x) is a command, but 2*x is an expression. Again, little different from a language like Python.
  • So here's an interesting feature of Haskell: commands can return values. But a command that returns a value is different from an expression with a value.
  • We have to use <- instead of let ... = ... to get a returned value out of a command. It's a pretty simple rule, the only hassle is you have to remember what's a command and what's a function.
  • get2Lines = do line1 <- getLine line2 <- getLine return (line1,line2)
  • To introduce a list of commands, use do.To return a value from a command, use return.To assign a name to an expression inside a do block use let.To assign a name to the result of a command, use <-.
  • what's a command and what's an expression? If it has any chance of doing I/O it must be a command, otherwise it's probably an expression.
  • Everything in Haskell has a type, even commands. In general, if a command returns a value of type a then the command itself is of type IO a.
  • eturn is simply a function of type a -> IO a that converts a value of type a to a command of type IO a.
  • 5. The type of a sequence of commands is the type of the last line.
  • The type of an if statement is the type of its branches. So if you want an if statement inside a do block, it needs to be a command, and so its branches need to be commands also. So it's
  • If something is of type IO a then it's a command returning an value of type a. Otherwise it's an expression. That's the rule.
  • following the rules above it's completely impossible to put an executed command inside an expression.
  • As the only way to do I/O is with commands, that means you have no way to find out what Haskell is doing inside expressions.
  • If the type isn't IO a, then you can sleep at night in the confidence that there are no side effects.
  • One last thing. Much of what I've said above is false. But what I hope I have done is describe a subset of Haskell in which you can start some I/O programming without having a clue what a monad is.
  • The idea of capturing imperative computations in a type of (immutable) values is lovely. And so is the general pattern we call "monad".
  • main = do return 1 print "Hello"
Javier Neira

Hoogle - 0 views

  •  
    Hoogle is a Haskell API search engine, which allows you to search many standard Haskell libraries by either function name, or by approximate type signature.
Javier Neira

The best programming languages - 0 views

  • Haskell is not a programmable programming language (unless you look at extensions like template Haskell) but there are enough abstractions in Haskell to easily build domain specific languages with it.
  • But, the idea of time, the idea of sequencing can be built upon a purely functional system and we are led to monads
  • Monad are the recognition that the idea of sequencing operations can be formalized in a purely functional language.
  • ...2 more annotations...
  • The monad is an abstraction allowing to create new control structures and, in associations with the powerful type system of Haskell which makes possible elegant overloading of operators, it is possible to easily build new abstractions that allow to customize the programming language.
  • Unfortunately, Lisp, Smalltalk and Haskell are too different from most programming languages currently used in industry and taught in schools. They really look like alien technology to most developpers.
Javier Neira

Haskell: The Confusing Parts - 0 views

  • f $ x = f x(f . g) x = f (g x)-- Some quick examples of using these. The following line...putStrLn (take 12 (map foo (bar ++ "ack")))-- ...can be rewritten as...putStrLn $ take 12 $ map foo $ bar ++ "ack"(putStrLn . take 12 . map foo) (bar ++ "ack")putStrLn . take 12 . map foo $ bar ++ "ack" In terms of good Haskell style, the last example above is preferable to the others. By the way, note that ($) has the lowest precedence (zero) of any operator, so you can almost always use arbitrary syntax on either side of it.
  • -- Actual definitions:f $ x = f x(f . g) x = f (g x)-- Some quick examples of using these. The following line...putStrLn (take 12 (map foo (bar ++ "ack")))-- ...can be rewritten as...putStrLn $ take 12 $ map foo $ bar ++ "ack"(putStrLn . take 12 . map foo) (bar ++ "ack")putStrLn . take 12 . map foo $ bar ++ "ack"
  • putStrLn (take 12 (map foo (bar ++ "ack")))-- ...can be rewritten as...putStrLn $ take 12 $ map foo $ bar ++ "ack"(putStrLn . take 12 . map foo) (bar ++ "ack")putStrLn . take 12 . map foo $ bar ++ "ack"
  • ...7 more annotations...
  • putStrLn (take 12 (map foo (bar ++ "ack")))
  • anywhere you might write \x -> foo x [1..10], you should instead write flip foo [1..10], where flip is a standard Prelude function that flips the first two arguments of whatever function you give it
  • two arguments of whatever
  • anywhere you might write \x -> foo x [1..10], you should instead write flip foo [1..10], where flip is a standard Prelude function that flips the first
  • The curry and uncurry functions stand in for \f x y -> f (x, y) and \f (x, y) -> f x y, respectively.
  • The curry and uncurry functions stand in for \f x y -> f (x, y)
  • I will point out that return is, in fact, not a return statement. It’s a function, and an inappropriately named function, at that. Writing return () in your do block will not cause the function to return.
  •  
    If you're used to the C family of languages, or the closely related family of "scripting languages," Haskell's syntax (mainly) is a bit baffling at first. For some people, it can even seem like it's sneaking out from under you every time you think you understand it. This is sort of a FAQ for people who are new to Haskell, or scared away by its syntax.
J.A. Alonso

A History of Haskell: being lazy with class - 0 views

  •  
    This long (55-page) paper describes the history of Haskell, including its genesis and principles, technical contributions, implementations and tools, and applications and impact.
Javier Neira

Fatvat: Web Sockets and Haskell - 0 views

  •  
    import Network import System.IO import Control.Concurrent import Char serverHandshake = "HTTP/1.1 101 Web Socket Protocol Handshake\r\n\ \Upgrade: WebSocket\r\n\ \Connection: Upgrade\r\n\ \WebSocket-Origin: http://localhost\r\n\ \WebSocket-Location: ws://localhost:9876/\r\n\ \WebSocket-Protocol: sample\r\n\r\n\0" acceptLoop socket = forever $ do (h,_,_) IO () listenLoop h = do sendFrame h "hello from haskell" threadDelay (3 * 1000000) sendFrame h "it works!" return () sendFrame :: Handle -> String -> IO () sendFrame h s = do hPutChar h (chr 0) hPutStr h s hPutChar h (chr 255)
Javier Neira

Understanding Haskell Monads - 0 views

  • The opposite of referentially transparent is referentially opaque. A referentially opaque function is a function that may mean different things and return different results each time, even if all arguments are the same.
  • a function that just prints a fixed text to the screen and always returns 0, is referentially opaque, because you cannot replace the function call with 0 without changing the meaning of the program.
  • n fact, a function, which doesn't take any arguments, isn't even a function in Haskell. It's simply a value. A number of simple solutions to this problem exist. One is to expect a state value as an argument and produce a new state value together with a pseudorandom number: random :: RandomState -> (Int, RandomState)
  • ...12 more annotations...
  • We have seen that we can solve this problem by expecting a state argument. But what's our state? The state of the terminal?
  • A general purpose language is almost useless, if you can't develop user interfaces or read files. We would like to read keyboard input or print things to the terminal.
  • We seem to have found a useful solution to our problem. Just pass the state value around. But there is a problem with this approach.
  • A very special feature of Haskell is the concept of generalization. That means, instead of implementing an idea directly, you rather try to find a more general idea, which implies your idea as a special case.
  • However, the traditional programmer never had to face generalization. At most they faced abstraction,
  • they are a very abstract structure, which allows implementing functionality at an incredibly general level.
  • Haskell [1] is a purely functional programming language. Functions written in it are referentially transparent. Intuitively that means that a function called with the same arguments always gives the same result.
  • askell takes another approach. Instead of passing the world state explicitly, it employs a structure from category theory called a monad.
  • They are an abstract structure, and at first it can be difficult to understand where they are useful. The two main interpretations of monads are as containers and as computations.
  • The ⊥ value is a theoretical construct. It's the result of a function, which never returns, so you can't observe that value directly. Examples are functions, which recurse forever or which throw an exception. In both cases, there is no ordinary returning of a value.
  • Now that Nothing is a valid result, our function handles all cases.
  • You have some computation with a certain type of result and a certain structure in its result (like allowing no result, or allowing arbitrarily many results), and you want to pass that computation's result to another computation.
Javier Neira

Smart constructors - HaskellWiki - 0 views

  • The most interesting are probably the static checks that can be done with Type arithmetic, that enforce the number of bands at compile time, rather than runtime, by lifting the band count into the type level.
  • typecheck perform the check statically, using phantom types and Peano numbers.
  • So encode a type-level version of the bounds check. Only resistors with bands >= 4 and <= 8 are valid:
  • ...1 more annotation...
  • Further checks can be obtained by separating the metal and ceramic values on the type level, so no function that takes a metal resistor can be accidentally passed a ceramic one. A newtype is useful for this:
J.A. Alonso

Apprendre Haskell vous fera le plus grand bien ! - 0 views

  •  
    Traducción francesa del libro "Learn You a Haskell for Great Good!".
J.A. Alonso

Haskell for Kids « Sententia cdsmithus - 0 views

  •  
    Curso de introducción a la programación para niños de 12 años usando la librería Gloss de Haskell.
J.A. Alonso

Haskell For Kids! Introduction - 0 views

  •  
    Proyecto para enseñar Haskell a niños de 12 ó 13 años usando Gloss.
J.A. Alonso

Functional Programming in Haskell (Part 2 : Abstract dataypes and ``infinite'' structures) - 0 views

  •  
    Introducción a la programación en Haskell por Madhavan Mukund
J.A. Alonso

Safe: De­bug­ging, Doc­u­ment­ing and Test­ing Haskell Pro­grams - 0 views

  •  
    This ar­ti­cle is a short piece ad­vo­cat­ing the use of Haskell based on the strength of its test­ing, de­bug­ging and doc­u­men­ta­tion tools.
1 - 20 of 157 Next › Last »
Showing 20 items per page