3

This is a reframe of the question that was asked at Defining variables inside a function Haskell

I have a function the beginning of which looks like this:

recursiveLs :: FilePath -> IO [FilePath]
recursiveLs dir =
   do
       folderExists <- doesDirectoryExist dir
       if folderExists
          then ...

The question is, how can I explicitly declare the type of folderExists before I assign to it in the action?

Community
  • 1
  • 1
David
  • 5,991
  • 5
  • 33
  • 39
  • 3
    By the way, note that you are not really "assigning" to `folderExists` here. That line is more like a function argument, and defines `folderExists` as an identifier in scope for the rest of the `do` block, with `doesDirectoryExist dir` providing the value `folderExists` is bound to. If you later assign another value to the name `folderExists`, that does not overwrite the earlier value; it simply shadows the `folderExists` previously defined in an outer scope. – C. A. McCann May 14 '13 at 18:28

1 Answers1

7

Well, let's try to do what you want in ghci:

> (a :: Integer) <- return 10

<interactive>:2:7:
    Illegal type signature: `Integer'
      Perhaps you intended to use -XScopedTypeVariables
    In a pattern type-signature

So, we should enable that pragma.

> :set -XScopedTypeVariables

And try again

> (a :: Integer) <- return 10
a :: Integer

Now we have a equal to 10, which is Integer:

> a
10
it :: Integer

Also, I believe that you've forgot about = in your recursiveLs function, there should be something like recursiveLs dir = do ...

  • Thank you --- it took me a while to figure out how to do this within Leksah as enabling that pragma under "1 Extensions" doesn't work. Also enabling it under "2 Extensions" does work" but of course now I have no idea what the difference is between "1" and "2" and why I have to do it twice! – David May 14 '13 at 17:31
  • [`{-# LANGUAGE ScopedTypeVariables #-}`](http://www.haskell.org/haskellwiki/Scoped_type_variables) – ДМИТРИЙ МАЛИКОВ May 14 '13 at 17:34
  • I'd also be interested in pointers to a discusion as to the value of "typing" variables, i.e. why is this option disabled by default? Is it generally considered a bad idea? – David May 14 '13 at 17:34
  • That works too, thank you. Does putting it in a module restrict the feature to that module? – David May 14 '13 at 17:36
  • Thanks - I fixed the "=" in my original example --- I was just focused on the variable declaration part at the time so was been sloppy writing the example. (No excuses!) – David May 14 '13 at 17:55
  • @David: It's disabled by default because it's not standard Haskell. GHC disables most non-standard extensions by default. – hammar May 14 '13 at 18:12
  • 1
    @David: The purpose of `ScopedTypeVariables` is actually to enable more complicated things and putting type annotations on a binding like this is not something people generally do in Haskell. It doesn't really serve any purpose and mostly makes the code harder to read. If you just need to restrict the type of the bound variable, putting an annotation where it's used works just as well. – C. A. McCann May 14 '13 at 18:19