'Initializing variables inside conditional statements [closed]

Why is it that whenever i initialize variables inside an if statement, for example, I always get a "pare error on input 'let'" enter image description here



Solution 1:[1]

There are at least two corrections that parse for me: attaching an in to each let or making the two let statements be part of a do block.

case size of
    2 ->
        let from = head temp in
        let to = head (tail temp) in
        ()

case size of
    2 -> do
        let from = head temp
        let to = head (tail temp)
        return ()

That said, using head and tail (and, usually, length, presumably used in the definition of size) is a code smell. Are you sure you didn't want this instead?

case temp of
    [from, to] -> ()

Solution 2:[2]

The problem with your code is that you have two let statement. You are only entitled to one let .. in or one .. where clause. When you use let you can have multiple assignments:

test :: (Eq a, Num a) => a -> IO ()
test x = case x of
    2 -> print "2"
    3 ->
        let 
            y=1
            z=2
        in 
        print $ y+z

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Daniel Wagner
Solution 2 Francis King