我想在Common Lisp中做类似的事情:
(defparameter *fun*
(lambda () x))
(let ((x 0))
(funcall *fun*)) ;should return 0
Run Code Online (Sandbox Code Playgroud)
我想在定义函数时访问未定义的函数中的本地绑定.
如果我使用x作为参数,它会工作,但我无法更改变量的值:
(defparameter *fun*
(lambda (x) (setf x (+ x 1))))
(let ((x 0))
(funcall *fun* x)) ;should return 1, would return 0
Run Code Online (Sandbox Code Playgroud)
我怎么能做我想做的事?
我想手动派生类型:
f1 x xs = (filter . (<)) x xs
我们第一次看到x,所以:
x :: t1
Run Code Online (Sandbox Code Playgroud)
然后(<)有这种类型:
(<) :: Ord a1 => a1 -> a1 -> Bool
Run Code Online (Sandbox Code Playgroud)
我们只能说(< x)是否可以统一以下类型:
t1 ~ a1
Run Code Online (Sandbox Code Playgroud)
然后
x :: a1
Run Code Online (Sandbox Code Playgroud)
所以
(<x) :: Ord a1 => a1 -> Bool
Run Code Online (Sandbox Code Playgroud)
过滤器有这种类型
filter :: (a2 -> Bool) -> [a2] -> [a2]
Run Code Online (Sandbox Code Playgroud)
第一次看到xs,所以:
xs :: t2
Run Code Online (Sandbox Code Playgroud)
我们只能说(filter . (<)) x xs是否可以统一以下类型:
a1 -> Bool ~ a2 -> Bool
t2 ~ [a2] …Run Code Online (Sandbox Code Playgroud) 所以当标题解释它时,我能够将每个偶数和奇数转换为它们对应的值,但是对于数据输入我有一个逗号.这创造了额外的L[].让我告诉你我的预期结果.
预期结果> binariza (L [E 1,L [E 2,E 3],E 4]) => L [E 0,L [E 1,E 0],E 1]
我的结果> binariza (L [E 1,L [E 2,E 3],E 4]) => L [E 0,L [L [E 1,L [E 0,L []]],L [E 1,L []]]]
这是我的代码,有人可以解释我如何避免在L[]每次发现逗号时封装所有内容.
data LA e = L [LA e] | E e deriving Show
binariza :: LA Integer -> LA Integer
binariza (L[]) = L[]
binariza (E num) =
if (even num) then
E 1
else
E …Run Code Online (Sandbox Code Playgroud) 我必须从Haskell中的给定函数中找到最通用的类型,或者更确切地说找到两个函数的"产品"的最一般类型(如果存在).我不确定但也许我应该使用Robinson统一算法,但我无法理解它.我需要一步一步的详细解决方案,所以我能理解.
功能 :
map :: (a ? b) ? [a] ? [b]
iterate :: (a ? a) ? a ? [a]
Run Code Online (Sandbox Code Playgroud)
如何找到最常用的类型
map iterate iterate map这不是作业.
任何人都可以解释下面的代码,请一步一步做.我知道它是定义一个函数和它的输入,但到底是什么(+)做的,为什么有三种不同的IntS IN plus和4个 Int S IN plusplus的类型,而它好像有只有3个参数中plusplus的代码?
plus :: Int -> Int -> Int
plus = (+)
plusPlus :: Int -> Int -> Int -> Int
plusPlus a b c = a + b + c
Run Code Online (Sandbox Code Playgroud) 我需要一些帮助来在 common lisp 中重写这个函数而不使用 usin SET,SETF或者SETQ(我也不能使用循环),我希望有人可以帮助我。这是代码:
(defun apply-values (DictValues Monomial)
(let ( (Coeff (monomial-coefficient Monomial))
(Vars (varpowers Monomial))
(Acc 1) )
(mapcar (lambda(x)
(setf Acc
(* Acc (expt (cdr (assoc (varpower-symbol x)
DictValues))
(varpower-power x)))))
Vars)
(* Coeff Acc)))
Run Code Online (Sandbox Code Playgroud)
我的问题是在以 开头的行mapcar。
在此先感谢您的帮助!
我想使用 monad 在 haskell 中编写一个循环,但我很难理解这个概念。
有人可以为我提供一个简单的 while 循环示例,同时满足某些涉及 IO 操作的条件吗?我不想要一个抽象的例子,而是一个真正有效的具体例子。
是否something <- stuff总是在Haskell中评估类似的语句,即使something在其余代码中没有调用它?(被something <- stuff称为"行动"? - 我不知道技术措辞).
如果这是真的,我还有另外一个问题.
我有一些代码从这样开始:
computeContour3d voxel voxmax level = do
voxelmax <- somefunction voxel
let max' = fromMaybe voxelmax voxmax
Run Code Online (Sandbox Code Playgroud)
也就是说,如果参数voxmax不是Nothing,则voxelmax没有必要,因为max' = fromJust voxmax在这种情况下.因此,如果我的第一个问题的回答是"是",我怎么能避免评估voxelmax何时没有必要?
monads evaluation haskell lazy-evaluation operator-precedence
我试图只是比较两个用户输入,但我似乎无法让它工作,并不断得到解析错误.任何帮助将不胜感激.
main = do
foo <- putStrLn "Enter two numbers."
numone <- getLine
numtwo <- getLine
putStrLn $ ("You entered " ++ numone ++ " and " ++ numtwo)
if
numone == numtwo
then
putStrLn "They are the same"
else
putStrLn "They are not the same"
Run Code Online (Sandbox Code Playgroud) 我想将resolve_on应用于列表c1中的每个元素,比如
for(Char c:c1){
resolve_on c c1 c2;}
Run Code Online (Sandbox Code Playgroud)
那么我怎样才能使用map函数呢?
resolvents :: [Char] -> [Char] -> [[Char]]
resolvents c1 c2 = map (//what should I do) c1
resolve_on :: Char -> [Char] -> [Char] -> [Char]
resolve_on c c1 c2
Run Code Online (Sandbox Code Playgroud) haskell ×8
types ×3
common-lisp ×2
lisp ×2
loops ×2
monads ×2
unification ×2
arguments ×1
clisp ×1
evaluation ×1
ghci ×1
io-monad ×1
lambda ×1
list ×1
map-function ×1
nested-lists ×1
recursion ×1
syntax ×1
tree ×1
while-loop ×1