Re: [Haskell-cafe] Tutorial: Haskell for the Evil Genius
I found some useful reference code on the Haskell Wiki and constructed my own memoized Fibonacci function using the MemoTrie library, which, fortunately, is builtin with Haskell Platform and therefore does not require the tutorial reader to install additional code. The new version of the tutorial includes an ordinary recursive Fibonacci function (fib1.hs), and the same code but renamed to fib', memoized using the memo function from the MemoTrie library (fib2.hs), and exposed as fib. Unix time information provides real numbers for comparison: The memoized version is clearly much faster. I rewrote the section, deleting the part that stated memoization was automatic, and added text describing how Haskell makes memoization easy. Which version of the Haskell Platform are you using? I tried running your memoized Fibonacci function that uses the MemoTrie library on Windows XP with Service Pack 3, and received the following error message: $./runhaskell fib2 fib2.hs:3:7: Could not find module `Data.Memotrie': Use -v to see a list of the files searched for. My version is as follows: $./ghci -v GHCi, version 6.12.3: http://www.haskell.org/ghc/ :? for help Glasgow Haskell Compiler, Version 6.12.3, for Haskell 98, stage 2 booted by GHC version 6.10.4 Using binary package database: C:\PROGRA~1\HASKEL~1\201020~1.0\lib\package.conf. d\package.cache hiding package base-3.0.3.2 to avoid conflict with later version base-4.2.0.2 wired-in package ghc-prim mapped to ghc-prim-0.2.0.0-2feb0cb38f65a4827135ada88c3 4f3ef wired-in package integer-gmp mapped to integer-gmp-0.2.0.1-72436e28c79d056c87cc0 d2d2f9f3773 wired-in package base mapped to base-4.2.0.2-0d1804f62045e52b2e806996d84f5318 wired-in package rts mapped to builtin_rts wired-in package haskell98 mapped to haskell98-1.0.1.1-b5196101fd7a8c42a8d53bd80 33d6765 wired-in package template-haskell mapped to template-haskell-2.4.0.1-401621dedd4 a5f07bfd8630247358bf5 wired-in package dph-seq mapped to dph-seq-0.4.0-be069f0bb710922a6ddd4ed2b91e3a6 c wired-in package dph-par mapped to dph-par-0.4.0-b31a0ce10b7c92126978fcc929077ad 6 Hsc static flags: -static Loading package ghc-prim ... linking ... done. Loading package integer-gmp ... linking ... done. Loading package base ... linking ... done. Loading package ffi-1.0 ... linking ... done. For reference, my code is as follows: #!c:/Program Files/Haskell Platform/2010.2.0.0/bin/ runhaskell import Data.Memotrie (memo) fib :: Int - Int fib = memo fib' where fib' :: Int - Int fib' 0 = 0 fib' 1 = 1 fib' n = fib' (n - 1) + fib' (n - 2) main :: IO () main = do putStrLn (show (fib 30)) Thank you. -- Benjamin L. Russell -- Benjamin L. Russell / DekuDekuplex at Yahoo dot com http://dekudekuplex.wordpress.com/ Translator/Interpreter / Mobile: +011 81 90-6526-1406 Furuike ya, kawazu tobikomu mizu no oto. -- Matsuo Basho^ ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
Re: [Haskell-cafe] Tutorial: Haskell for the Evil Genius
The latest Haskell Platform is 2012.2.0.0 You are apparently running a much older version. #!c:/Program Files/Haskell Platform/2010.2.0.0/bin/ runhaskell -- -- Regards, KC ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
[Haskell-cafe] character encoding problems with hxt (I think)
Dear Haskellers, I'm trying to write some code that grabs countries and provinces from the iso_3166 files on Linux systems. I seem to be running into some kind of character encoding problem. file says iso_3166_2.xml is a utf8 file, and isutf8 agrees, but when I run the following code, it crashes. uft8Copy makes a byte for byte copy as expected. noCrash read and writes the document without crashing, but the accented characters in the strings show up garbled. Just search for DE and you'll see what I mean. crash (on my system, (Debian testing)) produces the error message below. Can anyone enlighten me on what is going on? Thanks in advance. Henry Laxen {-# LANGUAGE Arrows #-} import Text.XML.HXT.Core import Data.List import qualified System.IO.UTF8 as U isoFile = /usr/share/xml/iso-codes/iso_3166_2.xml countZerosInLines = length . filter (\x - x == '0') . concat utf8Copy = do xml - U.readFile isoFile let doc = readString [withValidate no] xml U.writeFile iso_written.xml xml putStrLn Ran utf8Copy noCrash :: IO () noCrash = do lines - runX (readDocument [withValidate no] isoFile writeDocumentToString [withShowTree yes] ) -- mapM_ putStrLn lines print $ countZerosInLines lines putStrLn Ran noCrash crash :: IO () crash = do xml - U.readFile isoFile let doc = readString [withValidate no] xml src - runX $ doc // writeDocumentToString [withShowTree yes] print $ countZerosInLines src putStrLn Ran crash (didn't) {- Ran utf8Copy 1236 Ran noCrash error: UTF-8 encoding error at input position 2640: InvalidLaterByte 1 error: UTF-8 encoding error at input position 2646: InvalidLaterByte 1 *** Exception: Enum.toEnum{Word8}: tag (363) is outside of bounds (0,255) -} main = do utf8Copy noCrash crash ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
[Haskell-cafe] 2 line finite (monadic) state machine
Hi all,I just wrote some nice code: A finite state machine with monadic state, allowing both DFAs and NFAs to be expressed with the same functions. I've only used it in [], Set, and Maybe, but I think it would be interesting in several others (a probability monad comes to mind). Also, if anyone has any sensible interpretation of this in the continuation monad, let me know.import Control.Monad import Data.Maybe type State = String type Map a b = [(a, b)] -- In Map State (Map Char (m State)), the monad m determines the kind of FSM that is being run. -- If m = [] (or Set), these functions work as a NFA. If m = Maybe, we essentially have a DFA. transition :: (MonadPlus m) = Map State (Map Char (m State)) - State - Char - m State transition transMap q c = fromMaybe mzero $ lookup q transMap = lookup c toFSM :: (MonadPlus m) = Map State (Map Char (m State)) - State - (String - m State) toFSM transMap q0 = foldM (transition transMap) q0 egMachine = toFSM [("p", [ ('0', ["p"]) , ('1', ["p", "q"])])] "p" ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
Re: [Haskell-cafe] Tutorial: Haskell for the Evil Genius
On Sun, Sep 16, 2012 at 8:18 PM, Tillmann Rendel ren...@informatik.uni-marburg.de wrote: Hi, Kristopher Micinski wrote: Everyone in the Haskell cafe probably has a secret dream to give the best five minute monad talk. (1) Most programming languages support side effects. There are different kinds of side effects such as accessing mutable variables, reading files, running in parallel, raising exceptions, nondeterministically returning more than one answer, and many more. Most languages have some of these effects built into their semantics, and do not support the others at all. (2) Haskell is pure, so it doesn't support any side effects. Instead, when Haskell programmers want to perform a side effect, they explicitly construct a description of the side effecting computation as a value. For every group of related side effects, there is a Haskell type that describes computations that can have that group of side effects. (3) Some of these types are built in, such as IO for accessing the world outside the processor and ST for accessing local mutable variables. Other such types are defined in Haskell libraries, such as for computations that can fail and for computations that can return multiple answers. Application programmers often define their own types for the side effects they need to describe, tailoring the language to their needs. (4) All computation types have a common interface for operations that are independent of the exact side effects performed. Some functions work with arbitrary computations, just using this interface. For example, we can compose a computation with itself in order to run it twice. Such generic operations are highly reusable. (5) The common interface for constructing computations is called Monad. It is inspired by the mathematical theory that some computer scientists use when they describe what exactly the semantics of a programming language with side effects is. So most other languages support some monad natively without the programmer ever noticing, whereas Haskell programmers can choose (and even implement) exactly the monads they want. This makes Haskell a very good language for side effecting computation. Tillmann http://blog.languager.org Very nice! Except that perhaps I'd rewrite the last line as: ...makes Haskell a good language for playing with alternative paradigms of side-effecting computation though mine is not as pithy as yours! Rusi http://blog.languager.org ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
[Haskell-cafe] Interleaving type variables in a tree, has this a name ?
Hi Haskell Helpers, For a hobby projet to learn haskell, I'm trying to use this kind of interleaved tree structure (simplified): data ANode a b = ANode a [BNode a b] [BNode a b] data BNode a b = BNode b [ANode a b] [ANode a b] The first list of each node is for links up, the second for down. I can navigate with functions like: type UpRank = Int -- index in the list of nodes type DownRank = Int upA :: UpRank - ANode a b - Maybe (BNode a b, DownRank) downB :: DownRank - BNode a b - Maybe (ANode a b, UpRank) Such as going first up then down leads me to the same point (actually this simplified structure is not enough to do this, but is enough to show my problem simply). Thee are also of course upB and downA. From this idea I have devised a kind of zipper to navigate the tree, add nodes, and be able to step back. However, with this pattern of interleaving a and b values, I could not help but duplicating in a way every data or function declaration, to apply from an A or a B, and often code is very similar (like for upA and downB). Also, I want to go through the tree and modify it keeping the same structure, applying different functions to A and B nodes, like a map involving a pair of functions that will be applied alternatively. I also want a kind of fold. This leads very often to pairs or Either: (a - a, b - b) (ANode a b - ANode a b, BNode a b - BNode a b) Either (ANode a b) (BNode a b) Out of curiosity, is all this (duplicating functions for alternating type variables, using pairs) a known pattern ? Does it have a name ? Are there well known (and less tedious) alternatives, or higher-level workarounds ? Thanks for any hint, Eric ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
Re: [Haskell-cafe] Interleaving type variables in a tree, has this a name ?
On Sat, Oct 13, 2012 at 11:25 PM, Eric Dedieu papa.e...@free.fr wrote: Hi Haskell Helpers, For a hobby projet to learn haskell, I'm trying to use this kind of interleaved tree structure (simplified): data ANode a b = ANode a [BNode a b] [BNode a b] data BNode a b = BNode b [ANode a b] [ANode a b] Wouldn't the following work as well and be simpler to handle : data Node a b = Node a [Node b a] [Node b a] -- Jedaï ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe