Skip to content

Instantly share code, notes, and snippets.

@bitemyapp
Created February 20, 2014 19:21
Show Gist options
  • Select an option

  • Save bitemyapp/9121239 to your computer and use it in GitHub Desktop.

Select an option

Save bitemyapp/9121239 to your computer and use it in GitHub Desktop.
λ> let blah = 1
λ> let blah = 2
λ> blah
2
λ> let blah x = x
λ> blah 1
1
λ> :t blah
blah :: t -> t
λ> let (blah x = x) :: Int -> Int
<interactive>:8:13: parse error on input `='
λ> let (blah x = x :: Int -> Int)
<interactive>:9:13: parse error on input `='
λ> let blah x = x :: Int -> Int
λ> :t blah
blah :: (Int -> Int) -> Int -> Int
λ> let (blah x = x) :: Int -> Int
<interactive>:12:13: parse error on input `='
λ> let blah x = x
λ> :t blah
blah :: t -> t
λ> let blah = (\x -> x) :: Int -> Int
λ> :t blah
blah :: Int -> Int
λ> let adder x y = x + y
λ> :t adder
adder :: Num a => a -> a -> a
λ> let partialAdder = adder 1
λ> partialAdder 2
3
λ> :t partialAdder
partialAdder :: Num a => a -> a
λ> map (+1) [0, 1, 2, 3]
[1, 2, 3, 4]
λ> :t map
map :: (a -> b) -> [a] -> [b]
λ> fmap (+1) [0, 1, 2, 3]
[1, 2, 3, 4]
λ> :t fmap
fmap :: Functor f => (a -> b) -> f a -> f b
λ> :info Maybe
data Maybe a = Nothing | Just a -- Defined in `Data.Maybe'
instance Eq a => Eq (Maybe a) -- Defined in `Data.Maybe'
instance Monad Maybe -- Defined in `Data.Maybe'
instance Functor Maybe -- Defined in `Data.Maybe'
instance Ord a => Ord (Maybe a) -- Defined in `Data.Maybe'
instance Read a => Read (Maybe a) -- Defined in `GHC.Read'
instance Show a => Show (Maybe a) -- Defined in `GHC.Show'
λ> Just 1
Just 1
λ> :t (Just 1)
(Just 1) :: Num a => Maybe a
λ> :t Nothing
Nothing :: Maybe a
λ> (+1) (Just 1)
<interactive>:30:1:
No instance for (Num (Maybe a0)) arising from a use of `it'
Possible fix: add an instance declaration for (Num (Maybe a0))
In a stmt of an interactive GHCi command: myPrint it
λ> -- fmap _ Nothing = Nothing
λ> -- fmap f (Just x) = Just (f x)
λ> fmap (+1) (Just 1)
Just 2
λ> fmap (+1) Nothing
Nothing
λ> fmap (+1) . fmap (*10) . fmap (+2) $ Nothing
Nothing
λ> fmap (+1) . fmap (*10) . fmap (+2) $ Just 10
Just 121
λ> fmap (+1) . fmap (*10) . fmap (+2) (Just 10)
<interactive>:37:26:
Couldn't match expected type `a0 -> f0 b0'
with actual type `Maybe b1'
In the return type of a call of `fmap'
Probable cause: `fmap' is applied to too many arguments
In the second argument of `(.)', namely `fmap (+ 2) (Just 10)'
In the second argument of `(.)', namely
`fmap (* 10) . fmap (+ 2) (Just 10)'
λ> fmap (+1) . fmap (*10) (fmap (+2) (Just 10))
<interactive>:38:13:
Couldn't match expected type `a0 -> f0 b0'
with actual type `Maybe b1'
In the return type of a call of `fmap'
Probable cause: `fmap' is applied to too many arguments
In the second argument of `(.)', namely
`fmap (* 10) (fmap (+ 2) (Just 10))'
In the expression: fmap (+ 1) . fmap (* 10) (fmap (+ 2) (Just 10))
λ> (fmap (+1) . fmap (*10) . fmap (+2)) Just 10
<interactive>:39:1:
No instance for (Num (Maybe a0)) arising from a use of `it'
Possible fix: add an instance declaration for (Num (Maybe a0))
In a stmt of an interactive GHCi command: myPrint it
λ> (fmap (+1) . fmap (*10) . fmap (+2)) (Just 10)
Just 121
λ> fmap (+1) . fmap (*10) . fmap (+2) $ Just 10
Just 121
λ> import Data.Monoid
λ> mempty ["blah"]
()
λ> mempty []
<interactive>:44:1:
Overlapping instances for Monoid ([a0] -> t)
arising from the ambiguity check for `it'
Matching givens (or their superclasses):
(Monoid ([a] -> t))
bound by the inferred type for `it': Monoid ([a] -> t) => t
at <interactive>:44:1-9
Matching instances:
instance Monoid b => Monoid (a -> b) -- Defined in `Data.Monoid'
(The choice depends on the instantiation of `t, a0')
When checking that `it'
has the inferred type `forall t a. Monoid ([a] -> t) => t'
Probable cause: the inferred type is ambiguous
λ> mempty [1, 2]
<interactive>:45:1:
Could not deduce (Monoid t)
arising from the ambiguity check for `it'
from the context (Num t1, Monoid ([t1] -> t))
bound by the inferred type for `it':
(Num t1, Monoid ([t1] -> t)) => t
at <interactive>:45:1-13
When checking that `it'
has the inferred type `forall t t1.
(Num t1, Monoid ([t1] -> t)) =>
t'
Probable cause: the inferred type is ambiguous
λ> mempty ["blah"]
()
λ> mempty 1
<interactive>:47:1:
Could not deduce (Monoid t)
arising from the ambiguity check for `it'
from the context (Num a, Monoid (a -> t))
bound by the inferred type for `it': (Num a, Monoid (a -> t)) => t
at <interactive>:47:1-8
When checking that `it'
has the inferred type `forall t a. (Num a, Monoid (a -> t)) => t'
Probable cause: the inferred type is ambiguous
λ> mappend "blah " "woot"
"blah woot"
λ> mappend [1, 2, 3] [4, 5, 6]
[1, 2, 3, 4, 5, 6]
λ> mconcat [[1, 2, 3], [4, 5, 6]]
[1, 2, 3, 4, 5, 6]
λ> :t mappend
mappend :: Monoid a => a -> a -> a
λ> :t mconcat
mconcat :: Monoid a => [a] -> a
λ> data Status = Accepted | Pending | Rejected
λ> Accepted
<interactive>:54:1:
No instance for (Show Status) arising from a use of `myPrint'
Possible fix: add an instance declaration for (Show Status)
In a stmt of an interactive GHCi command: myPrint it
λ> data Status = Accepted | Pending | Rejected deriving (Show)
λ> Accepted
Accepted
λ> read "1" :: Int
1
λ> read "[1, 2, 3" :: Int
*** Exception: Prelude.read: no parse
λ> read "[1, 2, 3]" :: Int
*** Exception: Prelude.read: no parse
λ> read "[1, 2, 3]" :: [Int]
[1, 2, 3]
λ> -- Accepted > Pending > Rejected
λ> -- mappend = min
λ> mappend [] [1, 2, 3]
[1, 2, 3]
λ> mappend (mempty :: [Int]) [1, 2, 3]
[1, 2, 3]
λ> :l Status.hs
[1 of 1] Compiling Status ( Status.hs, interpreted )
Status.hs:12:10:
Not in scope: type constructor or class `Monoid'
Perhaps you meant `Monad' (imported from Prelude)
Failed, modules loaded: none.
λ> :l Status.hs
[1 of 1] Compiling Status ( Status.hs, interpreted )
Status.hs:11:10:
No instance for (Eq Status)
arising from the superclasses of an instance declaration
Possible fix: add an instance declaration for (Eq Status)
In the instance declaration for `Ord Status'
Failed, modules loaded: none.
λ> :l Status.hs
[1 of 1] Compiling Status ( Status.hs, interpreted )
Ok, modules loaded: Status.
λ> mappend Accepted Pending
<interactive>:68:1:
No instance for (Show Status) arising from a use of `print'
Possible fix: add an instance declaration for (Show Status)
In a stmt of an interactive GHCi command: print it
λ> :l Status.hs
[1 of 1] Compiling Status ( Status.hs, interpreted )
Ok, modules loaded: Status.
λ> :l Status.hs
[1 of 1] Compiling Status ( Status.hs, interpreted )
Ok, modules loaded: Status.
λ> mappend Accepted Pending
Pending
λ> mappend Rejected Pending
Rejected
λ> mappend Rejected Accepted
Rejected
λ> mappend Accepted Accepted
Accepted
λ> mconcat [Accepted, Pending, Rejected]
Rejected
λ> foldr min [Accepted, Pending, Rejected]
<interactive>:76:1:
No instance for (Show ([[Status]] -> [Status]))
arising from a use of `print'
Possible fix:
add an instance declaration for (Show ([[Status]] -> [Status]))
In a stmt of an interactive GHCi command: print it
λ> min Accepted Pending
Pending
λ> head [0, 1, 2]
0
λ> tail [0, 1, 2]
[1,2]
λ> :t sum
sum :: Num a => [a] -> a
λ> foldr (+) [0, 1, 2, 3] 0
<interactive>:81:1:
No instance for (Num [[t0]]) arising from a use of `it'
Possible fix: add an instance declaration for (Num [[t0]])
In a stmt of an interactive GHCi command: print it
λ> foldr (+) 0 [0, 1, 2, 3]
6
λ> :t foldr
foldr :: (a -> b -> b) -> b -> [a] -> b
λ> foldr (+) 10 [0, 1, 2, 3]
16
λ> foldr (+) 1 [0, 1, 2, 3]
7
λ> scanr (+) 1 [0, 1, 2, 3]
[7,7,6,4,1]
λ> scanl (+) 1 [0, 1, 2, 3]
[1,1,2,4,7]
λ>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment