haskell

What is the rule of the order of multiple type variables in haskell?

[亡魂溺海] 提交于 2020-01-03 07:17:13
问题 For example, ParsecT has multiple type variables in its definition. newtype ParsecT s u m a = ParsecT {unParser :: forall b . State s u -> (a -> State s u -> ParseError -> m b) -> (ParseError -> m b) -> (a -> State s u -> ParseError -> m b) -> (ParseError -> m b) -> m b } Can we do it like this ? newtype ParsecT m a s u -- Only the order of s u m a is changed to m a s u. = ParsecT {unParser :: forall b . State s u -> (a -> State s u -> ParseError -> m b) -> (ParseError -> m b) -> (a -> State

How does readsPrec and the relative functions return [Red] for read “[Red]” :: [Color]

戏子无情 提交于 2020-01-03 06:51:08
问题 This question is a continuation of what happens when executing (read "[Red]") :: [Color] under ghci?. From user5402's answer, I know that there is a very complex execution path for read "[Red]" :: [Color] , which includes readsPrec and readsPrecList . According to @user5402's comments, readsPrecList call the readsPrec , so readsPrec returns [(Red,"]")] to readsPrecList and then we will get the final result [Red] from readsPrecList . However, I still cannot understand what function of the link

What are GADTs?

百般思念 提交于 2020-01-03 06:49:13
问题 I was reading the GADTs for dummies page on the Haskell Wiki, and I still don't understand how and why they should be used. The author provided a motivating example: data T a where D1 :: Int -> T String D2 :: T Bool D3 :: (a,a) -> T [a] What exactly does this code do and why is it useful? If this question is a little too vague, perhaps a related question is: can GADTs be used to implement member functions? 回答1: Lets say you want to model a Fruit bag. This bag can have apple or oranges. So as

Getting GHC to produce “Add With Carry (ADC)” instructions

感情迁移 提交于 2020-01-03 06:48:10
问题 Here is code that adds two triples of unboxed Words representing a 192 bit number into a new triple of unboxed Words, and also returns any overflow: {-# LANGUAGE MagicHash #-} {-# LANGUAGE UnboxedTuples #-} import GHC.Prim(plusWord2#, Word#, or#) longAdd :: (# Word#, Word#, Word# #) -> (# Word#, Word#, Word# #) -> (# Word#, (# Word#, Word#, Word# #) #) longAdd (# xl, xm, xh #) (# yl, ym, yh #) = let plusWord3 x y c = let (# c1, r1 #) = plusWord2# x y (# c2, r2 #) = plusWord2# r1 c in (#

Removing items from a list if a predicate holds

断了今生、忘了曾经 提交于 2020-01-03 05:02:08
问题 I am trying to make a function which takes as input a predicate and a list. and removes all elements from the list for which the predicate holds. What I have so far is the following function: removeif :: func->[a]->[a] removeif [] = [] removeif func (h:t)= if func then delete h (h:t) else removeif func t I am confused about the func part of the func->[a]->[a] because I don't know how should I tell that its a predicate. For example what I want is that I give from the terminal this command

Haskell compare all list items

别说谁变了你拦得住时间么 提交于 2020-01-03 04:34:06
问题 Edit: it's hard to describe what I'm trying to do, but here's a try (from the comments): I am building a wordfeud solver, so I have a word, and some letters (both char list). I applied this ( How to find the frequency of characters in a string in Haskell? ) to both lists to get the frequency of all letters. What I'm doing now is iterating though the 'word' char list, and checking if all chars occur sufficiently in the 'letters' char list. I have written a Haskell function that compares two

Standard queue package for Haskell? [closed]

你离开我真会死。 提交于 2020-01-03 04:22:09
问题 Closed. This question is off-topic. It is not currently accepting answers. Want to improve this question? Update the question so it's on-topic for Stack Overflow. Closed 3 years ago . Is there a standard queue implementation for Haskell? I see several reasonably mature priority queue implementations, but no simple queues. Data.Sequence seems OK, but I assume we could get better performance with a more restricted datatype. Further, restricting the operations (ie, not a deque) can prevent bugs

Haskell cabal issue with Mac OS X 10.11? (cannot satisfy -package-id)

纵然是瞬间 提交于 2020-01-03 04:09:14
问题 I used the latest Haskell Platform 7.10.2-a (https://www.haskell.org/platform/mac.html) on Mac OS X 10.11 for El-capitan. When I tried to install yesod with cabal install yesod , I have multiple error messages such as: Building email-validate-2.1.3... Building http-api-data-0.2.1... Building fast-logger-2.4.1... Building http-date-0.0.6.1... Failed to install crypto-random-0.0.9 Build log ( /Users/smcho/.cabal/logs/crypto-random-0.0.9.log ): Configuring crypto-random-0.0.9... Building crypto

Where is Network.Socket.ByteString.Lazy's sendTo?

拟墨画扇 提交于 2020-01-03 03:10:28
问题 Both Network.Socket.ByteString and Network.Socket.ByteString.Lazy have a send function. Network.Socket.ByteString has a sendTo function, but Network.Socket.ByteString.Lazy doesn't. How can I use Network.Socket.ByteString 's sendTo with a Lazy.ByteString or Network.Socket.ByteString.Lazy 's send function. (i.e. how do I tell it where to send the packet.) Can anyone recommend a good tutorial on Haskell's Strings, BytesStrings. Lazy.ByteStrings, etc. as I find them very confusing (coming from a

Haskell: parse error on input 'putStrLn'

允我心安 提交于 2020-01-03 01:55:11
问题 I just wrote my first Haskell program, but there is an error that I cannot understand. I think it is right because I just wrote it like the example from a book. Could anyone help me please? main = do putStrLn "Hello, what's your name?" name <- getLine putStrLn ("Hey" ++ name ++ ", nice to meet you!") The error message is: parse error on input 'putStrLn' It is strange. 回答1: Though it's impossible to tell from your posted code because SO converts tabs to spaces at least some of the time, the