小编sch*_*ine的帖子

如何在 Haskell 中正确使用 toLower?

我最近开始学习 Haskell 并想将某些内容转换为小写。我查找了函数“toLower”,但它似乎不起作用。

Prelude> import Data.Text
Prelude Data.Text> toLower "JhELlo"

<interactive>:2:9: error:
    * Couldn't match expected type `Text' with actual type `[Char]'
    * In the first argument of `toLower', namely `"JhELlo"'
      In the expression: toLower "JhELlo"
      In an equation for `it': it = toLower "JhELlo"
Prelude Data.Text> toLower 'JhELlo'

<interactive>:3:9: error:
    * Syntax error on 'JhELlo'
      Perhaps you intended to use TemplateHaskell or TemplateHaskellQuotes
    * In the Template Haskell quotation 'JhELlo'
Prelude Data.Text>
Run Code Online (Sandbox Code Playgroud)

haskell

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

为什么Complex的乘法实现需要一个RealFloat?

在GHCi中,人们发现以下内容:

 import Data.Complex
 :t 2 * (3 :+ 4)
2 * (3 :+ 4) :: RealFloat a => Complex a
 :t (* (3 :+ 4))
(* (3 :+ 4)) :: RealFloat a -> Complex a -> Complex a
Run Code Online (Sandbox Code Playgroud)

然而:

 :t fmap (* 2) (3 :+ 4)
fmap (* 2) (3 :+ 4) :: Num a => Complex a -> Complex a
Run Code Online (Sandbox Code Playgroud)

现在,为什么会这样呢?难道只是fromIntegerNum a => Complex a已经键入RealFloat a => a -> Complex a?如果是这样,为什么?

haskell complex-numbers

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

为什么Haskell不允许在理解中进行模式匹配?

我写了以下(琐碎)函数:

h c = [f x | x <- a, f <- b, (a, b) <- c]
Run Code Online (Sandbox Code Playgroud)

我本以为这是因为:

h c = do (a, b) <- c
         f <- b
         x <- a
         return (f x)
Run Code Online (Sandbox Code Playgroud)

反过来,desugared(忽略的fail东西)为:

h c = c >>= \(a, b) -> b >>= \f -> a >>= \x -> return (f x)
Run Code Online (Sandbox Code Playgroud)

但是,GHCi会返回错误:

<interactive>:24:17: error: Variable not in scope: a :: [a1]
<interactive>:20:27: error:
    Variable not in scope: b :: [t0 -> b1]
Run Code Online (Sandbox Code Playgroud)

这似乎是荒谬的,因为a …

monads haskell scope list-comprehension list

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

有没有办法告诉GHC将所有警告转储到文件中?

到目前为止,我已经尝试了以下方法:

me@pc:~/code$ ghc file.hs -Wall | tee warnings.log
me@pc:~/code$ ghc file.hs -Wall > warnings.log
Run Code Online (Sandbox Code Playgroud)

但是ghc只像正常情况那样打印警告,并且仅通过非警告步骤。

有没有办法做到这一点?

haskell ghc

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

我可以编译这个模棱两可的类型函数吗?

首先:我知道我将无法使用此功能,除非我已TypeApplications启用。但我认为AllowAmbiguousTypes是为了解决这个问题。

我目前有以下代码:

{-# LANGUAGE ExplicitForAll, AllowAmbiguousTypes #-}

module A where

class B c where
    d :: forall e. e -> c e

class F g where
    h :: forall i. g i -> i

data J k = J {l :: k}

instance B J where
    d = J

instance F J where
    h = l

m :: forall n o. (B n, F n) => o -> o
m = h . d
Run Code Online (Sandbox Code Playgroud)

用 GHCi …

haskell types ambiguous

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

为什么会产生StackOverflow错误?

我最近开始使用Haskell,并定义了这个看似简单的函数:

f 0 = 1
f x = x * f x - 1
Run Code Online (Sandbox Code Playgroud)

但是,结果如下:

GHCi, version 8.2.1: http://www.haskell.org/ghc/  :? for help
Prelude> f 0 = 1
Prelude> f x = x * f x - 1
Prelude> f 10
*** Exception: stack overflow
Prelude>
Run Code Online (Sandbox Code Playgroud)

haskell ghci

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

在GHC 8.6.5中是否已删除在do块中使用let语句的功能?

我在中输入了一些代码ghci,类似于以下代码:

main = do { a <- getLine ; let b = "Hello " ++ a ; putStrLn b }
Run Code Online (Sandbox Code Playgroud)

但是,我收到此错误:

<interactive>:1:63: error: parse error on input `}'
Run Code Online (Sandbox Code Playgroud)

在Haskell / GHC的早期版本中,我记得这种方法工作得很好-甚至明确指出,在do块中,您不需要in关键字。但是,使它起作用的唯一方法似乎是:

main = do { a <- getLine ; let b = "Hello " ++ a in putStrLn b }
Run Code Online (Sandbox Code Playgroud)

不会产生此错误。

这个被删除了吗?如果是这样,我是否需要dolet in表达式中添加第二个块?

syntax monads haskell let do-notation

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

在 Ubuntu 上使用 PowerShell 检查文件是否可执行

我不想使用 bash。

我已经在对象中查找了任何参数FileInfo,但没有任何东西可以告诉您该文件是否可执行。

我该怎么做呢?

linux filesystems powershell executable

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

为什么在这种情况下不映射(映射f)x工作?

我最近在Haskell中尝试过这个:

> :t getEqs
getEqs :: [Char] -> [Char] -> ([Bool], [Bool])
> :t mixpairs
mixpairs :: [[[Char]]]
> :t map
map :: (a -> b) -> [a] -> [b]
> map (map getEqs) mixpairs
Run Code Online (Sandbox Code Playgroud)

然而,由于看似没有理由,它返回了这个:

<interactive>:38:1: error:
    • No instance for (Show ([Char] -> ([Bool], [Bool])))
        arising from a use of ‘print’
        (maybe you haven't applied a function to enough arguments?)
    • In a stmt of an interactive GHCi command: print it
> 
Run Code Online (Sandbox Code Playgroud)

AFAICS应该发生的是一个获取列表列表并返回另一个东西的函数映射到另一个列表列表列表.
这似乎应该有效.

haskell

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

为什么 (,) 的 Functor 实例映射到第二个值?

在 Haskell 中, (,) 的 Functor 实例显然是

instance Functor (,) where
    fmap f (a,b) = (a,f b)
Run Code Online (Sandbox Code Playgroud)

这导致了一个不直观的事实:

> fmap (const 5) [1, 2]
[5,5]
> fmap (const 5) (1, 2)
(1,5)
Run Code Online (Sandbox Code Playgroud)

现在,在我看来,使用这个定义会更好:

instance Functor (,) where
    fmap f (a,b) = (f a,f b)
Run Code Online (Sandbox Code Playgroud)

它会像这样工作:

> fmap (const 5) (1, 2)
(5,5)
Run Code Online (Sandbox Code Playgroud)

为什么不是这样?

haskell tuples functor

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

如何将$表达式转换为“常规”表达式

我已经阅读了在Haskell中在一个元组中打印值,该解决方案有效。我对这个问题的意图是$通过将代码“翻译”为$自由代码来了解符号的含义。

showDetails :: (String, Int, Int) -> String
showDetails (name, uid, _) = "Your name is:" ++ name ++ " Your ID is: " ++ show uid

main = do 
    putStrLn . unlines . map showDetails $ [("A",100,1),("B",101,2)]
Run Code Online (Sandbox Code Playgroud)

如何$告诉showDetails应用于列表元素(元组)?$那条线的免费版本是什么?

haskell

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

如何将索引添加到列表中?

例如,我有一个list = ['a', 'b', 'c'].
我想要的是一个列表indexed(list) == [(1, 'a'), (2, 'b'), (3, 'c)]

是否有内置或模块?

python list indices

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