小编Wil*_*ess的帖子

如何在 Haskell 中减去 Maybe 值?例如,如果我想减去 Just 8 - Just 5 得到 Just 3,我该怎么做?

我怎样才能做到以下几点。例如,如果我想减去Just 8-Just 5得到Just 3,我该怎么做?

Just 8 - Just 5 = Just 3

Just 15 - Just 9 = Just 6
Run Code Online (Sandbox Code Playgroud)

monads haskell maybe

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

如何将可遍历的函数应用于一个值

如果我想要类似的东西,我应该使用什么

[a->b] -> a -> [b]
Run Code Online (Sandbox Code Playgroud)

基本上我有一个函数列表,所有函数都接受一个值a并返回b。我想将所有这些应用到一个a并得到结果[b]

我应该使用哪一种?

谢谢

haskell function list apply traversable

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

将带有 >>= 的表达式转换为 do 表示法

我有以下代码

newtype State s a = State { runState :: s -> (s,a) }    
evalState :: State s a -> s -> a
evalState sa s = snd $ runState sa s

instance Functor (State s) where
  fmap f sa = State $ \s ->
    let (s',a) = runState sa s in
      (s',f a)

instance Applicative (State s) where
  pure a = State $ \s -> (s,a)

  sf <*> sa = State $ \s ->
    let (s',f) = runState sf …
Run Code Online (Sandbox Code Playgroud)

monads haskell code-translation do-notation

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

My redefinition of Haskell 'length' function won't work

Can someone please explain how I can fix my program. Very new to Haskell, been trying to create a length function that calculates the length of a list of any type.

I am aiming to do this using data as I want to create a brand new type to do so (this is the area of Haskell that I'm currently learning, which is why it might not be the most efficient implementation of this function)

data List a = Nil …
Run Code Online (Sandbox Code Playgroud)

recursion haskell list type-mismatch algebraic-data-types

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

努力理解“无法构造无限类型”错误

我正在尝试解决 HackerRank 问题,但遇到了一个我无法弄清楚的错误。问题出在solve1函数上。确切的错误是:cannot construct the infinite type a ~ t0 a. Expected type ([t0 a], [t0 a], [t0 a]) Actual type ([a], [a], [a]). In the second argument of `tripleMap`, namely '(tripList xs)'

我一直看着这些类型,它们在我眼中继续显得正确。tripList获取数字列表并返回数字列表的三元组。tripleMap将一组数字列表作为其第二个参数。

tripList我的 REPL 中进行测试时,我得到了想要的结果:

> tripList [1,0,-1,0,1] 
([1,1],[0,0],[-1])
Run Code Online (Sandbox Code Playgroud)

这是我的代码:

length' :: (Foldable t, Num b, Fractional b, Ord b) => t a -> b
length' = foldr (\_ acc -> 1 + acc) 0

tripList :: (Num …
Run Code Online (Sandbox Code Playgroud)

haskell

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

Haskell 中的记忆

上下文

def fib(n):
    if n < 2: return 1
    return fib(n-1) + fib(n-2)
Run Code Online (Sandbox Code Playgroud)

可以通过记忆加速:

fib_memo = {}
def fib(n):
    if n < 2: return 1
    if not fib_memo.has_key(n):
        fib_memo[n] = fib(n-1) + fib(n-2)
    return fib_memo[n]
Run Code Online (Sandbox Code Playgroud)

这种记忆化的实现技术在许多编程语言中被广泛使用,但它不能直接应用于 Haskell,因为 Haskell 是纯的,我们不想为了记忆一个函数而引入杂质。幸运的是,由于 Haskell 的惰性求值特性,可以在没有副作用的情况下记住一个函数。

以下memoize函数采用类型函数Int -> a 并返回同一函数的记忆版本。诀窍是将函数转换为值,因为在 Haskell 中,函数不会被记忆,但会被记忆。

问题:

  1. 函数式编程中的函数和值不是一样的吗?
  2. 缓存如何不被视为副作用(在纯函数的上下文中)

haskell functional-programming memoization lazy-evaluation pure-function

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

为什么是无限类型?(发生检查:不能构造无限类型:a~树a)

data Tree a = Branch a a | Leaf deriving (Show)

construct :: (Integral a) => [a] -> Tree a
construct [] = Leaf
construct (x:[]) = Leaf                -- _____error________
construct (l:r:xs) = if l>r then Branch l (construct $ r:xs) 
                            else Branch (construct $ l:xs) r
Run Code Online (Sandbox Code Playgroud)
* Occurs check: cannot construct the infinite type: a ~ Tree a
* In the second argument of `Branch', namely `(construct $ r : xs)'
  In the expression: Branch l (construct $ r …
Run Code Online (Sandbox Code Playgroud)

haskell types infinite

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

方案:是否可以将 S 表达式列表转换为原子列表?

我试图将一个 S 表达式列表转换为一个简单的原子列表,类似于The Little Schemer一书中问题。

我的代码是(在 Dr.Racket 中输入):

> (define lat '((coffee) cup ((tea) cup) (and (hick)) cup))
> (define f
    (lambda (lat)
      (cond
        ((null? lat) (quote ()))
        ((atom? (car lat)) (cons (car lat) (f (cdr lat))))
        (else (cons (f (car lat)) (f (cdr lat)))))))
> (f lat)
'((coffee) cup ((tea) cup) (and (hick)) cup)
Run Code Online (Sandbox Code Playgroud)

上面的代码返回与输入列表相同的列表。我尽力了,但得到了不同的答案,例如:

(coffee)
(cup . cup)
( () (()) (()) )
Run Code Online (Sandbox Code Playgroud)

用于程序中的各种修改。

我想知道,我们能不能得到答案:

'(coffee cup tea cup and hick cup)
Run Code Online (Sandbox Code Playgroud)

给予

'((coffee) …
Run Code Online (Sandbox Code Playgroud)

lisp recursion scheme flatten the-little-schemer

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

haskell,IO Monoid 关联性被破坏了吗?

在 haskell IO 类型中有 Monoid 的实例:

instance Monoid a => Monoid (IO a) where
    mempty = pure empty
Run Code Online (Sandbox Code Playgroud)

如果我有三个共享某个状态的操作,并通过副作用改变彼此的行为,从 IO 类型的角度来看,这可能会导致违反关联律:

a1:: IO String
a2:: IO String
a3:: IO String
Run Code Online (Sandbox Code Playgroud)

(a1 mappenda2) mappenda3 /= a1 mappend(a2 mappenda3)

例如,如果 a1,a2,a3 请求字符串中的当前时间,或者 IO 包含一些计算请求编号的 DB。这意味着它可以是:

(a1 `mappend` a2) `mappend` a3  == "1"++"2"++"3"
a1 `mappend` (a2 `mappend` a3) == "3"++"1"++"2"
Run Code Online (Sandbox Code Playgroud)

编辑:

我想我不应该用 db 举一个例子,它很困惑,更喜欢的例子:

a1 = show <$> getUnixTime 
a2 = show <$> getUnixTime
a3 = show <$> getUnixTime …
Run Code Online (Sandbox Code Playgroud)

io monads haskell functional-programming monoids

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

可以在 Haskell 列表推导式中使用 if 结构吗?

是否可以if在列表推导式中使用某种语句来确定列表中每个元素的多个结果之一?

假设我对包含 a0或 a元素的列表有一个列表理解1

如果值为 a 0,则字符串"off"需要存储在另一个列表中的相同位置。如果该值1"on"

这只是一个例子。使用递归看起来很容易完成,但是如果需要在列表列表上完成怎么办?还是列表列表?

haskell if-statement list-comprehension

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