我怎样才能做到以下几点。例如,如果我想减去Just 8-Just 5得到Just 3,我该怎么做?
Just 8 - Just 5 = Just 3
Just 15 - Just 9 = Just 6
Run Code Online (Sandbox Code Playgroud) 如果我想要类似的东西,我应该使用什么
[a->b] -> a -> [b]
Run Code Online (Sandbox Code Playgroud)
基本上我有一个函数列表,所有函数都接受一个值a并返回b。我想将所有这些应用到一个a并得到结果[b]。
我应该使用哪一种?
谢谢
我有以下代码
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) 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) 我正在尝试解决 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) 上下文:
Run Code Online (Sandbox Code Playgroud)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]这种记忆化的实现技术在许多编程语言中被广泛使用,但它不能直接应用于 Haskell,因为 Haskell 是纯的,我们不想为了记忆一个函数而引入杂质。幸运的是,由于 Haskell 的惰性求值特性,可以在没有副作用的情况下记住一个函数。
以下
memoize函数采用类型函数Int -> a并返回同一函数的记忆版本。诀窍是将函数转换为值,因为在 Haskell 中,函数不会被记忆,但值会被记忆。
问题:
haskell functional-programming memoization lazy-evaluation pure-function
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) 我试图将一个 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) 在 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) 是否可以if在列表推导式中使用某种语句来确定列表中每个元素的多个结果之一?
假设我对包含 a0或 a元素的列表有一个列表理解1。
如果值为 a 0,则字符串"off"需要存储在另一个列表中的相同位置。如果该值1则"on"。
这只是一个例子。使用递归看起来很容易完成,但是如果需要在列表列表上完成怎么办?还是列表列表?
haskell ×9
monads ×3
list ×2
recursion ×2
apply ×1
do-notation ×1
flatten ×1
function ×1
if-statement ×1
infinite ×1
io ×1
lisp ×1
maybe ×1
memoization ×1
monoids ×1
scheme ×1
traversable ×1
types ×1