haskell

Haskell way to do error checking of multiple items with abort

↘锁芯ラ 提交于 2020-01-11 05:17:35
问题 What is a good way for a Haskell function to check a number of different conditions and return an error message on a failure? In Python or similar language, it would be straightforward: if failure_1: return "test1 failed" if failure_2: return "test2 failed" ... if failure_n: return "testn failed" do_computation How do you do this without arbitrarily nested case/if statements in Haskell? Edit: some of the test conditions may require IO which puts any test results in the IO monad. I believe

Finite Field Linear Algebra Library for Haskell

旧城冷巷雨未停 提交于 2020-01-11 04:55:11
问题 I'm searching for a finite field linear algebra library for Haskell. Something like FFLAS-FFPACK for Haskell would be great :-). Of course, I checked hmatrix, there seems to be some support for arbitrary matrix element types but I couldn't find any finite field library which works with hmatrix. And surely I'd appreciate a performant solution :-) In particular I want to be able to multiply 𝔽 p n×1 and 𝔽 p 1×m matrices (vectors) to 𝔽 p n×m matrices. 回答1: Your best bet would be a binding to

What is the general case of QuickCheck's promote function?

Deadly 提交于 2020-01-11 04:50:06
问题 What is the general term for a functor with a structure resembling QuickCheck's promote function, i.e., a function of the form: promote :: (a -> f b) -> f (a -> b) (this is the inverse of flip $ fmap (flip ($)) :: f (a -> b) -> (a -> f b) ). Are there even any functors with such an operation, other than (->) r and Id ? (I'm sure there must be). Googling 'quickcheck promote' only turned up the QuickCheck documentation, which doesn't give promote in any more general context AFAICS; searching SO

Codifying presence/absence of authentication at type level

一曲冷凌霜 提交于 2020-01-11 04:42:08
问题 Context: I'm approaching Haskell from a standpoint of converting runtime errors to compile-time errors. My hypothesis is that this is possible if one can codify business logic within the program's types itself. I'm writing a Telegram bot, which should be accessible by users within my company. To achieve this "restriction", whenever someone starts chatting with the bot it will look up the chat_id in a table and check if a valid oauth_token exists. If not, the user will first be sent a link to

How to detect if a program has been compiled using -threaded?

寵の児 提交于 2020-01-11 04:32:05
问题 I'm working on a Haskell daemon that uses POSIX fork/exec together with file locking mechanism. My experiments show that file locks aren't inherited during executeFile with -threaded runtime (see also this thread), no matter if I use +RTS -N or not. So I'd like to add a check to be sure that the daemon ins't compiled with -threaded . Is there a portable way to detect it? 回答1: There is a value in Control.Concurrent for this, for example: module Main (main) where import Control.Concurrent main

Function to return a Haskell record with a modified field

不羁的心 提交于 2020-01-11 04:31:20
问题 Given : data MyRecord a = MyRecord{list :: [a], other_fields :: Char, …} I am trying to write a function which puts a new a on list and returns a new MyRecord : pushOntoList :: a -> MyRecord -> MyRecord Question : Is there a way to write pushOntoList is such a way that it does not depend on what is in the rest of the record, but simply gives it back unmodified? Another way to ask this is can you write pushOntoList without seeing the rest of the MyRecord definition? 回答1: Yes, very easily using

stack build results in “output was redirected with -o, but no output will be generated because there is no Main module.”

孤街浪徒 提交于 2020-01-11 03:50:07
问题 Didn't see any relevant result when google searching for this error so thought I'd post it. stack build Building all executables for `gitchapter' once. After a successful build of all of them, only specified executables will be rebuilt. gitchapter-0.1.0.0: build (exe) Preprocessing executable 'app' for gitchapter-0.1.0.0.. Building executable 'app' for gitchapter-0.1.0.0.. Preprocessing executable 'test' for gitchapter-0.1.0.0.. Warning: Enabling workaround for Main module 'Main' listed in

How do I pad string representations of integers in Haskell?

微笑、不失礼 提交于 2020-01-11 01:43:05
问题 I'l looking for an idiomatic (perhaps built-in) way of padding string representations of integers with zeros on the left. In my case the integers are never more than 99 so fix r = if length r == 1 then '0':r else r fix.show <$> [1..15] works. But I expect there is a better way. How do I pad string representations of integers in Haskell? 回答1: printf style formatting is availble via the Text.Printf module: import Text.Printf fmt x = printf "%02d" x Or to special case the formatting of 0: fmt 0

Once I have an F-Algebra, can I define Foldable and Traversable in terms of it?

爱⌒轻易说出口 提交于 2020-01-10 14:08:57
问题 I have defined an F-Algebra , as per Bartosz Milewski's articles (one, two): (This is not to say my code is an exact embodiment of Bartosz's ideas, it's merely my limited understanding of them, and any faults are mine alone.) module Algebra where data Expr a = Branch [a] | Leaf Int instance Functor Expr where fmap f (Branch xs) = Branch (fmap f xs) fmap _ (Leaf i ) = Leaf i newtype Fix a = Fix { unFix :: a (Fix a) } branch = Fix . Branch leaf = Fix . Leaf -- | This is an example algebra.

How do experienced Haskell developers approach laziness at *design* time?

断了今生、忘了曾经 提交于 2020-01-10 06:43:07
问题 I'm an intermediate Haskell programmer with tons of experience in strict FP and non-FP languages. Most of my Haskell code analyzes moderately large datasets (10^6..10^9 things), so laziness is always lurking. I have a reasonably good understanding of thunks, WHNF, pattern matching, and sharing, and I've been able to fix leaks with bang patterns and seq, but this profile-and-pray approach feels sordid and wrong. I want to know how experienced Haskell programmers approach laziness at design