w3hello.com logo
Home PHP C# C++ Android Java Javascript Python IOS SQL HTML videos Categories
  Home » HASKELL » Page 1
The Simplest Variadic Function
The example here demonstrates how to use your code. The problem (correctly identified by @Cubic) is polymorphism. In particular, you can see in the example code calls the method with a type: main :: IO () main = do printAll 5 "Mary" "had" "a" "little" "lamb" 4.2 printAll 4 3 5 You need to do the same: foo :: Int foo = process (1 :: Int) (2 :: Int) (3 :: Int) This specifies the type

Categories : Haskell

What's the difference between partial evaluation and function inlining in a functional language?
There is a difference between evaluating constant expressions over a given set of operators and functions known by the compiler (or even preprocessor), which happens at compile time . E.g. the compiler compiles print(2*2) as print(4). This needs by no means to be limited to operator expressions, as you seem to imply (e.g. print(sqrt(2.0)) partial evaluation, which is a broader concept. A compil

Categories : Haskell

Type signatures that never make sense
You can't do anything interesting with x on it's own. You can do stuff with [x]; for example, you can count how many nodes are in the list. So, for example, foo :: (a -> a) -> [a] -> Bool foo _ [] = True foo _ (_:_) = False bar :: x -> [a] -> Bool bar _ [] = True bar _ (_:_) = False If you have an x and a function that turns an x into something else, you can do interesting stuf

Categories : Haskell

Error when using options fields in Yesod subsite form
It's usually best to run your forms in the master site, not the subsite, by calling lift. You'll also need to modify your type synonyms a bit to match, but the basic idea is to replace: (formWidget, _) <- generateFormPost testForm with (formWidget, _) <- lift $ generateFormPost testForm EDIT I still recommend the above approach. However, to get the alternate that you're asking for, ch

Categories : Haskell

Is it possible to implement the CONNECT HTTP method with the Snap framework?
I believe this is via escapeHttp. I discovered this by looking at the source for websockets-snap, which presumably is the right way to upgrade a connection. For reference, in the WAI world this is handled via the responseRaw function.

Categories : Haskell

Constraint Error with instance definition
If you want any type that is an instance of Num to be an instance of Adder, you can achieve that like: {-# LANGUAGE FlexibleInstances #-} {-# LANGUAGE UndecidableInstances #-} class Adder a where plus :: a -> a -> a instance Num a => Adder a where plus = (+) to add a String instance, you need one more language extension {-# LANGUAGE FlexibleInstances #-} {-# LANGUAGE Unde

Categories : Haskell

How to implement data streaming with the Snap framework?
It will work if you add any content type that's not "application/x-www-form-urlencoded" to your /write, e.g.: dd if=/dev/zero bs=1M count=100 | curl -H "Content-Type: application/json" --data-binary @- http://localhost:8000/write This bit in Snap does something like if contentType == Just "application/x-www-form-urlencoded" then readData maximumPOSTBodySize where maximumPOSTBodySize =

Categories : Haskell

Can we have any useful first order programs with infinite lists?
I used an infinite list to represent players in a game recently: let playerList = [PlayerA, PlayerB] let players = cycle playerList let currentPlayer = head players let advance = tail players -- "save" this to your players "variable" -- or, maybe clearer, inside of MonadState [Player] let advance' = modify tail I think you could come up with a lot more.

Categories : Haskell

Use of backslashes with variables in Haskell
Remember, this f = x -> <thing> is the same as this f x = <thing> Given that, perhaps your expression will make more sense to you if I give the parenthesis voltea = (f -> (x -> (y -> f y x))) is the same as voltea f = (x -> (y -> f y x)) is the same as voltea f x = y -> f y x is the same as voltea f x y = f y x Now, remember that in Haskell, funct

Categories : Haskell

ByteString.Lazy.Char8 (Not enough space)
There seems to be a problem with lazy bytestrings on 64-bit Windows 7, e.g. this pandoc patch (July 2013) a Haskell-Cafe posting (July 2013) an lpaste posting (March 2013) Since the reports are all from over a year ago, I would try using GHC 7.8.3 (for example, from the Haskell Platform 2014.2.0.0) if you are not already using that version.

Categories : Haskell

Learning Haskell `parsec`: trying to rewrite `words` function as a basic exercise
Update 2: Seems like this function gets the job done, though am not sure how idiomatic it is. It's fine, but it doesn't work as you intend: > words "Hello world" ["Hello","world"] > parse wordsReplica "" "Hello world" Right ["Hello","","","","","","world"] Not quite what you want. After all, a word should consist of at least one character. But if you change many to many1,

Categories : Haskell

Make Data Type of Kind * -> * That's Not a Functor
A type t of kind * -> * can be made an instance of Functor if and only if it is possible to implement a law-abiding instance of the Functor class for it. So that means you have to implement the Functor class, and your fmap has to obey the Functor laws: fmap id x == x fmap f (fmap g x) == fmap (f . g) x So basically, to solve this, you have to name some type of your choice and prove that the

Categories : Haskell

Profiling a partially evaluated program
Most likely, the second signal is being delivered before the program has finished handling the first one, and at this point the signal's action has been reset to the default action, which (for SIGINT) is to terminate the program. Because of the swapping, there's a significant interval before the profiling code can write out the profiling data, during which time the program is vulnerable to a secon

Categories : Haskell

How to pass arguments through recursive (tree) evaluation in Haskell
I can't see the rest of your code, so can't compose the remainder. Here is the idea of how you could pass "global state" along as the enact computes the branches of the tree: data Inter e r = None | Primitive e r | Composite (Inter e r) (Inter e r) deriving (Show, Read, Eq, Ord) enact :: (Eq e, Eq r) => Inter e r -> State a (Inter e r) enact (Comp

Categories : Haskell

Printing a list in Haskell
These are not "stupid" mistakes, but you're going to have to step back a bit from "what type do I write here" in order to make sense of what's going on. I notice you've asked a bunch of overlapping questions today surrounding these issues. I hope we as a community can get you an answer that will get you on the right track. In that light, I'm marking this post Community Wiki and encouraging others

Categories : Haskell

Why does `changes` return `Event t (Future a)`
The function changes returns different values than the function changes' that you describe. The key point is the following: Consider a Behavior defined by stepper (or accumB), which happens to change at time t0. What value does the Behavior have at this moment in time? The answer is that the Behavior takes on the new value for all times that are strictly larger than the time of change, t > t0, an

Categories : Haskell

where return goes on Haskell?
You can use show which converts its argument to a string: writePhrase :: Int -> String writePhrase x = "test " ++ show x ++ " hello"

Categories : Haskell

Functional dependencies in a particular case
Firstly, I have no idea why you are using such a complicated datatype - so I'll use a simplified one to address your question. The same principle applies to your exact scenario, however. Define the datatype simple as: data TList (xs :: [*]) where Nil :: TList '[] (:.) :: x -> TList xs -> TList (x ': xs) infixr 4 :. Then your class has one less type parameter: class TApplies f (xs

Categories : Haskell

How does Monad conform to the composition of function
Try to implement that function yourself and you will find that it would be useless: func :: Monad m => m a -> (m a -> m b) -> m b func x f = f x So what you are basically suggesting is applying a value to a function. I don't think we need a special function for that. :-) The whole point of >>= is that it executes the side effect of it's first parameter and then passes the resu

Categories : Haskell

Haskell - Creating a list by using functions
In the first pattern match you get a value of type list. Now all you have to do is concatenate them when you pattern match the Branch constructor. treeToList :: Tree a -> [a] treeToList (Leaf x) = [x] treeToList (Branch a b) = (treeToList a) ++ (treeToList b) Demo in ghci: *Main> treeToList (Branch (Branch (Leaf 2) (Leaf 3)) (Leaf 4)) [2,3,4]

Categories : Haskell

Adding the elements of a list of list in haskell (not concatenation but addition)
You might try conc = map sum to get the signature Num a => [[a]] -> [a] that you're looking for with a (+) operation between elements. In addition, summ has a horrid type signature and will give you all sorts of errors. You probably just meant sum since you call it on the output of conc. So for example this "just works": main = do putStrLn "Enter a list of lists of floats: " f

Categories : Haskell

Haskell type error. Can't match (Int -> Int) with (Bits b => Int -> b)
(Bits b=>a->b) means that you can extract any type instance of Bits, not just one. hash = id only works for type Int, the compiler is basically complaining "hey, what about all the other Bits instances?"

Categories : Haskell

How to avoid calling the same function with the same parameters twice?
You can do this using let or where: f x = let y = g x in if y > 0 then y else -2 Or f x = if y > 0 then y else -2 where y = g x There are subtle differences between the two here, the biggest one to keep in mind is that where can be used to define values that don't change in between function calls and the compiler will only define the value once, while a local let will be de

Categories : Haskell

How can I make this code compile and run? How to convert to primitive recursion?
You need to provide it with an input. value. You have compilePoly (PAdd PVar PVar) :: Int -> Int, and this can't be printed. Instead, try main = print $ compilePoly (PAdd PVar PVar) 1 This will be equivalent to (x -> x + x) 1, resulting in 2. What might make this more clear would be defining compilePoly a bit differently: compilePoly :: Poly -> Int -> Int compilePoly (PConst

Categories : Haskell

Hasql's session and IO
Yes, in your example you create a new session for every request, which is unacceptable. First of all, Session is just and alias to the reader monad transformer, which gives you a direct access to the pool. So you can always do: session postgresSettings sessionSettings $ do -- session' :: H.Session b m r -> m r session' <- flip runReaderT <$> ask let runApp request respond =

Categories : Haskell

Struggling with lists in Haskell
What is happening is that due to Haskell's laziness GHCi is printing out the first two sub-lists as it receives them, but then runs into an error. This error is because you haven't defined all the possible inputs for create_list_of_lists, namely when the first argument is empty. You'll need create_list_of_lists [] num = ??? create_list_of_lists (x:xs) num = [read x `div`z | z <- [1..num]] :

Categories : Haskell

Yet Another Haskell Rigid Type Variable Error
You're telling the compiler that content has type Parser (Content a), but the line causing the error is return $ Content $ unlines xs Since unlines returns a String, and the Content constructor has type a -> Content a, here you would have String ~ a, so the value Content $ unlines xs has type Content String. If you change the type signature of content to Parser (Content String) then it shou

Categories : Haskell

Creating a list of lists in Haskell
That's because you are passing it as list of String and not as list of Integer. This should work: *Main> create_list_of_lists [3123,11254,790,86214,114125,36214] 7 [11254,5627,3751,2813,2250,1875,1607,790,395,263,197,158,131,112,86214,43107,28738,21553,17242,14369,12316,114125,57062,38041,28531,22825,19020,16303,36214,18107,12071,9053,7242,6035,5173] If you want to pass it as a list of strin

Categories : Haskell

Use contents of a list as positional arguments to a single multi-argument function
I use usually match patterns like let (a:b:c:_) = args in func a b c or inlined ((a:b:c:_) -> func a b c) args if you really want do that you can create one operator to "inyect" element lists to any function showAdd2 <<< args but I think is not very usable... {-# LANGUAGE FlexibleInstances, UndecidableInstances, ConstraintKinds, InstanceSigs, MultiParamTypeClasses #-} class

Categories : Haskell

Using different created types on haskell
borrow db book person = (book, person) : db : is the cons operator, which takes an element and adds it to a list. (book, person) packs the book and person items into a single tuple. (book, person) : db therefore takes the book and person, packs them into a tuple, and adds that tuple to the list db.

Categories : Haskell

Using the haskell map function with a string
This answer refers to an old version of the question. So let's get this quite clear (please always add the type signature of all functions you're talking about!) function :: Char -> [String] Well, then the type of map function is [Char] -> [[String]], i.e. String -> [[String]]. But you want the result to be only [String], not a triply-nested list. You probably want to join the lists o

Categories : Haskell

puzzle function with fold in haskell?
It makes a list of n x-es like [x,x,x,x,x,..] (n times) and then what it does is: x1 = ( 1 + x/1)/2 x2 = (x1 + x/x1)/2 x3 = (x2 + x/x2)/2 x4 = (x3 + x/x3)/2 and the result is [xn,x(n-1),...,x2,x1]

Categories : Haskell

Haskell colon and dot syntax?
:. is just a data constructor that has been defined. You can play in ghci to get an idea: *Main> let a = Atom "test1" *Main> let b = Atom "test2" *Main> let c = a :. b *Main> c Atom "test1" :. Atom "test2" I have added deriving (Show) in your code so that it shows us String in the REPL. You can also see it's type: *Main> :t (:.) (:.) :: LispVal -> Lis

Categories : Haskell

Convert GHC.Int.Int64 to Int
You can turn any Integral type into another Num type using fromIntegral: fromInt64ToInt :: Int64 -> Int fromInt64ToInt = fromIntegral

Categories : Haskell

Proving foldr f st (xs++ys) = f (foldr f st xs) (foldr f st ys)
First: you can find the definition for many basic Haskell functions via the API documentation, which is available on Hackage. The documentation for base is here. foldr is exported in Prelude, which has a link to its source code: foldr :: (a -> b -> b) -> b -> [a] -> b foldr k z = go where go [] = z go (y:ys) = y `k` go ys It's defined like th

Categories : Haskell

Folding over an ITree
I'm trying to figure out the reasoning behind your foldTree signature. According to the Foldable section of the Typeclassopedia the generic fold operation has one of the following signatures: class Foldable t where fold :: Monoid m => t m -> m foldMap :: Monoid m => (a -> m) -> t a -> m foldr :: (a -> b -> b) -> b -> t a -> b foldl :: (a -> b -

Categories : Haskell

Taking in two functions and returning a function that adds their results
makePoly (Add p1 p2) = ( -> (makePoly p1) n + (makePoly p2) n) Explantation: Create the first poly: makePoly p1 Evaluate it at n: (makePoly p1) n Same with the second: (makePoly p2) n Add them together and wrap in a lambda. This should also tell you how to define add f g. You also have a problem here: makePoly (PConst n) = ->n ^ The third n will refer to t

Categories : Haskell

Haskell -- type signature on variable or function
From my comment Look at how aeson implements it, you have decode :: FromJSON a => ByteString -> Maybe a Whenever you use it interactively, you always have to specify it as decode "{}" :: Maybe MyType When you're using fromJValue (or decode) in larger projects, the majority of the time those polymorphic values get converted into concrete ones at some point, so the compiler can infer wh

Categories : Haskell

List to tuple in Haskell
Here is one way to do it, with the help of a helper function that lets you drop every second element from your target list, and then just use zip. This may not have your desired behavior when the list is of odd length since that's not yet defined in the question. -- This is just from ghci let my_list = ["Questions", "that", "may", "already", "have", "your", "correct", "answer"] let dropEvery []

Categories : Haskell




© Copyright 2018 w3hello.com Publishing Limited. All rights reserved.