> I wrote this code putting brevity over readability, which is something I usually never do Shouldn't the point of such a post be to show interesting code? I'm having trouble reading through the densely packed source. In addition to tromp's minor nitpick, I have several major ones. - the code is full of redundant parentheses. HLint can detect those (and many other style errors) automatically. LPaste has HLint install…
Hi, author here. I think you partially missed the main purpose of the article, which for me was just having fun by playing with a language I'm currently learning. I wasn't try to teach anything to anyone. But I must say, thanks for the great feedback! Lots of stuff I didn't know that we'll make me write better Haskell code :)
Haskell version of Norvig's spelling corrector
11–18 of 18 posts
Re: Haskell version of Norvig's spelling corrector
#12> I wrote this code putting brevity over readability, which is something I usually never do Shouldn't the point of such a post be to show interesting code? I'm having trouble reading through the densely packed source. In addition to tromp's minor nitpick, I have several major ones. - the code is full of redundant parentheses. HLint can detect those (and many other style errors) automatically. LPaste has HLint install…
Hi, author here. I think you partially missed the main purpose of the article, which for me was just having fun by playing with a language I'm currently learning. I wasn't try to teach anything to anyone. But I must say, thanks for the great feedback! Lots of stuff I didn't know that we'll make me write better Haskell code :)
Re: Haskell version of Norvig's spelling corrector
#13Re: Haskell version of Norvig's spelling corrector
#14> I wrote this code putting brevity over readability, which is something I usually never do Shouldn't the point of such a post be to show interesting code? I'm having trouble reading through the densely packed source. In addition to tromp's minor nitpick, I have several major ones. - the code is full of redundant parentheses. HLint can detect those (and many other style errors) automatically. LPaste has HLint install…
Hi, author here. I think you partially missed the main purpose of the article, which for me was just having fun by playing with a language I'm currently learning. I wasn't try to teach anything to anyone. But I must say, thanks for the great feedback! Lots of stuff I didn't know that we'll make me write better Haskell code :)
Re: Haskell version of Norvig's spelling corrector
#15Cool! Since we're suggesting changes, here's what I'd do. (Not that anything is wrong with the OP's code, just that it's good to point out all the different stylistic techniques you can adopt.) 7. alphabet = ['a'..'z'] 8. nWords = B.readFile "big.txt" >>= return . train . lowerWords . B.unpack or: 8. nWords = train . lowerWords . B.unpack B.readFile "big.txt" Make `splits`, `deletes`, etc. values (not functions). `sp…
Re: Haskell version of Norvig's spelling corrector
#16Which "proves" again that programming is neither about OO nor about purity..)
So there we can't see any "benefits" of truly-OO (original code has been written in a "functional style") or pure-functional approaches (the code has no "benefits" being converted into a pure-functional language).
Lists and Sets are "classes" in Python, but it doesn't matter, because implementation of "basic" types does not alter the behavior - sets could be implemented out of Lisp's conses.
Btw, knowing who the author is and seeing some "functional patterns" in Python code, it is very probable that original corrector has been prototyped/written in Common Lisp, then re-written in Python, and now re-written in Haskell.
The point was in an elegant algorithm and compact implementation, not in language of choice or in particular programming paradigm.
Re: Haskell version of Norvig's spelling corrector
#17Earlier quoted context omitted.
As long as we’re picking nits… > I wrote this code putting brevity over readability Overall, this is not particularly terse, for Haskell code. With all the lambdas, it looks like OCaml! For example, these are equivalent, and I find the latter clearer: (sortBy (\(_,c1)(_,c2) -> c2 `compare` c1)) sortBy (flip (comparing snd)) Now, it’s not necessarily a bad thing to be explicit, but in cases such as these, it’s less re…
For reverse sorting, there's a type that does specifically that. sortBy (comparing (Down . snd)) See http://hackage.haskell.org/package/base-4.7.0.1/docs/Data-Or...
Re: Haskell version of Norvig's spelling corrector
#18Cool! Since we're suggesting changes, here's what I'd do. (Not that anything is wrong with the OP's code, just that it's good to point out all the different stylistic techniques you can adopt.) 7. alphabet = ['a'..'z'] 8. nWords = B.readFile "big.txt" >>= return . train . lowerWords . B.unpack or: 8. nWords = train . lowerWords . B.unpack B.readFile "big.txt" Make `splits`, `deletes`, etc. values (not functions). `sp…
I used to compose return with a series of pure functions as well, but I found that using liftM seems cleaner.
nWords = liftM (train . lowerWords . B.unpack) (B.readFile "big.txt")
There was recently a very good article[0] about practically using monads that mentioned using liftM.However whenever using a functor instance is possible it's probably better, since functors can't do as much as monads. I'm not quite sure how much this would help/apply to this small example though.
0: http://softwaresimply.blogspot.com/2014/12/ltmt-part-3-monad...