Search Results

Search found 762 results on 31 pages for 'haskell'.

Page 16/31 | < Previous Page | 12 13 14 15 16 17 18 19 20 21 22 23  | Next Page >

  • Efficient heaps in purely functional languages

    - by Kim
    As an exercise in Haskell, I'm trying to implement heapsort. The heap is usually implemented as an array in imperative languages, but this would be hugely inefficient in purely functional languages. So I've looked at binary heaps, but everything I found so far describes them from an imperative viewpoint and the algorithms presented are hard to translate to a functional setting. How to efficiently implement a heap in a purely functional language such as Haskell? Edit: By efficient I mean it should still be in O(n*log n), but it doesn't have to beat a C program. Also, I'd like to use purely functional programming. What else would be the point of doing it in Haskell?

    Read the article

  • Looking for Haskell `group` alternative in F#

    - by Heather
    Haskell group descripted there: http://www.haskell.org/ghc/docs/latest/html/libraries/base/Data-List.html#v:group example : group "Mississippi" = ["M","i","ss","i","ss","i","pp","i"] Is there simple F# way of doing it? I can implement something alike let rec sameCounter (data : list<float>) el same (ss : list<float * int>) = if data.Length > (el + 1) then if data.[el] = data.[el + 1] then sameCounter data (el + 1) <| same + 1 <| ss else if same > 0 then sameCounter data (el + 1) <| 0 <| (data.[el], same) :: ss else sameCounter data (el + 1) <| 0 <| ss else ss let group d = sameCounter d 0 0 [] but it's a bit ugly way I think, is there better variant?

    Read the article

  • What XMonad Configuration Best Replicates Default Ion3 Behavior and Feature Set?

    - by mtp
    Not being very familiar with Haskell and lamenting that Ion 3 is now abandonware, I am curious if anyone out there has found a way of replicating the default Ion 3 behavior and aesthetics in XMonad. If I can't have a near-exact replica of Ion 3-style behavior in XMonad, here is what would be critical to me: Virtual desktops that are empty by default and that spawn full-screen applications, which can be split horizontally or vertically evenly, leaving an empty adjacent pane. The panes, which house open windows, are manually resizable, preferably via keyboard. The panes exhibit tabbed behavior, meaning that they can house multiple windows. Windows can be tagged and moved between panes / virtual desktops via keyboard sequence. A given window may be temporarily exploded into full-screen mode via keyboard sequence. Each new virtual desktop starts in the same state—i.e., with one pane. Each virtual desktop may have its panes divided independently of other virtual desktops. From my investigation, it appears that there are several configurations that provide #3. For as much as I want to spend the time to familiarize myself with Haskell, I just simply don't have time. Any suggestions would be greatly appreciated. As far as I can tell, Ion has no conception of master pane or window, so this behavior is not desired.

    Read the article

  • Cabal and zlib on OS X

    - by Bill
    When I run "cabal update" on my Mac (Snow Leopard, Intel), I get: % cabal update Downloading the latest package list from hackage.haskell.org cabal: Codec.Compression.Zlib: incompatible zlib version Anyone else seeing this? Reinstalling the Haskell Platform hasn't helped. I'm unable to install or update Cabal packages at the moment, so I'd appreciate any help. Thanks!

    Read the article

  • parsec-3.1.0 with custom token datatype

    - by Tener
    parsec-3.1.0 ( http://hackage.haskell.org/package/parsec-3.1.0 ) works with any token type. However there are combinators like Text.Parsec.Char.satisfy that are only defined for Char datatype. There doesn't seem to be any more general counterpart available. Should I define my own versions or did I miss something? Perhaps there are different parser libraries in Haskell that allows: custom token types custom parser state (I need to parse stateful format - Wavefront OBJ)

    Read the article

  • Disadvantage of unlifted type products?

    - by peaker
    In Haskell, lifted type products mean that there's a semantic difference between (a,b,c) and (a, (b, c)). If all pattern matches of all products was always irrefutable, then there would be no difference, and (a, b, c) could be syntactic sugar for (a, (b, c)). Why did Haskell choose to lift type products?

    Read the article

  • GUI wxHaskell database table in grid

    - by snorlaks
    Hello, I'm writing GUI app in haskell. I use sqlite to gather data and want to use grid in wxHASKELL TO present data in app. Everything is all right but I want to have possibility to add rows from haskell app. I achieved that but when I refresh it It expands and covers other controls. To make things short how can I set maximum size of control, and if there are more rows in that grid then make scroll appear ? thanks for any help, bye

    Read the article

  • What is your favourite cleverly written functional code?

    - by sdcvvc
    What are your favourite short, mind-blowing snippets in functional languages? My two favourite ones are (Haskell): powerset = filterM (const [True, False]) foldl f v xs = foldr (\x g a -> g (f a x)) id xs v -- from Hutton's tutorial (I tagged the question as Haskell, but examples in all languages - including non-FP ones - are welcome as long as they are in functional spirit.)

    Read the article

  • Why does ghci say that 1.1 + 1.1 + 1.1 > 3.3 is True?

    - by Frerich Raabe
    I've been going through a Haskell tutorial recently and noticed this behaviour when trying some simple Haskell expressions in the interactive ghci shell: Prelude> 1.1 + 1.1 == 2.2 True Prelude> 1.1 + 1.1 + 1.1 == 3.3 False Prelude> 1.1 + 1.1 + 1.1 > 3.3 True Prelude> 1.1 + 1.1 + 1.1 3.3000000000000003 Does anybody know why that is?

    Read the article

  • GHC 6.12 and MacPorts

    - by absz
    I recently installed GHC 6.12 and the Haskell Platform 2010.1.0.1 on my Intel MacBook running OS X 10.5.8, and initially, everything worked fine. However, I discovered that if I use cabal install to install a package which depends on a MacPorts library (e.g., cabal install --extra-lib-dirs=/opt/local/lib --extra-include-dirs=/opt/local/include gd), things work fine in GHCi, but if I try to compile, I get the error Linking test ... Undefined symbols: "_iconv_close", referenced from: _hs_iconv_close in libHSbase-4.2.0.0.a(iconv.o) "_iconv", referenced from: _hs_iconv in libHSbase-4.2.0.0.a(iconv.o) "_iconv_open", referenced from: _hs_iconv_open in libHSbase-4.2.0.0.a(iconv.o) ld: symbol(s) not found collect2: ld returned 1 exit status After some Googling, I found a long Haskell-cafe thread discussing this problem. The upshot seems to be that MacPorts installs an updated version of libiconv, and the binary interface is slightly different from the version included with the system. Consequently, if you try to link with any MacPorts library, the MacPorts libiconv gets linked in too; and since the base library was built to link against a different version of libiconv, things break. I've tried setting LD_LIBRARY_PATH and DYLD_LIBRARY_PATH and adding more flags to try to get it to look at /usr/lib again (e.g. cabal install --extra-lib-dirs=/opt/local/lib --extra-include-dirs=/opt/local/include --extra-lib-dirs=/usr/lib --extra-include-dirs=/usr/include gd), but neither worked. Uninstalling the MacPorts libiconv isn't really an option, since I have a bunch of ports installed which depend on it---including some ports I want Haskell to link to, like gd2. From what I've seen online, the upshot really seems to be "you're boned": you cannot link against any MacPorts library while compiling with GHC, and there doesn't seem to be a solution. However, that thread was from the end of 2009, so I figure there's a chance that someone has a solution, workaround, ridiculous hack… anything, really. So: does anybody know how to get GHC 6.12 to link against the system libiconv at the same time as it links to libraries from MacPorts? Or, failing that, a way to make linking not break in some other clever way?

    Read the article

  • Info on type family instances

    - by yairchu
    Intro: While checking out snoyman's "persistent" library I found myself wanting ghci's (or another tool) assistance in figuring out stuff. ghci's :info doesn't seem to work as nicely with type-families and data-families as it does with "plain" types: > :info Maybe data Maybe a = Nothing | Just a -- Defined in Data.Maybe ... > :info Persist.Key Potato -- "Key Potato" defined in example below data family Persist.Key val -- Defined in Database.Persist ... (no info on the structure/identity of the actual instance) One can always look for the instance in the source code, but sometimes it could be hard to find it and it may be hidden in template-haskell generated code etc. Code example: {-# LANGUAGE FlexibleInstances, GeneralizedNewtypeDeriving, MultiParamTypeClasses, TypeFamilies, QuasiQuotes #-} import qualified Database.Persist as Persist import Database.Persist.Sqlite as PSqlite PSqlite.persistSqlite [$persist| Potato name String isTasty Bool luckyNumber Int UniqueId name |] What's going on in the code example above is that Template-Haskell is generating code for us here. All the extensions above except for QuasiQuotes are required because the generated code uses them. I found out what Persist.Key Potato is by doing: -- test.hs: test = PSqlite.persistSqlite [$persist| ... -- ghci: > :l test.hs > import Language.Haskell.TH > import Data.List > runQ test >>= putStrLn . unlines . filter (isInfixOf "Key Potato") . lines . pprint where newtype Database.Persist.Key Potato = PotatoId Int64 type PotatoId = Database.Persist.Key Potato Question: Is there an easier way to get information on instances of type families and data families, using ghci or any other tool?

    Read the article

  • What are best practices for managing related Cabal packages?

    - by Norman Ramsey
    I'm working on a dataflow-based optimization library written in Haskell. It now seems likely that the library is going to have to be split into two pieces: A core piece with minimal build dependencies; call it hoopl-core. A full piece, call it hoopl, which may have extra dependencies on packages like a prettyprinter, QuickCheck, and so on. The idea is that the Glasgow Haskell Compiler will depend only on hoopl-core, so that it won't be too difficult to bootstrap the compiler. Other compilers will get the extra goodies in hoopl. Package hoopl will depend on hoopl-core. The Debian package tools can build multiple packages from a single source tree. Unfortunately Cabal has not yet reached that level of sophistication. But there must be other library or application designers out there who have similar issues (e.g., one package for a core library, another for a command-line interface, another for a GUI interface). What are current best practices for building and managing multiple related Haskell packages using Cabal?

    Read the article

  • Can parser combinators be made efficient?

    - by Jon Harrop
    Around 6 years ago, I benchmarked my own parser combinators in OCaml and found that they were ~5× slower than the parser generators on offer at the time. I recently revisited this subject and benchmarked Haskell's Parsec vs a simple hand-rolled precedence climbing parser written in F# and was surprised to find the F# to be 25× faster than the Haskell. Here's the Haskell code I used to read a large mathematical expression from file, parse and evaluate it: import Control.Applicative import Text.Parsec hiding ((<|>)) expr = chainl1 term ((+) <$ char '+' <|> (-) <$ char '-') term = chainl1 fact ((*) <$ char '*' <|> div <$ char '/') fact = read <$> many1 digit <|> char '(' *> expr <* char ')' eval :: String -> Int eval = either (error . show) id . parse expr "" . filter (/= ' ') main :: IO () main = do file <- readFile "expr" putStr $ show $ eval file putStr "\n" and here's my self-contained precedence climbing parser in F#: let rec (|Expr|) (P(f, xs)) = Expr(loop (' ', f, xs)) and loop = function | ' ' as oop, f, ('+' | '-' as op)::P(g, xs) | (' ' | '+' | '-' as oop), f, ('*' | '/' as op)::P(g, xs) -> let h, xs = loop (op, g, xs) let op = match op with | '+' -> (+) | '-' -> (-) | '*' -> (*) | '/' -> (/) loop (oop, op f h, xs) | _, f, xs -> f, xs and (|P|) = function | '('::Expr(f, ')'::xs) -> P(f, xs) | c::xs when '0' <= c && c <= '9' -> P(int(string c), xs) My impression is that even state-of-the-art parser combinators waste a lot of time back tracking. Is that correct? If so, is it possible to write parser combinators that generate state machines to obtain competitive performance or is it necessary to use code generation?

    Read the article

  • How to lazy process an xml documentwith hexpat?

    - by Florian
    In my search for a haskell library that can process large (300-1000mb) xml files i came across hexpat. There is an example in the Haskell Wiki that claims to -- Process document before handling error, so we get lazy processing. For testing purposes i have redirected the output to /dev/null and throw a 300mb file at it. Memory consumption kept rising until i had to kill the process. Now i removed the error handling from the process function: process :: String -> IO () process filename = do inputText <- L.readFile filename let (xml, mErr) = parse defaultParseOptions inputText :: (UNode String, Maybe XMLParseError) hFile <- openFile "/dev/null" WriteMode L.hPutStr hFile $ format xml hClose hFile return () As a result the function now uses constant memory. Why does the error handling result in massive memory consumption? As far as i understand xml and mErr are two seperate unevaluated thunks after the call to parse. Does format xml evaluate xml and build the evaluation tree of 'mErr'? If yes is there a way to handle the error while using constant memory? http://www.haskell.org/haskellwiki/Hexpat/

    Read the article

  • Haskell Parse Paragraph and em element with Parsec

    - by Tincho
    I'm using Text.ParserCombinators.Parsec and Text.XHtml to parse an input like this: this is the beginning of the paragraph --this is an emphasized text-- and this is the end\n And my output should be: <p>this is the beginning of the paragraph <em>this is an emphasized text</em> and this is the end\n</p> This code parses and returns an emphasized element em = do{ ;count 2 (char '-') ; ;s <- manyTill anyChar (count 2 (char '-')) ;return (emphasize << s) } But I don't know how to get the paragraphs with emphasized items Any ideas? Thanks!!

    Read the article

  • ghc6 install trouble: hGetContents: invalid argument (invalid UTF-8 byte sequence)

    - by olimay
    Having trouble installing ghc6 on Ubuntu Maverick via apt. Here's what seems to be the relevant error that comes up when I try to (apt-get|aptitude) install ghc6: A package failed to install. Trying to recover: Setting up ghc6 (6.12.1-13ubuntu1) ... ghc-pkg: /home/opm/.ghc/i386-linux-6.12.1/package.conf.d/unix-compat-0.2-edefa7bced91ebe610d455bab466e200.conf: hGetContents: invalid argument (invalid UTF-8 byte sequence) (Here's the full output, if you're interested: http://paste.ubuntu.com/566475/ ) This still happens after apt-get clean and apt-get update. My searching around has not really helped me understand what's going on, except that it might be caused by a mismatch in locale. So, here's the output of locale too: LANG=en_US.utf8 LANGUAGE=en_US:en LC_CTYPE="en_US.utf8" LC_NUMERIC="en_US.utf8" LC_TIME="en_US.utf8" LC_COLLATE="en_US.utf8" LC_MONETARY="en_US.utf8" LC_MESSAGES="en_US.utf8" LC_PAPER="en_US.utf8" LC_NAME="en_US.utf8" LC_ADDRESS="en_US.utf8" LC_TELEPHONE="en_US.utf8" LC_MEASUREMENT="en_US.utf8" LC_IDENTIFICATION="en_US.utf8" LC_ALL= Any ideas? Additional background: this all seems very strange to me, because I used to have ghc6 installed correctly--I use XMonad as my main window manager most of the time. I tried to install haskell-platform (through apt), which failed and told me that there was something wrong with ghc6, and so I reinstalled ghc6 and began to get the above error message.

    Read the article

  • Pattern matching in Perl ala Haskell

    - by Paul Nathan
    In Haskell (F#, Ocaml, and others), I can do this: sign x | x > 0 = 1 | x == 0 = 0 | x < 0 = -1 Which calculates the sign of a given integer. This can concisely express certain logic flows; I've encountered one of these flows in Perl. Right now what I am doing is sub frobnicator { my $frob = shift; return "foo" if $frob eq "Foomaticator"; return "bar" if $frob eq "Barmaticator"; croak("Unable to frob legit value: $frob received"); } Which feels inexpressive and ugly. This code has to run on Perl 5.8.8, but of course I am interested in more modern techniques as well.

    Read the article

  • Sending multiline message via sockets without closing the connection

    - by Yasir Arsanukaev
    Hello folks. Currently I have this code of my client-side Haskell application: import Network.Socket import Network.BSD import System.IO hiding (hPutStr, hPutStrLn, hGetLine, hGetContents) import System.IO.UTF8 connectserver :: HostName -- ^ Remote hostname, or localhost -> String -- ^ Port number or name -> IO Handle connectserver hostname port = withSocketsDo $ do -- withSocketsDo is required on Windows -- Look up the hostname and port. Either raises an exception -- or returns a nonempty list. First element in that list -- is supposed to be the best option. addrinfos <- getAddrInfo Nothing (Just hostname) (Just port) let serveraddr = head addrinfos -- Establish a socket for communication sock <- socket (addrFamily serveraddr) Stream defaultProtocol -- Mark the socket for keep-alive handling since it may be idle -- for long periods of time setSocketOption sock KeepAlive 1 -- Connect to server connect sock (addrAddress serveraddr) -- Make a Handle out of it for convenience h <- socketToHandle sock ReadWriteMode -- Were going to set buffering to LineBuffering and then -- explicitly call hFlush after each message, below, so that -- messages get logged immediately hSetBuffering h LineBuffering return h sendid :: Handle -> String -> IO String sendid h id = do hPutStr h id -- Make sure that we send data immediately hFlush h -- Retrieve results hGetLine h The code portions in connectserver are from this chapter of Real World Haskell book where they say: When dealing with TCP data, it's often convenient to convert a socket into a Haskell Handle. We do so here, and explicitly set the buffering – an important point for TCP communication. Next, we set up lazy reading from the socket's Handle. For each incoming line, we pass it to handle. After there is no more data – because the remote end has closed the socket – we output a message about that. Since hGetContents blocks until the server closes the socket on the other side, I used hGetLine instead. It satisfied me before I decided to implement multiline output to client. I wouldn't like the server to close a socket every time it finishes sending multiline text. The only simple idea I have at the moment is to count the number of linefeeds and stop reading lines after two subsequent linefeeds. Do you have any better suggestions? Thanks.

    Read the article

  • Sum of Fibonacci numbers

    - by Rafal
    Hi, I'm rather new to Haskell. The problem is to find the sum of all even Fibonacci numbers not greater than 4 million. I can't use lists. If I understand correctly, the below solution is wrong, because it uses lists: my_sum = sum $ filter (even) $ takeWhile (< 4000000) fibs Where fibs is the list of all Fibonacci numbers. Somehow, I find it difficult not to think in Haskell in terms of lists. Could anyone guide me to a solution to this problem? Regards

    Read the article

  • Can parser combination be made efficient?

    - by Jon Harrop
    Around 6 years ago, I benchmarked my own parser combinators in OCaml and found that they were ~5× slower than the parser generators on offer at the time. I recently revisited this subject and benchmarked Haskell's Parsec vs a simple hand-rolled precedence climbing parser written in F# and was surprised to find the F# to be 25× faster than the Haskell. Here's the Haskell code I used to read a large mathematical expression from file, parse and evaluate it: import Control.Applicative import Text.Parsec hiding ((<|>)) expr = chainl1 term ((+) <$ char '+' <|> (-) <$ char '-') term = chainl1 fact ((*) <$ char '*' <|> div <$ char '/') fact = read <$> many1 digit <|> char '(' *> expr <* char ')' eval :: String -> Int eval = either (error . show) id . parse expr "" . filter (/= ' ') main :: IO () main = do file <- readFile "expr" putStr $ show $ eval file putStr "\n" and here's my self-contained precedence climbing parser in F#: let rec (|Expr|) (P(f, xs)) = Expr(loop (' ', f, xs)) and shift oop f op (P(g, xs)) = let h, xs = loop (op, g, xs) loop (oop, f h, xs) and loop = function | ' ' as oop, f, ('+' | '-' as op)::P(g, xs) | (' ' | '+' | '-' as oop), f, ('*' | '/' as op)::P(g, xs) | oop, f, ('^' as op)::P(g, xs) -> let h, xs = loop (op, g, xs) let op = match op with | '+' -> (+) | '-' -> (-) | '*' -> (*) | '/' -> (/) | '^' -> pown loop (oop, op f h, xs) | _, f, xs -> f, xs and (|P|) = function | '-'::P(f, xs) -> let f, xs = loop ('~', f, xs) P(-f, xs) | '('::Expr(f, ')'::xs) -> P(f, xs) | c::xs when '0' <= c && c <= '9' -> P(int(string c), xs) My impression is that even state-of-the-art parser combinators waste a lot of time back tracking. Is that correct? If so, is it possible to write parser combinators that generate state machines to obtain competitive performance or is it necessary to use code generation?

    Read the article

  • What do you do when you feel you need a variatic list comprehension?

    - by cspyr0
    I would like to make a method where I could give it a list of lengths and it would return all combinations of cartesian coordinates up to those lengths. Easier to explain with an example: cart [2,5] Prelude> [ [0,0],[0,1],[0,2],[0,3],[0,4],[1,0],[1,1],[1,2],[1,3],[1,4] ] cart [2,2,2] Prelude> [ [0,0,0],[0,0,1],[0,1,0],[0,1,1],[1,0,0],[1,0,1],[1,1,0],[1,1,1] ] A simple list comprehension won't work because I don't know how long the lists are going to be. While I love Haskell's simplicity for many problems, this is one that I could write procedurally (in C or something) in 5 minutes whereas Haskell gives me an aneurysm! A solution to this specific problem would help me out a lot; I'd also love to hear about your thought processes when tackling stuff like this.

    Read the article

  • How to stop GHC from generating intermediate files?

    - by unknown
    When compiling a haskell source file via ghc --make foo.hs GHC always leaves behind a variety of intermediate files other than foo.exe. These are foo.hi and foo.o. I often end up having to delete the .hi and .o files to avoid cluttering up the folders. Is there a command line option for GHC not to leave behind its intermediate files? (When asked on #haskell, the best answer I got was ghc --make foo.hs && rm foo.hi foo.o.

    Read the article

  • Deriving PathInfo class in web routes?

    - by drozzy
    I am reading the web-routes tutorial in Happstack, and I have no idea what this is doing: $(derivePathInfo ''Sitemap) class PathInfo a where toPathSegments :: a -> [String] fromPathSegments :: URLParser a The doc simply says: we use template-haskell to derive an instance of PathInfo for the Sitemap type. but where does it "store" it? I thought haskell had no state, and is PathInfo our own thing, or is it part of happstack? If someone could explain this, for dummies? Thanks.

    Read the article

< Previous Page | 12 13 14 15 16 17 18 19 20 21 22 23  | Next Page >