Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Haskell: What is the difference between (+1) and (\x->x+1)?

Is there a difference between these two functions?

ghct says:

Prelude> :t (+1)
(+1) :: Num a => a -> a
Prelude> :t \x->x+1
\x->x+1 :: Num a => a -> a

But

When I used (+1) syntax in this piece of code:

data B = B { 
    pos :: Int, 
    cells :: [Int] 
} deriving (Show)

createB :: Int -> B
createB n = B 0 (take n $ repeat 0)

size :: B -> Int
size b = length $ cells b

get_curr :: B -> Int
get_curr b = (cells b) !! (pos b)

apply :: (Int -> Int) -> B -> B
apply f b = let n = pos b
                h = take n $ cells b       -- head
                t = drop (n + 1) $ cells b -- tail
                in B n $ h ++ [f (get_curr b)] ++ t

-- ...
eval :: [Char] -> StateT B IO ()
eval [] = return ()
eval (x:xs) = do
                b <- get

                put $ case x of
                        '+'         -> apply (+1) b
                        '-'         -> apply (-1) b
                        '>'         -> fwd b
                        '<'         -> back b
                        otherwise   -> b
                -- ...

prelude (as well as compiler) said:

> :load BrainFuck.hs 
[1 of 1] Compiling BrainFuck        ( BrainFuck.hs, interpreted )

BrainFuck.hs:49:40:
    No instance for (Num (Int -> Int))
      arising from the literal `1'
    Possible fix: add an instance declaration for (Num (Int -> Int))
    In the expression: 1
    In the first argument of `apply', namely `(- 1)'
    In the expression: apply (- 1) b
Failed, modules loaded: none.

What am I doing wrong? sorry if code is not-so-cool (full source here: https://github.com/nskeip/bf/blob/a755b2d27292593d63fe1e63c2a6e01cebc73520/BrainFuck.hs)

like image 909
Nikita Hismatov Avatar asked Feb 29 '12 18:02

Nikita Hismatov


People also ask

What does -> mean in Haskell?

(->) is often called the "function arrow" or "function type constructor", and while it does have some special syntax, there's not that much special about it. It's essentially an infix type operator. Give it two types, and it gives you the type of functions between those types.

What does X mean in Haskell?

It's a function which takes a list (whose items are the same type as x ) and outputs the same list with x added at the start.

What does <$> mean in Haskell?

It's merely an infix synonym for fmap , so you can write e.g. Prelude> (*2) <$> [1.. 3] [2,4,6] Prelude> show <$> Just 11 Just "11" Like most infix functions, it is not built-in syntax, just a function definition. But functors are such a fundamental tool that <$> is found pretty much everywhere.


2 Answers

This code:

(-1)

... doesn't mean the same thing as this code:

\ x -> x - 1

- is a special case in Haskell; it is the only prefix operator in the language. When you write (-1), you get "negative one" which is a number, and not "subtract one" which is a function.

You should use subtract 1 to get what you need.

like image 172
dflemstr Avatar answered Sep 21 '22 05:09

dflemstr


Your problem is not with (+1), it's with (-1):

Prelude> :t (-1)
(-1) :: Num a => a

-1 is a number! Try with apply (\x -> x-1) b or apply (subtract 1) b.

like image 30
Joni Avatar answered Sep 20 '22 05:09

Joni