小编npo*_*cop的帖子

Haskell - Prime Powers Excercise - 无限合并

在大学,我的任务如下:

定义以下函数:

primepowers :: Integer -> [Integer]
Run Code Online (Sandbox Code Playgroud)

计算给定参数n的素数的前n个幂的无限列表,排序为asc.也就是说,primepowers n按升序包含元素

{p ^ i | p是素数,1≤i≤n}.

在完成这项任务后,我走到了尽头.我有以下四个功能:

merge :: Ord t => [t] -> [t] -> [t]
merge [] b = b
merge a [] = a
merge (a:ax) (b:bx)
  | a <= b    = a : merge ax (b:bx)
  | otherwise = b : merge (a:ax) bx

primes :: [Integer] 
primes = sieve [2..]
    where sieve [] = []
          sieve (p:xs) = p : sieve (filter (not . multipleOf p) xs) …
Run Code Online (Sandbox Code Playgroud)

recursion haskell infinity

2
推荐指数
2
解决办法
614
查看次数

从Haskell中的列表中删除重复的元素

我是Haskell的初学者.我只是想知道如何实现一个从数组中删除repeat元素的函数.例如,[1,1,1,3,4,2,2,3],结果应为[1,3,4,2].我不想使用像元素这样的现有函数,并通过使用递归来实现它.我的想法是比较x:xs,如果x是重复元素,则执行递归,否则重新运行该函数.这是正确的,如何通过代码实现这一点?

haskell

2
推荐指数
1
解决办法
7259
查看次数

封装GADT模式匹配

在这段代码中有重复的片段:

insert x (AATree t) = case insert' x t of
    Same t -> AATree t
    Inc t  -> AATree t

insertBlack :: (Ord a) => a -> AANode Black (Succ n) a -> AnyColor (Succ n) a
insertBlack x (Black l y r)
    | x < y     = case insert' x l of
          Same l' -> AnyColor $ Black l' y r
          Inc  l' -> AnyColor $ skew l' y r
    | otherwise = case insert' x …
Run Code Online (Sandbox Code Playgroud)

refactoring haskell pattern-matching gadt

2
推荐指数
1
解决办法
208
查看次数

Haskell加入getHomedirectory字符串

我有一个包含代表目录的字符串的文件.其中一些字符串中有一个波浪号(〜).我想将用户的homedirectory(〜)加入到字符串的其余部分.到目前为止我所拥有的:

import Data.List (isPrefixOf)
import System.Directory (doesDirectoryExist, getHomeDirectory)
import System.FilePath (joinPath)

getFullPath s
    | "~" `isPrefixOf` s = joinPath [getHomeDirectory, tail s]
    | otherwise          = s
Run Code Online (Sandbox Code Playgroud)

但是我收到以下错误:

Couldn't match type `IO FilePath' with `[Char]'Expected type: FilePath Actual type: IO FilePathIn the expression: getHomeDirectoryIn the first argument of `joinPath', namely `[getHomeDirectory, tail s]'In the expression: joinPath
Run Code Online (Sandbox Code Playgroud)

我不知道,我找不到,如何转换类型,使它们匹配,并可以连接在一起.

string haskell filepath

2
推荐指数
1
解决办法
850
查看次数

optparse-applicative 子命令帮助文本

我正在使用 stackage lts 5.1 附带的 optparse-applicative 我有一个带有子命令的解析器,并且我已经描述了它们的选项的帮助文本,但它们没有显示。

这是我运行可执行文件时的输出--help

[david@devcentos65 manipro]$ /home/david/.local/bin/manipro --help
manipro - text1

Usage: manipro COMMAND [-v|--verbose]   text2

Available options:  
  -h,--help                Show this help text  
  -v,--verbose             text3

Available commands:   
  export                   text4
  dico                     text9
Run Code Online (Sandbox Code Playgroud)

代码 :

parserArgs :: ParserInfo ArgApp
parserArgs = info (helper <*> args) desc
    where
    desc =  
        fullDesc <> 
        progDesc "text1"  <> 
        header "text2"


args = ArgApp <$> argCmd <*> optverbose
    where
    optverbose = switch ( 
        short 'v' <> long "verbose" <> 
        help "text3" …
Run Code Online (Sandbox Code Playgroud)

haskell command-line-arguments applicative optparse-applicative

2
推荐指数
1
解决办法
1040
查看次数

使用optparse-applicative解析"枚举"选项

如何从以下方面为此示例实现解析器grep --help:

 --binary-files=TYPE   assume that binary files are TYPE;
                       TYPE is 'binary', 'text', or 'without-match'
Run Code Online (Sandbox Code Playgroud)

假设我有

data BinaryFiles = Binary | Text | WithoutMatch
Run Code Online (Sandbox Code Playgroud)

我该如何编写解析器?option auto似乎是一个kludge,因为Read它应该是一个"逆" Show,我想保持派生instance Show BinaryFiles.

haskell optparse-applicative

2
推荐指数
1
解决办法
227
查看次数

在Haskell中使用保护程序编写产品函数

我需要以product两种方式编写函数:

  1. 使用卫兵
  2. 使用if-then-else

这样函数返回m到n的乘积.

例:

product 3 5

返回3*4*5 = 60

谢谢

haskell if-statement product function

1
推荐指数
1
解决办法
890
查看次数

将函数提升到IO monad中以解析从文件中读取的json字符串

我有问题,readFile返回一个IO String但解析期望一个常规String(或在下面的例子中ByteString).我虽然只是使用,liftM但下面的例子仍然失败.问题是什么?

import Data.Aeson
import Data.Attoparsec
import Data.ByteString (ByteString, pack)
import Data.Maybe
import Network.HTTP
import Network.URI
import qualified Data.ByteString.Char8 as C
import Control.Monad

main = do
    myres  <-  liftM parse json (C.readFile "dummy.json")
    print myres
Run Code Online (Sandbox Code Playgroud)

错误:

Couldn't match expected type `t0 -> Parser a0'
            with actual type `Parser Value'
In the second argument of `liftM', namely `json'
In a stmt of a 'do' block:
  myres <- liftM parse json (C.readFile …
Run Code Online (Sandbox Code Playgroud)

haskell

1
推荐指数
1
解决办法
824
查看次数

在Haskell中进行JSON解析期间的重载分辨率

我正在阅读"真实世界Haskell"(好书),我对编译器如何选择重载函数感到困惑.

如果我有一个类型类

type JSONError = String

class JSON a where
    toJValue :: a -> JValue
    fromJValue :: JValue -> Either JSONError a
Run Code Online (Sandbox Code Playgroud)

和这样的两个实例

instance JSON Bool where
    toJValue = JBool
    fromJValue (JBool b) = Right b
    fromJValue _ = Left "not a JSON boolean"
Run Code Online (Sandbox Code Playgroud)

instance JSON String where
    toJValue = JString
    fromJValue (JString s) = Right s
    fromJValue _ = Left "not a JSON string"
Run Code Online (Sandbox Code Playgroud)

编译器如何在两个"fromJValue"函数之间进行选择,例如,给出一个Integer?

parsing haskell overloading typeclass

0
推荐指数
1
解决办法
203
查看次数

Writer monad运算符定义问题

我想>>=为Write monad 定义运算符.我尝试了类似的东西,但它不起作用(不编译).我可以得到一些帮助吗?谢谢

newtype Writer a = Writer { runWriter :: (a, [String]) }

instance Monad Writer where
    return = Writer

    Writer m >>= f = Writer $ \r ->
        (runWriter (f m) r)
Run Code Online (Sandbox Code Playgroud)

我明白了:

Couldn't match type ‘a’ with ‘(a, [String])’
Run Code Online (Sandbox Code Playgroud)

我在返回行时遇到此错误,但也>>=没有很好地定义,我正在努力使其正确.

haskell

0
推荐指数
1
解决办法
62
查看次数