haskell

elemIndices in Haskell

I wrote the code to get the element index elemIndex :: [String] -> [String] -> [Int] elemIndex [] [] = [] elemIndex x y = elemIndex True [(elem a y) | a <- x ] is there any alternative way/similer of performing the above logic? and also i've seen that some use index [] _ = [] to return null lists could you pls explain the use of...

nested if in haskell

//edit 5 when the i use only these 2 lines index :: [String] -> [String] -> Bool index a b = and [x `elem` a |x <- b] it works fine!!!! eg: index ["asd","asdd","dew"] ["asdd","asdad"] False But when i use the entire code mentioned below empty [] = True empty _ = False index a b = do if empty a ...

First column of a matrix given as a list of rows in Haskell

If I have a matrix given as a list of rows [[1,2,3],[4,5,6]], I want to return the first column, [1,4]. I'm an absolute beginner in Haskell and I have no idea on even how to deal with nested lists. ...

Higher-kinded generics in Java

Suppose I have the following class: public class FixExpr { Expr<FixExpr> in; } Now I want to introduce a generic argument, abstracting over the use of Expr: public class Fix<F> { F<Fix<F>> in; } But Eclipse doesn't like this: The type F is not generic; it cannot be parametrized with arguments <Fix<F>> Is this possible at ...

Haskell: Creating Type Classes for Zippers

So I've been reading a bit about the Zipper pattern in Haskell (and other functional languages, I suppose) to traverse and modify a data structure, and I thought that this would be a good chance for me to hone my skills at creating type classes in Haskell, since the class could present a common traversal interface for me to write code to...

Functional learning woes

I'm a beginner to functional languages, and I'm trying to get the whole thing down in Haskell. Here's a quick-and-dirty function that finds all the factors of a number: factors :: (Integral a) => a -> [a] factors x = filter (\z -> x `mod` z == 0) [2..x `div` 2] Works fine, but I found it to be unbearably slow for large numbers. So I m...

Hash tables using VLists

Phil Bagwell, in his 2002 paper on the VList data structure, indicates that you can use a VList to implement a persistent hash table. However, his explanation of how that worked didn't include much detail, and I don't understand it. Can anybody give me a more detailed explanation, or even examples? Further, it appears to me from what I ...

What is the type signature of this Haskell function?

I wrote a function to check whether a number is prime or not: prime n = prime' n 2 (floor (sqrt n)) where prime' n c u | n `mod` c == 0 = False | c > u = True | otherwise = prime' n (c+1) u I can't figure out what the type signature of this function should be. At first I thought it ...

Unable to have full-screen in Mac's Xmonad

I installed the newest X11. I run the following command unsuccessfully after sourcing ~/.profile $exec /usr/X11/lib/X11/xinit/xinitrc I get a message Screen is terminating and I do not see any evidence of XMonad. I run the command the second time, and I get zsh: permission denied: /usr/X11/lib/X11/xinit/xinitrc Process completed...

Haskell style/efficiency

So I was working on a way to lazily generate primes, and I came up with these three definitions, which all work in an equivalent way - just checking whether each new integer has a factor among all the preceding primes: primes1 :: [Integer] primes1 = mkPrimes id [2..] where mkPrimes f (x:xs) = if f (const True) x ...

"type error in conditional" in Haskell

My original code is given below and works fine. I wanted to add range check of 'ind' and in the modified version I added an if statement. When I run it I get a "type error in conditional", and I THINK its because of the output defintion [[String]] not IO()? Is there anyother way to check for the range of the value held in ind and produc...

Confusion about currying and point free style in Haskell

I was trying to implement the function every :: (a -> IO Bool) -> [a] -> IO Bool which was the topic for this question. I tried to do this without explicit recursion. I came up with the following code every f xs = liftM (all id) $ sequence $ map f xs My function didn't work since it wasn't lazy (which was required in the question),...

Could someone explain these Haskell functions to me?

I've dabbled with Haskell in the past, and recently got back into it seriously, and I'm reading real world haskell. Some of the examples they've shone, I've yet to understand. Such at this one: myLength [] = 0 myLength (x:xs) = 1 + myLength (xs) I don't see how this works, what is 1 really being added too? How is the recursion ret...

convert string list to int list in haskell

Hi, I've got a list of strings, is it possible to convert it to an list of ints? eg: ["1","2"] -> [1,2] THANKS, ...

What are zygo/meta/histo/para/futu/dyna/whatever-morphisms?

Is there a list of them with examples accessible to a person without extensive category theory knowledge? ...

Concurrent Haskell Actions with Timeout

Hi,how could one implement a function in concurrent haskell that either returns 'a' successfully or due to timeout 'b'? timed :: Int → IO a → b → IO (Either a b) timed max act def = do Best Regards, Cetin SertNote: the signature of timed can be completely or slightly different. ...

Functional paragraphs

Sorry I don't quite get FP yet, I want to split a sequence of lines into a sequence of sequences of lines, assuming an empty line as paragraph division, I could do it in python like this: def get_paraghraps(lines): paragraphs = [] paragraph = [] for line in lines: if line == "": # I know it could also be "if line:" ...

Haskell: can't use "map putStrLn" ?

Hi, I have a list of strings, and tried this: ls = [ "banana", "mango", "orange" ] main = do map PutStrLn list_of_strings That didn't work, and I can't understand why. ghc print-list.hs print-list.hs:3:0: Couldn't match expected type `IO t' against inferred type `[IO ()]' In the expression: main When checking the...

Efficient heaps in purely functional languages

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 h...

run a same haskell application take diff time

I wrote an application to analyze a log file using haskell. When I run it with the same log file, sometimes it costs 30s, and sometimes costs 20s, the execution time differs by up to 10 seconds. why is there such a large difference in running time? ...