haskell

Combining parsers in Haskell

守給你的承諾、 提交于 2020-03-20 12:43:37
问题 I'm given the following parsers newtype Parser a = Parser { parse :: String -> Maybe (a,String) } instance Functor Parser where fmap f p = Parser $ \s -> (\(a,c) -> (f a, c)) <$> parse p s instance Applicative Parser where pure a = Parser $ \s -> Just (a,s) f <*> a = Parser $ \s -> case parse f s of Just (g,s') -> parse (fmap g a) s' Nothing -> Nothing instance Alternative Parser where empty = Parser $ \s -> Nothing l <|> r = Parser $ \s -> parse l s <|> parse r s ensure :: (a -> Bool) ->

Combining parsers in Haskell

99封情书 提交于 2020-03-20 12:40:11
问题 I'm given the following parsers newtype Parser a = Parser { parse :: String -> Maybe (a,String) } instance Functor Parser where fmap f p = Parser $ \s -> (\(a,c) -> (f a, c)) <$> parse p s instance Applicative Parser where pure a = Parser $ \s -> Just (a,s) f <*> a = Parser $ \s -> case parse f s of Just (g,s') -> parse (fmap g a) s' Nothing -> Nothing instance Alternative Parser where empty = Parser $ \s -> Nothing l <|> r = Parser $ \s -> parse l s <|> parse r s ensure :: (a -> Bool) ->

Combining parsers in Haskell

若如初见. 提交于 2020-03-20 12:39:33
问题 I'm given the following parsers newtype Parser a = Parser { parse :: String -> Maybe (a,String) } instance Functor Parser where fmap f p = Parser $ \s -> (\(a,c) -> (f a, c)) <$> parse p s instance Applicative Parser where pure a = Parser $ \s -> Just (a,s) f <*> a = Parser $ \s -> case parse f s of Just (g,s') -> parse (fmap g a) s' Nothing -> Nothing instance Alternative Parser where empty = Parser $ \s -> Nothing l <|> r = Parser $ \s -> parse l s <|> parse r s ensure :: (a -> Bool) ->

Haskell Stack doesn't use system Ghc

旧时模样 提交于 2020-03-18 12:32:08
问题 I'm trying to use Haskell Stack to setup a project but using the stack provided ghc I am having a -fPIC error GitHub issue To bypass the fPIC problem I'm trying to use a package manager provided GHC using the 7.10.3 version (stack currently is using 8.0.2) When I try to force stack to use the systm ghc it simply ignores me. I've tried to override using /home/user/.stack/config.yaml and {project}/stack.yaml system-ghc : true option... but stack use the 8.0.2 version in the setup. Am I missing

What is the main difference between Free Monoid and Monoid?

一个人想着一个人 提交于 2020-03-18 11:10:37
问题 Looks like I have a pretty clear understanding what a Monoid is in Haskell, but last time I heard about something called a free monoid. What is a free monoid and how does it relate to a monoid? Can you provide an example in Haskell? 回答1: In a programming context, I usually translate free monoid to [a] . In his excellent series of articles about category theory for programmers, Bartosz Milewski describes free monoids in Haskell as the list monoid (assuming one ignores some problems with

Could not deduce (Eq a) from (Num a) or from (Floating a). But Can deduce (Eq a) from (Integral a). Why?

孤人 提交于 2020-03-17 10:18:00
问题 I'm going through a haskell tutorial (Learn you a haskell for great good) and I was playing around with this code I wrote based of off one of the functions in the book. reverseNum :: (Num a) => a -> a reverseNum 123 = 321 reverseNum x = 0 and ghci tells me that it can't deduce (Eq a) from (Num a). So I change the first line to this reverseNum :: (Integral a) => a -> a and it worked. This was strange because I thought to be a part of the Num typeclass you needed to also be apart of Eq. I tried

Could not deduce (Eq a) from (Num a) or from (Floating a). But Can deduce (Eq a) from (Integral a). Why?

偶尔善良 提交于 2020-03-17 10:15:28
问题 I'm going through a haskell tutorial (Learn you a haskell for great good) and I was playing around with this code I wrote based of off one of the functions in the book. reverseNum :: (Num a) => a -> a reverseNum 123 = 321 reverseNum x = 0 and ghci tells me that it can't deduce (Eq a) from (Num a). So I change the first line to this reverseNum :: (Integral a) => a -> a and it worked. This was strange because I thought to be a part of the Num typeclass you needed to also be apart of Eq. I tried

JetBrains 第二轮:再为免费全家桶续命三个月

白昼怎懂夜的黑 提交于 2020-03-14 13:00:44
昨天分享了 如何通过参与JetBrains的解密任务来获取正版全家桶的兑换码 。今天 JetBrains 一早继续在Twitter推出第二波任务: 下面,我们就继续来一起参与一下,为我们的正版JetBrains全家桶续个命! 线索一:Twitter 第一条线索还是比较容易的,通过观察不难发现,Twitter上推出字符串中的单词是倒置的! 所以,我们写个小程序来处理一下: @Test public void jetbrains() { String str = ".spleh A+lrtC/dmC .thgis fo tuo si ti semitemos ,etihw si txet nehw sa drah kooL .tseretni wohs dluohs uoy ecalp a si ,dessecorp si xat hctuD erehw esac ehT .sedih tseuq fo txen eht erehw si ,deificeps era segaugnal cificeps-niamod tcudorp ehT" ; String[] split = str.split(" "); String result = ""; for (int i =0;i < split.length;i++) { result += new StringBuffer

How do I override a Haskell package with a git / local package with Nix?

耗尽温柔 提交于 2020-03-14 05:20:04
问题 Essentially I'm using this: default.nix { nixpkgs ? import <nixpkgs> {}, compiler ? "ghc864" }: nixpkgs.pkgs.haskell.packages.${compiler}.callPackage ./gitchapter.nix { } gitchapter.nix { mkDerivation, base, directory, extra, filepath, foldl, hpack , HUnit, mtl, optparse-applicative, pandoc-include-code, parsec , pretty-simple, process, QuickCheck, rainbow, regex-pcre , regex-posix, safe, stdenv, string-conversions, system-filepath , template-haskell, text, transformers, turtle, unix ,

How do I override a Haskell package with a git / local package with Nix?

依然范特西╮ 提交于 2020-03-14 05:19:08
问题 Essentially I'm using this: default.nix { nixpkgs ? import <nixpkgs> {}, compiler ? "ghc864" }: nixpkgs.pkgs.haskell.packages.${compiler}.callPackage ./gitchapter.nix { } gitchapter.nix { mkDerivation, base, directory, extra, filepath, foldl, hpack , HUnit, mtl, optparse-applicative, pandoc-include-code, parsec , pretty-simple, process, QuickCheck, rainbow, regex-pcre , regex-posix, safe, stdenv, string-conversions, system-filepath , template-haskell, text, transformers, turtle, unix ,