haskell

IO implementation inside Haskell

安稳与你 提交于 2021-02-05 06:06:55
问题 I know as a fact, that we cant implement an IO monad independently, but I dont know why exactly. This code is an attempt to implement an imperative paradigm using functional language. Can you explain the difference between this example and true IO? It looks like function main implements the correct action order and remains lazy. import System.IO.Unsafe data Io a = Io a runIO :: Io a -> a runIO (Io a) = a instance Monad Io where return x = Io x Io a >>= f = f a -- internal side effect function

Lazy evaluation in Haskell's do notation using the trace function

百般思念 提交于 2021-02-05 05:54:12
问题 I want to know why this "debug message 1" doesn't get printed in this snippet: import Debug.Trace main = do return (trace "debug message 1" ()) trace "debug message 2" (return ()) The second "debug message 2" is printed out, but not "debug message 1". It seems that both expressions are the same. I tried binding the "debug message 1" to a variable, and then using that variable in another place, it did in fact trigger the evaluation and print "debug message 1", but I still don't understand why

Lazy evaluation in Haskell's do notation using the trace function

时光总嘲笑我的痴心妄想 提交于 2021-02-05 05:54:05
问题 I want to know why this "debug message 1" doesn't get printed in this snippet: import Debug.Trace main = do return (trace "debug message 1" ()) trace "debug message 2" (return ()) The second "debug message 2" is printed out, but not "debug message 1". It seems that both expressions are the same. I tried binding the "debug message 1" to a variable, and then using that variable in another place, it did in fact trigger the evaluation and print "debug message 1", but I still don't understand why

How is it possible to collect all error messages in the Either Monad?

核能气质少年 提交于 2021-02-04 19:11:07
问题 I tried to validate the construction of a Record with Applicatives and the Either Monad . It works fine. But I can't see all Error Messages. Only the first is visible because the Right Path of the Either Monad ignores them. Here is my code: import Data.Either (either) import Text.Printf (printf) data Record = Record { fieldA :: String , fieldB :: String , fieldC :: String } deriving (Show, Eq) type Err = String setField :: String -> String -> Either Err String setField field value | length

Print List of Lists without brackets

一个人想着一个人 提交于 2021-02-04 17:52:40
问题 I am trying to print Pascals triangle up to some arbitrary row, after some thought I came up with this solution: next xs = zipWith (+) ([0] ++ xs) (xs ++ [0]) pascal n = take n (iterate next [1]) main = do n <- readLn :: IO Int mapM_ putStrLn $ map show $ pascal n Which works quite well, except for the printing. When I apply pascal 4 I get: [1] [1,1] [1,2,1] [1,3,3,1] When what I really want is this: 1 1 1 1 2 1 1 3 3 1 Is there any way I can do this? 回答1: Define your own pretty-printing

Quantified constraints vs. (closed) type families

吃可爱长大的小学妹 提交于 2021-02-04 17:24:08
问题 I am trying to use this blogpost's approach to higher-kinded data without dangling Identity functors for the trival case together with quantified-constraint deriving: {-# LANGUAGE TypeFamilies #-} {-# LANGUAGE QuantifiedConstraints, StandaloneDeriving, UndecidableInstances #-} module HKD2 where import Control.Monad.Identity type family HKD f a where HKD Identity a = a HKD f a = f a data Result f = MkResult { foo :: HKD f Int , bar :: HKD f Bool } deriving instance (forall a. Show a => Show

How can I constrain a QuickCheck parameter to a list of non-empty Strings?

二次信任 提交于 2021-02-04 15:49:16
问题 I have a property that takes a list of Strings: myProp :: [String] -> Bool I need to constrain the inputs that QuickCheck generates so that only non-empty strings are in the list. How can I do this? 回答1: You use forAll together with listOf (which generates lists) and listOf1 (which generates non-empty lists). Examples quickCheck $ forAll (listOf $ listOf1 arbitrary) $ myProp -- more verbose alternative to make things clear nonEmptyString :: Gen String nonEmptyString = listOf1 arbitrary

Class contraints for monads and monad functions

可紊 提交于 2021-02-04 15:08:43
问题 I am trying to write a new monad that only can contain a Num. When it fails, it returns 0 much like the Maybe monad returns Nothing when it fails. Here is what I have so far: data (Num a) => IDnum a = IDnum a instance Monad IDnum where return x = IDnum x IDnum x >>= f = f x fail :: (Num a) => String -> IDnum a fail _ = return 0 Haskell is complaining that there is No instance for (Num a) arising from a use of `IDnum' It suggests that I add a add (Num a) to the context of the type signature

Haskell : can only load one file at a time via :load

a 夏天 提交于 2021-02-04 13:19:04
问题 suppose I have two modules NecessaryModule1 & NecessaryModule2 (as outlined in the post Haskell : loading ALL files in current directory path. Then I have noticed in both WinGHCi and GHCi that if I do : > :load NecessaryModule1 [1 of 1] Compiling NecessaryModule1 ( NecessaryModule1.hs, interpreted ) Ok, modules loaded: NecessaryModule1. > addNumber1 2 3 5 > :load NecessaryModule2 [1 of 1] Compiling NecessaryModule2 ( NecessaryModule2.hs, interpreted ) Ok, modules loaded: NecessaryModule2. >

ZipList Monoid haskell

别等时光非礼了梦想. 提交于 2021-02-04 07:31:06
问题 The default monoid for lists in the GHC Prelude is concatenation. [1,2,3] <> [4,5,6] becomes [1,2,3] ++ [4,5,6] and thus [1,2,3,4,5,6] I want to write a ZipList Monoid instance that behaves like this: [ 1 <> 4 , 2 <> 5 , 3 <> 6 ] The result is [5,7,9] assuming I am using the sum monoid. Note this behaves like zipWith (+) Potentially it would behave like this: [ Sum 1 <> Sum 4 , Sum 2 <> Sum 5 , Sum 3 <> Sum 6 ] I need to create a newtype around the ZipList newtype and the Sum newtype in order