haskell

Defining restricted subset of Storable for type match

时光总嘲笑我的痴心妄想 提交于 2020-01-24 11:05:04
问题 I am getting error for the code below - I suspect it has to do with the type signature of dispatch function which returns a Vector of type Storable a . What is the simple way to update dispatch function type signature to do only Int32 and CChar in type signature: {-# LANGUAGE BangPatterns #-} import Data.Vector.Storable as SV import Foreign.C.Types (CChar) import GHC.Int (Int32) data AList = I {-# UNPACK #-} !(SV.Vector Int32) | S {-# UNPACK #-} !(SV.Vector CChar) dispatch :: (Storable a) =>

Why does this function work even though the argument is missing?

痴心易碎 提交于 2020-01-24 11:04:06
问题 I'm trying to understand the following piece of code: import Data.Char (ord) encodeInteger :: String -> Integer encodeInteger = read . concatMap ch where ch c = show (ord c) But I don't see how this can work when encodeInteger is defined as a function that takes a string, but in the second line, the function is implemented without that string argument. Also, concatMap (according to hoogle), takes a function and a list, but only the function ch is provided. Why does this code still work? Is

Defining restricted subset of Storable for type match

自古美人都是妖i 提交于 2020-01-24 11:04:04
问题 I am getting error for the code below - I suspect it has to do with the type signature of dispatch function which returns a Vector of type Storable a . What is the simple way to update dispatch function type signature to do only Int32 and CChar in type signature: {-# LANGUAGE BangPatterns #-} import Data.Vector.Storable as SV import Foreign.C.Types (CChar) import GHC.Int (Int32) data AList = I {-# UNPACK #-} !(SV.Vector Int32) | S {-# UNPACK #-} !(SV.Vector CChar) dispatch :: (Storable a) =>

Capitalizing first letter of words while removing spaces (Haskell)

こ雲淡風輕ζ 提交于 2020-01-24 10:47:05
问题 I'm just starting out in Haskell and this is like the third thing I'm writing, so, naturally, I'm finding myself a little stumped. I'm trying to write a bit of code that will take a string, delete the spaces, and capitalize each letter of that string. For example, if I input "this is a test", I would like to get back something like: "thisIsATest" import qualified Data.Char as Char toCaps :: String -> String toCaps [] = [] toCaps xs = filter(/=' ') xs toCaps (_:xs) = map Char.toUpper xs I

Carry STRef implicitly in an environment during computation

这一生的挚爱 提交于 2020-01-24 09:20:06
问题 I am working on some bigger computation that requires use of mutable data in some critical moments. I want to avoid IO as much as I can. My model used to constist of ExceptT over ReaderT over State datatype, and now I want to replace State with mentioned ST . To simplify, let's assume I would like to keep single STRef with an Int during the whole computation, and let's skip the ExceptT outer layer. My initial idea was to put STRef s Int into ReaderT 's environment: {-#LANGUAGE Rank2Types#-} {

Haskell: Can't import System.Random

折月煮酒 提交于 2020-01-24 09:17:05
问题 This question was migrated from Super User because it can be answered on Stack Overflow. Migrated 17 days ago . I have installed Haskell on MacOS Mojave via the instructions found here, i.e. using the stack command. However, import System.Random brought in ghci the error message Could not find module ‘System.Random’ . By searching for a solution, I came accross this discussion on Stackoverflow, and I followed the suggestion posted there by Michael Snoyman to try the command stack install

parenthesis in Haskell functions

六眼飞鱼酱① 提交于 2020-01-24 08:50:23
问题 I just want to know how do we know which functions need brackets () and which ones do not? For example replicate 100 (product (map (*3) (zipWith max [1,2,3,4,5] [4,5,6,7,8]))) works fine. But replicate 100 (product (map (*3) (zipWith (max [1,2,3,4,5] [4,5,6,7,8])))) does not work. It is because I put a set of brackets for zipWith. In this small example, zipWith and max do not have brackets, but replicate, product and map do. In general is there a way to know/figure out which functions need

parenthesis in Haskell functions

倾然丶 夕夏残阳落幕 提交于 2020-01-24 08:48:47
问题 I just want to know how do we know which functions need brackets () and which ones do not? For example replicate 100 (product (map (*3) (zipWith max [1,2,3,4,5] [4,5,6,7,8]))) works fine. But replicate 100 (product (map (*3) (zipWith (max [1,2,3,4,5] [4,5,6,7,8])))) does not work. It is because I put a set of brackets for zipWith. In this small example, zipWith and max do not have brackets, but replicate, product and map do. In general is there a way to know/figure out which functions need

Why isn't Cabal hyperlinking my sources?

喜欢而已 提交于 2020-01-24 08:12:05
问题 $ cabal --version cabal-install version 1.22.2.0 using version 1.22.2.0 of the Cabal library $ cabal install --haddock-hyperlink-source gloss-juicy Warning: /home/theking/.cabal/config: Unrecognized field hyperlink-source on line 91 I do have hyperlink-source: True on in my config. I added the flag just to be redundant. When I look for the docs, it is all colorized and everything, but the source code isn't linkified. What am I doing wrong? 回答1: Hopefully we're talking about the haddock

Type Signature Mismatch on Bifunctor instance definition

穿精又带淫゛_ 提交于 2020-01-24 07:24:11
问题 I'm learning about Haskell by working through the book Haskell Programming from First Principles, Allen & Moronuki. In the exercises for the chapter on Monad Transformers, Functor & Applicative composition, it asks the reader to write Bifunctor instance for the following type data SemiDrei a b c = SemiDrei a My first attempt (which compiles) was instance Bifunctor (SemiDrei a) where bimap f g (SemiDrei a) = SemiDrei a But, looking at it, It seemed to me that I ought to be able to write bimap