我最近开始学习 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) 在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)
现在,为什么会这样呢?难道只是fromInteger在Num a => Complex a已经键入RealFloat a => a -> Complex a?如果是这样,为什么?
我写了以下(琐碎)函数:
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 …
到目前为止,我已经尝试了以下方法:
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只像正常情况那样打印警告,并且仅通过非警告步骤。
有没有办法做到这一点?
首先:我知道我将无法使用此功能,除非我已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,并定义了这个看似简单的函数:
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) 我在中输入了一些代码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)
不会产生此错误。
这个被删除了吗?如果是这样,我是否需要do在let in表达式中添加第二个块?
我不想使用 bash。
我已经在对象中查找了任何参数FileInfo,但没有任何东西可以告诉您该文件是否可执行。
我该怎么做呢?
我最近在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 中, (,) 的 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中在一个元组中打印值,该解决方案有效。我对这个问题的意图是$通过将代码“翻译”为$自由代码来了解符号的含义。
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应用于列表元素(元组)?$那条线的免费版本是什么?
例如,我有一个list = ['a', 'b', 'c'].
我想要的是一个列表indexed(list) == [(1, 'a'), (2, 'b'), (3, 'c)]。
是否有内置或模块?
haskell ×10
list ×2
monads ×2
ambiguous ×1
do-notation ×1
executable ×1
filesystems ×1
functor ×1
ghc ×1
ghci ×1
indices ×1
let ×1
linux ×1
powershell ×1
python ×1
scope ×1
syntax ×1
tuples ×1
types ×1