prelude

Filed under: haskell

Still trying to work through the solution for the algorithm last week. I haven’t figured out how to translate nested loops yet, and peeking around on stackoverflow provides some hints about benefiting from learning about maps and functors (and dare I say, monads).

I’ve heard about these concepts in hushed tones but none of the advice dispensed nor read about has resonated yet… It’s supposed to take time. Truly waiting for that day when I wake up with the magical understanding of how to use these but until then I’ll periodically review these links:

Anyway… on to what I do know.

How do you use these in Haskell? They’re in Prelude and are available when you type ghci in your console to start the repl. You can check the :t type or :i info about the functions

The Prelude: a standard module imported by default into all Haskell modules.

Prelude> :t concat
concat :: Foldable t => t [a] -> [a]
Prelude> :t fmap
fmap :: Functor f => (a -> b) -> f a -> f b
Prelude> :t mapM
mapM :: Monad m => (a -> m b) -> t a -> m (t b)
Prelude> mapM (\x -> [x]) [0, 1, 2]
[[0,1,2]]
Prelude> mapM (\x -> [(x, x+1)]) [1, 2, 3]
[[(1,2),(2,3),(3,4)]]

One thing I learned about using ghci in emacs tonight is that the shell and eshell modes work great. The ansi term doesn’t show the results for the mapM examples above - don’t know why this happens but it’s pretty easy to switch to the other kind of shell(s). What’s the difference between {shell, term, ansi-term, eshell}? There’s minor differences so I don’t know the answer but based on skimming the answer it looks like shell is often most often used as it directly runs your machine’s shell whereas the other options are written in elisp.

Spacemacs has a configuration layer for the shell settings that will set your preferred option when you toggle the shell with: SPC ' I’m going to change the settings for that tonight.