我正在编写这个函数 len 来计算 GHCi 中列表的长度。
len [] = 0
len [x] = 1
len (x:xs) = 1 + len xs
Run Code Online (Sandbox Code Playgroud)
我试图用[]作为参数调用该函数,但错误Exception: Non-exhaustive patterns in function len击中了我。我不是已经在函数定义中包含了空列表案例吗?
haskell ghci non-exhaustive-patterns read-eval-print-loop multiline-repl-definition
我有这个功能inserts,其中
inserts 1 [2,3] = [[1,2,3],[2,1,3],[2,3,1]]
Run Code Online (Sandbox Code Playgroud)
这是定义(直接来自Bird和Gibbons的Haskell算法设计)
inserts :: a -> [a] -> [[a]]
inserts x [] = [[x]]
inserts x (y:ys) = (x:y:ys) : map (y:) (inserts x ys)
Run Code Online (Sandbox Code Playgroud)
我已经用上面的例子在 ghci 中尝试过,但我得到以下异常
[[1,2,3],[2,1,3]*** Exception: <interactive>:2:1-53: Non-exhaustive patterns in function inserts
Run Code Online (Sandbox Code Playgroud)
有谁知道缺少的模式是什么?
haskell ghci non-exhaustive-patterns multiline-repl-definition
我在 Haskell 中有以下两种类型签名:
foo :: (a -> (a,b)) -> a -> [b]
bar :: (a -> b) -> (a -> b -> c) -> a -> c
Run Code Online (Sandbox Code Playgroud)
我想编写这两个函数的具体实现,但我真的很难理解从哪里开始。
我知道它foo需要一个函数(a -> (a,b))并返回a一个包含b.
并bar接受一个函数(b -> c),该函数返回一个(a -> b -> c)最终返回a和的函数c。
谁能告诉我一个具体实现的例子?
我怎么知道从哪里开始这样的事情以及定义的左侧是什么?
implementation haskell types functional-programming function
我有一个 Haskell 语言项目,但我对这项技术不是很熟悉。
我的程序必须采用 5 个命令行参数(包括二进制),它应该类似于以下行:
./my_program --a 30 --b 20
Run Code Online (Sandbox Code Playgroud)
如何检查参数的数量(如果返回错误arguments != 5)?
我知道这个getargs功能,但我不知道如何使用它。
我试图理解用 python 代码编写的方案过程:
def callcc(proc):
"Call proc with current continuation; escape only"
ball = RuntimeWarning("Sorry, can't continue this continuation any longer.")
def throw(retval): ball.retval = retval; raise ball
try:
return proc(throw)
except RuntimeWarning as w:
if w is ball: return ball.retval
else: raise w
Run Code Online (Sandbox Code Playgroud)
它来自本教程:http : //norvig.com/lispy2.html。
以上是如何工作的?是什么ball意思,为什么 a proc(edure?) 被称为 athrow作为其参数值?评论“仅转义”是什么意思?
顺便说一句,这是我目前(可能是被误导的)对适用于 python 的延续的理解,这类似于传递一个带产量的函数:
def c(func, *args, **kwargs):
# func must be a coroutine
return func(*args, **kwargs)
def inc(x=0):
while True:
yield …Run Code Online (Sandbox Code Playgroud) 看看这个定义出现在Data.Tree:
foldTree :: (a -> [b] -> b) -> Tree a -> b
foldTree f = go where
go (Node x ts) = f x (map go ts)
Run Code Online (Sandbox Code Playgroud)
我的具体问题是:当goname 出现在方程的右侧(map go ts)时,函数的类型如何
(a -> [b] -> b)
Run Code Online (Sandbox Code Playgroud)
被推断?
例如,有这行代码:
foldTree (:) (Node 1 [Node 2 []])
Run Code Online (Sandbox Code Playgroud)
实例化定义:
foldTree (:) = go where
go (Node 1 [Node 2 []]) = (:) 1 (map go [Node 2 []])
Run Code Online (Sandbox Code Playgroud)
(:) 1 (map go [Node 2 []])没有完全评估,所以我只看到 …
我想通过map向量列表上的 a应用 lambda 函数,并能够从结果中获取布尔值列表,然后比较布尔值列表中的所有元素
lambda = (\ list x -> distance (x (5,5)) < 10)
[(0,1),(1,6),(15,36)] ->
Run Code Online (Sandbox Code Playgroud)
在每个元素上应用 lambda,这将给出 :[True, True, False]
然后检查是否所有元素都是True
我试图这样做
checkConvergence :: [Vector] -> Vector -> Bool
checkConvergence list y = map (\ list x -> distance (x y) < 10) list
Run Code Online (Sandbox Code Playgroud)
但我得到了这个:
Run Code Online (Sandbox Code Playgroud)Couldn't match expected type ‘Bool’ with actual type [(Vector -> Vector) -> Bool]
我需要验证数字输入是否在某个范围内。为此,我正在使用
ensure :: (a -> Bool) -> a -> Maybe a
ensure p v | p v = Just v
| otherwise = Nothing
Run Code Online (Sandbox Code Playgroud)
检查某个值的上限和下限的一种方法x :: Int是通过一元链:
let validated = pure x >>= ensure (>0) >>= ensure (<100)
Run Code Online (Sandbox Code Playgroud)
据我了解,两次验证的顺序无关紧要;因此,应该可以以应用形式重写上述表达式。如何?我没能做到,但我希望一旦我做到了,我就能对应用程序有更深入的了解:-)。
我需要将一个函数应用于两个列表。map 函数是map :: (a->b) -> [a] -> [b],但是我需要更像map2 :: (a->b->c) -> [a] -> [b] -> [c]. 有没有类似的前奏功能map可以做到这一点?
我需要帮助从理论上理解我的代码。这是我的 lisp 程序:
(defun depth (lst)
(if (or (null lst) (atom lst)) 0
(+ 1 (apply 'max (mapcar #'depth lst)))
))
Run Code Online (Sandbox Code Playgroud)
我知道它适用于这个例子:
(write (depth '((a (b c) d r ((t))))) -> 3
Run Code Online (Sandbox Code Playgroud)
我只是无法理解我尝试过的语句的else语句IF。
如果您能帮助我,将不胜感激。先感谢您。
haskell ×8
map-function ×3
ghci ×2
lisp ×2
types ×2
applicative ×1
callcc ×1
common-lisp ×1
fold ×1
function ×1
io ×1
lambda ×1
list ×1
monads ×1
python ×1
recursion ×1
scheme ×1
tree ×1
validation ×1