r/haskellquestions • • Aug 08 '21

My impression after learning Haskell for a while

44 Upvotes

Well folks,

My weekend sure ends quickly. I am spending my entire weekend learning Haskell recently since I'm too busy working on my job during weekdays. After ~500 pages of the book Haskell programming first principles, I have to say that I have learned a lot. In summary:

- Type as first class citizen. Even though Haskell is static typed language, I feel like it's very much enjoyable to work with compare to Ruby (Yes, I know Ruby is a dynamic typed language).

- Thinking in terms of recursion. I feel like the language encourages me to think this way. I don't know if this will yield more benefits or bring more difficulties but it's surely new to me.

- Haskell don't have null and its way of providing an alternative way for null is ingenious. Well, thanks to the incredible type system.

Until now, Haskell is holding its end of the bargain: "being part of the solution" to me and I haven't even touched monad yet (I heard it's great).

That's all. See you guys next weekend. Happy learning!


r/haskellquestions • • Sep 28 '21

Compiling a list of upcoming Haskell books. Am I missing any?

33 Upvotes

r/haskellquestions • • Jun 09 '20

From a mathematician's point of view, I hate IEEE 754

28 Upvotes

Possibly unpopular opinion: I think that both div and / should return Maybe values to represent possible failure of division by zero, instead of returning a plain value or .

But this behaviour is ridiculous, integral division exceptions (the better behaviour IMHO of the below) and fractional division returns IEEE values!

Really, it's not Infinity. And NaN doesn't really help, as it doesn't "break" appropriately, per se. Just because something isn't a number doesn't mean that you can just be able to show it without a problem...

Seriously, look how ridiculous this is:

λ> 1 / 0
Infinity
λ> 0 / 0
NaN
λ> 1 `div` 0
*** Exception: divide by zero
λ> 0 `div` 0
*** Exception: divide by zero
λ> read "Infinity" :: Double
Infinity
λ> read "NaN" :: Double
NaN

Just... ugh.

I guess I should just use an alternative Prelude to do this...Sigh.

Not really a question, unless you count "whyyyyy..."


r/haskellquestions • • Nov 17 '21

Codebase to study for learning Haskell

22 Upvotes

Hello,

I'm trying to develop an intuition for good (idiomatic, efficient, maintainable) Haskell code and would like to look at some open source projects for this purpose. Could you recommend some codebase that is...

  • ... following current best practices closely enough,

  • ... not too big / complex, so I have a chance to understand what's going on in a reasonable amount of time.

  • ... application code rather than library code, could be a web application or a terminal script, could be something of general purpose (like e.g. a TODO list) or something technical (e.g. a function plotter), or maybe a simple game.

  • I have no preference for particular frameworks / libraries as long as it's mainly based on things that could still be recommended for use in a new project today.

Thank you in advance for your recommendations!


r/haskellquestions • • Apr 13 '20

Understanding the type of (.)(.)

21 Upvotes

Hello guys,

I have some issues understanding the type definition of two function compositions.
Function composition (.) is defined as

(.) :: (b -> c) -> (a -> b) -> a -> c
(.)(.) :: (a1 -> b -> c) -> a1 -> (a2 -> b) -> a2 -> c

I do not understand how the types add up here, could someone do the insertions step by step to help me understand?


r/haskellquestions • • Jan 14 '20

I wrote my first useful Haskell program! Code review?

21 Upvotes

The purpose of this program is to test the user on their knowledge of a list of vocabulary words. A word is shown, the definition is shown once the user presses the enter key. The user is then prompted (y/n), if y/yes the word is removed from the list, any other input and the word will be placed at the back of the list.

I've struggled with Haskell conceptually for a long time and this is the first program I have been able to write without structural assistance. If anyone could be so kind as to look at my code and provide any kind of feedback I would be very grateful.

https://gist.github.com/akilmarshall/c18dffa6bb67cff7e781ed5362953918


r/haskellquestions • • Sep 05 '21

What is the problem you think is hard to solve in Haskell, but easy in other languages and why?

20 Upvotes

Haskell is known for being very different from other languages. It requires understanding MONADS to understand how the Hello World programm works. It's paradigm is pretty restrictive, despite providing huge benefits of purity, first class functions, e.t.c.

Let's pretend it's the superior language NOT and talk about it's weak sides. It would be also nice to see a discussion like:" -I think X problem is kinda hard in Haskell, compared to other languages

  • Actually, this problem is beautifully solved by the Y library -Wow, I didn't know that, thanks"


r/haskellquestions • • Jun 02 '22

Public perception towards haskell is depressing to me

19 Upvotes

I heard ppl saying, "I know there are always some ppl favorable impression for other languages, even FP ones. Haskell, no one. Everyone I know dislikes it one way or another".

How much truth is in that saying? Do many ppl really dislike haskell? Does it deserve it? What do you think is the problem? While these are just hearsay, due to these occurrences, sometimes I wonder if I am delusional in using haskell. Perhaps I am just turning blind eye to any alternatives. So I'd be glad if you provide some perspectives.

  • By the way, it seems some ppl genuinely dislike the concept of monad after they understood it. Maybe ppl understood it but hated the idea of using intricate concept like monad to simulate imperative programming?


r/haskellquestions • • Apr 29 '23

Monadic bind understanding problem

18 Upvotes

I am puzzled why the following works correctly.

ghc> Identity 4 >>= (*10) >>= (+3)
Identity 43

Neither (*10) nor (+3) return an Identity value.

ghc> :t (>>=)
(>>=) :: Monad m => m a -> (a -> m b) -> m b

r/haskellquestions • • Jun 05 '21

Haskell toolset and stack in 2021

18 Upvotes

So I have a project I want to do in Haskell, mostly because I have a particular dependency that is only available in Haskell. I read a book, learned all about monads. I actually really like the language now and I'm excited to proceed with it.

But I can't for the life of me get the tooling to play nice. On both Windows and WSL, I've constantly had issues getting HLS to work in VS Code and importing the aforementioned dependency. After a lot of effort (and still no working editor support), I finally managed to import the latest git commit of what I wanted with stack, only to find that I now couldn't import a simple library like Data.Text.Format.

I'm now working on Linux virtual machines and willing to keep spinning up fresh installs of whatever distro until I have an environment that works.

I still can't get VS Code support with stack, even when I tried compiling HLS from source with the same stack snapshot, it keeps telling me the project was compiled with a different version.

I've had some luck with cabal, managed to get full VS Code support on my Lubuntu VM and even import the text formatting library, but my Github dependency won't build with cabal, only with stack.

So what steps do I take in June 2021 to go from a fresh OS install to having a working stack that plays nice with VS Code? And what should my workflow be for adding new dependencies? I've looked at older guides and answers but nothing ever seems to work right.


r/haskellquestions • • Oct 17 '20

Memoization feels brittle, how to be sure I'm not breaking it?

18 Upvotes

If i'm taking an example from https://wiki.haskell.org/Memoization :

memoized_fib :: Int -> Integer
memoized_fib = (map fib [0 ..] !!)
   where fib 0 = 0
         fib 1 = 1
         fib n = memoized_fib (n-2) + memoized_fib (n-1)

It is sufficent to write this function as:

memoized_fib :: Int -> Integer
memoized_fib i = map fib [0 ..] !! i
   where fib 0 = 0
         fib 1 = 1
         fib n = memoized_fib (n-2) + memoized_fib (n-1)

And poof! memoization is gone.

I'll be interested in : 1/ why explicitly giving this argument breaks memoization and 2/ how can I check (without benchmarking obviously) if I'm breaking memoization or not, in more complicated code?


r/haskellquestions • • Dec 09 '15

Crisis of Haskell faith

18 Upvotes

Hi all,

I'm having a kind of small crisis in my 'learning Haskell' journey and I thought that this would be the best place to ask some of the questions I have.

I read 'Learn you a Haskell' from cover to cover and mastered the material. As a result, I've gained an enormous appreciation for the language. I now think recursively by default and it's even helped my programming in my native tongues incredibly.

That being said, I'm having tremendous difficulty taking the next step Haskell-wise. By that I mean, every time I have a great idea for an application, I research approaches to solving it in Haskell, get scared, and wind up writing in in a more familiar language (in my latest one, I chose C... C!). I could solve math puzzles in Haskell like no one's business, but as far as using a graphics library, or a linear algebra library, I'm stymied. LYAH didn't discuss any tooling, best practices, or optimal work-flow tips.

I rarely need my hand holded, and I'm fairly adept at retrieving information but I either (a) can't get clear answers from my own research, or (b) I can't understand people's advice. For example, some of the questions that I can't find answers to include:

  • Is cabal out and stack in? Are they even analogous? If so, what do the cool kids use?

  • Is there a better source of documentation for a module than the docs on Hackage? I only really see a list of functions and their signatures and single sentence that is usually over my head. I know that sometimes you can unambiguously infer the functionality of a function from its signature, but am I to do this for every module I found on hackage?

  • If I wanted to write a streaming command-line utility, which of the 3+ string libraries should I use?

At the risk of sounding immodest, I'm really proficient in two prog langs, R and Python, and moderately proficient in a few others. I mention that only to show that it's not coding in general that's my problem and that I don't usually feel this discouraged when learning a language.... v v v TL;DR I'm having trouble taking my Haskell programming to the next level and it's discouraging. I have no idea how to get any substantial non-trivial development done and, for some reason, most forms of documentation aren't helping. Is this a common problem? Have others felt like this? If so, are you now grateful that you persevered? Am I doing the right thing by forcing myself through this? Thanks all!


r/haskellquestions • • Sep 30 '22

What is "Idiomatic Haskell Style"?

17 Upvotes

When programming in a language, I try to find out what the preferred style is in that language. Like pythonic code in python. Is there a so-called "idiomatic" style of programming in Haskell?

I've tried to glean a bit of how it would work from code I read elsewhere, and from (free) books on Haskell, but I don't have the full picture.

I understand everyone is different, and prefers different things, but to some extent there has to be some sort of consensus, right?

Keep in mind, I just finished a (free) online course in Haskell, so I'm still pretty new to the language, but I have a relatively strong grasp of the basics. (Took me a while to understand Monads, but I think I've mostly got it)


r/haskellquestions • • Nov 27 '21

Concurrency/Parallelism introductions?

17 Upvotes

Hi, would anyone be able to suggest some introductory materials regarding concurrency and/or parallelism please?

The difficulty I'm having is finding material that expects no experience with concurrency/parallelism, but (at least beginner-to-intermediate) experience with Haskell. Introductory material either tends to be in imperative languages - which is expected as they're arguably the predominant paradigm - or in Haskell, but with the expectation that you're familiar with concurrency and wanting to translate that knowledge to Haskell and/or wanting to learn what Haskell offers uniquely compared to most other languages, such as STM. I'd still like to learn those more specialized approaches for sure, but understanding a "better solution" is difficult when you have no idea how the "worse solutions" operate, for lack of better terms.

The book "Parallel and Concurrent Programming in Haskell" is great, but a bit too advanced for me at the moment. I got lost around the k-means example because I'm not too comfortable with mathematical algorithms like that in general, so trying to grok it as well as the parallel aspects was like hitting a brick wall.

The alternative I guess would be learning a functional language that focuses on this area of programming more specifically e.g. Erlang/Elixir, but I'm smitten by Haskell at the moment. Either that or learning in an imperative setting first which - whilst it could be instructive - seems completely backwards considering how functional languages are touted as being a superior approach for concurrent/parallel programming in particular. I'd rather not put on chains, as it were, only to then learn how to remove those chains when I could have just not put the chains on in the first place, for lack of a less terrible metaphor.

TIA :)

A couple of resources I've found so far:

http://www.macs.hw.ac.uk/~dsg/gph/docs/Gentle-GPH/sec-gph.html

https://github.com/crabmusket/haskell-simple-concurrency/blob/master/src/tutorial.md

P.S. I'm conflating concurrency and parallelism a bit in this post; material focusing on either that expects no prior knowledge of the subject (but prior knowledge of Haskell) would be great.


r/haskellquestions • • Nov 10 '21

Lots of copying?

17 Upvotes

Coming from C++, I just started learning Haskell a day or two ago. It seems like there’s a lot of copying going on, since everything is immutable, and that seems like a lot of memory. Is this accurate, or is everything a constant reference or something? if so, how can I use less memory?

Sorry if this is a dumb question, I’m very new to Haskell and functional programming.


r/haskellquestions • • Oct 11 '20

What is the recommended effects pattern for dummies in 2020?

17 Upvotes

Every few months I try to research which effects encapsulation pattern or library has been generally recommended as good, and it feels like every time I do I get a different answer. For a long time it was mtl, then tagless-final, then fused, back to mtl, etc etc. The community hasn't seemed to settle on a single pattern or library as the default option, and as a beginner/intermediate Haskeller I can't quite grasp the pros and cons of different options. It seems like a small transformer stack of Reade, Writer, Except, and maybe State is probably best for small scripts and applications.

Edit: Minutes after posting this, I read My thoughts on Haskell in 2020 which concludes:

Next time a beginner talks to you...Don't recommend any effect system. Massive abstractions can wait.


r/haskellquestions • • Mar 20 '19

What is (x:xs)?

17 Upvotes

Can someone please explain what is (x:xs) in haskell?


r/haskellquestions • • Aug 05 '25

Why aren't compiler messages more helpful?

16 Upvotes

Hello all. I'm new to Haskell, not at all new to programming.

Recently I've been trying out a few off-the-beaten-path programming languages (e.g. C3, Raku, Hare, V, Racket), and I'm currently looking at Haskell. One thing that has surprised me about non-mainstream languages in general, is that the error messages delivered by their respective compilers are often surprisingly hard to understand -- not impossible, but pretty difficult. This surprises me especially when the language has been in use for quite a while, say a decade or more, because I would expect that over the years the compiler code would accrue more and more and more hand-coded heuristics based on developer feedback.

Why do I bring this up in the Haskell subreddit? Well, guess what. In attempt to familiarize myself with Haskell, I'm following the book Learn You a Haskell for Great Good! by Miran Lipovaca. In chapter 2, the reader is introduced to the REPL. After a few basic arithmetic expressions, the author gives his first example of an expression that the REPL will not be able to evaluate. He writes:

What about doing 5 + "llama" or 5 == True? Well, if we try the first snippet, we get a big scary error message!

No instance for (Num [Char ]) arising from a use of ‘+’ at <interactive >:1:0 -9
Possible fix: add an instance declaration for (Num [Char ])
In the expression: 5 + "llama"
In the definition of ‘it ’: it = 5 + "llama"

Yikes! What GHCI is telling us here is that "llama" is not a number and so it doesn’t know how to add it to 5. Even if it wasn’t "llama" but "four" or "4", Haskell still wouldn’t consider it to be a number. + expects its left and right side to be numbers.

(End of quote from the book.) Actually since the publication of the book the error message has changed slightly. From GHCi 9.12.2 I get:

<interactive>:1:1: error: [GHC-39999]
No instance for 'Num String' arising from the literal '5'.
In the first argument of '(+)', namely 5.
In the expression: 5 + "llama"
In an equation for 'it': it = 5 + "llama"

Apparently some work has been done on this particular error message since the book was written. However, IMO both the old and the new message are remarkably cryptic, focusing on the first argument to the + operator (while in fact the second operand is the problem) and cryptically proposing that an "instance declaration" might help (while in fact no such thing is needed).

The problem is of course simply that the + operand requires both its operands to be a number type. Why doesn't the Haskell compiler identify this as the most likely cause of the error?

One could ask: do other languages (than Haskell) do better? Well, yes. Let's take Java as an example, a very mainstream language. I had to change the example slightly because in Java the + operator is actually overloaded for Strings; but if I create some other type Llama and instantiate it as llama, then use it as an operand in 5 + llama, here's what I get:

test1/BadAdd.java:5: error: bad operand types for binary operator '+'
                System.out.println(5 + llama);
                                     ^
  first type:  int
  second type: Llama
1 error

"Bad operand types for binary opreator +". That's very clear.

As stated, I'm wondering, both in the specific case of Haskell, and in the general case of other languages that have been around for a decade or more, why compiler messages can't match this level of clarity and helpfulness. Is there something intrinsic about these languages that makes them harder to parse than Java? I doubt it. Is it a lack of developer feedback? I'd be interested to know.


r/haskellquestions • • Sep 13 '22

Is Learn You a Haskell for Great Good! old-fashioned?

16 Upvotes

I read A Tour of C++, Python Tutorial, and The Rust Book to learn them, and I am a simple man so I want to read haskell official tutorial. But it seems that Haskell does not have an official tutorial.

If so, how about Learn You a Haskell for Great Good! It is published in 2010, wouldn't it be so old-fashioned? Is there any other book you will recommend?


r/haskellquestions • • Apr 03 '16

What is ->, really?

16 Upvotes

I've been learning Haskell for a few weeks and re-implementing a lot of basic features like the List type and the Functor and Monad typeclasses in order to understand them better.

Not long ago I found out that (->) is actually a type constructor, and this has made me feel uneasy.

On one hand, this makes sense. I can understand that (->) Float Float or (->) String Int are types, not so different from Bool and Char. I also understand that the (-> e) type constructor belongs to the Functor typeclass, with the implementation of fmap simply being (.), in the same way that the implementation of fmap for the [] type constructor is map.

On the other hand, (->) feels a bit like black magic. I can type

 f x = x : "foo"

and suddenly f has a type of Char -> [Char]. How on earth did that happen? Where did the (->) come from? Did I use a value constructor somewhere that gave me the type Char -> [Char]?

Is there anyway I can understand (->) in terms of basic language features? Is it something I can re-implement from scratch (data (->) a b = ...), or is there just some black magic going on that I have to accept?


r/haskellquestions • • Apr 24 '23

Can anyone please explain what tf the SelectT transformer is, how to use and understand it?

15 Upvotes

Recently stumbled upon this gem and it looks even worse than ContT. It says it is " Selection monad transformer, modelling search algorithms.", but there no much description, methods and I don't know any tutorial on it yet. I really struggled with ContT and this one looks even more intimidating.

Any help?


r/haskellquestions • • Feb 21 '21

What are the point of Monads ?

15 Upvotes

Been learning Haskell in my CS course but I don’t really get the point of Monads? Like what’s the actual reason for them ?


r/haskellquestions • • Mar 23 '12

Welcome!

15 Upvotes

This is my humble attempt to provide a service to people who, like me, have questions about programming using the Haskell language. This subreddit is intended as a friendly place of assistance and understanding, mainly for those who want to ask questions in a familiar environment. I really hope that this is a welcome gesture, and that people will gather here to help others out.

Thank you!


r/haskellquestions • • May 07 '23

Using VSCode with Haskell and stack

14 Upvotes

I learn/write Haskell in VSCode with the "Haskell for Visual Studio Code"-plugin (provides hls support for VSCode) on Windows.

On its own it works perfectly fine but now I am trying to create my first project using stack and this stops hls from working.

Starting a new project with stack works fine:

  1. stack new test

  2. in the folder test run stack build

But then hls has two problems:

in src/Lib.hs

Failed to parse result of calling stack
Configuring GHCi with the following packages: test
C:\Users\jbitterlich\AppData\Local\hie-bios\wrapper-340ffcbd9b6dc8c3bed91eb5c533e4e3.exe: startProcess: permission denied (Permission denied)

and in test/Spec.hs:

Failed to parse result of calling stack
Using main module: 1. Package `test' component test:test:test-test with main-is file: C:\Users\jbitterlich\github\haskell_projects\test\test\Spec.hs
test> configure (lib + test)
Configuring test-0.1.0.0...
test> initial-build-steps (lib + test)
test> Test running disabled by --no-run-tests flag.
Completed 2 action(s).
The following GHC options are incompatible with GHCi and have not been passed to it: -threaded
Configuring GHCi with the following packages: test
C:\Users\jbitterlich\AppData\Local\hie-bios\wrapper-340ffcbd9b6dc8c3bed91eb5c533e4e3.exe: startProcess: permission denied (Permission denied)

I found this question online that seems to be related, but I am too much overwhelmed right now by cabal, stack, ghcup that I don't know what to do with this information:https://stackoverflow.com/questions/73155847/using-vscode-with-haskell-ghcup-and-stack-hls-crashes-with-newer-versions-of