0

This a working snippet:

import Turtle
...
groom :: FilePath -> IO ()
groom src = do
  view (ls src)
...

I can see a list of paths on the console. Actually I'd like to have something like [FilePath] for use, e.g.:

treeCount :: FilePath -> Int
treeCount src = length (lstree src)

Naturally, it won't compile, lstree being what it is:

lstree :: FilePath -> Shell FilePath

What is the correct way to treat this Shell thing? It's a newbie question, sorry.

Jan Hrcek
  • 626
  • 11
  • 24
Alexey Orlov
  • 2,412
  • 3
  • 27
  • 46

1 Answers1

1

I haven't actually tried this, but just looking at the type signatures the following might work:

import qualified Control.Foldl as F

treeCount :: FilePath -> IO Int
treeCount src = fold (lstree src) F.length

Fold with F.list to get [FilePath] instead.

melpomene
  • 84,125
  • 8
  • 85
  • 148
  • Couldn't match expected type ‘Shell FilePath -> Fold a1 Int -> Int’ with actual type ‘Fold a0 [a0]’ • The function ‘F.list’ is applied to two arguments, but its type ‘Fold a0 [a0]’ has none In the expression: F.list (lstree src) F.length In an equation for ‘treeCount’: `treeCount src = F.list (lstree src) F.length` – Alexey Orlov Dec 10 '17 at 19:01
  • @AlexeyOrlov That needs to be `fold (lstree src) F.list`. – melpomene Dec 10 '17 at 19:06
  • No instance for (MonadIO []) arising from a use of ‘fold’ – Alexey Orlov Dec 10 '17 at 19:12
  • @AlexeyOrlov Did you give it the wrong type signature? It should return `IO [FilePath]`. – melpomene Dec 10 '17 at 19:14
  • It compiles, thanks! I'm taking a timeout: I have to grok it. – Alexey Orlov Dec 10 '17 at 19:18
  • @AlexeyOrlov I'm not familiar with Shell, but there seems to be a lot going on here. `Shell` is a monad, where `Shell a` stands for a stream of `a` values, each produced with a side effect. The library provides functions like `Turtle.Shell.fold` to "convert" a `Shell` computation into an `IO` one, as long as a folding function is provided to "compress" the stream into a single value (e.g. `F.length` above). – chi Dec 10 '17 at 19:25
  • Thanks again! I'm falling behind, though. I hope for tomorrow. – Alexey Orlov Dec 10 '17 at 19:31
  • I am not the first one to be trapped by IO monad: https://stackoverflow.com/questions/11467066/how-to-get-normal-value-from-io-action-in-haskell – Alexey Orlov Dec 11 '17 at 09:21
  • There is even an icon: https://photos.smugmug.com/Humor/Lambdacats/trapd-in-IO-monad-plz-help/960526421_MnNqB-S-1.jpg – Alexey Orlov Dec 11 '17 at 09:22