我该如何处理Haskell困难? [英] How do I deal with this Haskell difficulty?
问题描述
我是Haskell的新手,我自己学习,正在运行程序,但对于循环不甚了解.在我的程序中,我做了While循环,我将展示两者的工作原理,但是我需要解释一个解释for loop的句子的用法.(let x = ref 0 in(for(i = 1至10)(x:= deref x + i))); deref x)
.我使用了一些变量如果您愿意,我可以向大家展示.我做了这样的While循环,并且有效
I'm a new in Haskell and i studied by myself, I'm running program but I don't understand for loop to much. In my program I did While loop,and I'm going to show my work for both but I need explain someone explain for loop how to do this sentence which i made
(let x = ref 0 in ( for (i=1 to 10 )(x:=deref x+i)));deref x )
. There is some variable I use
i can show it to you guys if you want.
I did While loop like this and it works
interp env (While b1 b2) = do
c1 <- interp env b1
case c1 of
(VBool True) -> do
interp env b2
interp env (While b1 b2)
(VBool False) -> return VNil
但是,我的循环工作是
interp env ( For s a1 a2 a3)= do
v1 <- interp env a1
v2 <- interp env a2
if (v1<v2 ) then do
x <- interp env a1
let env' = extendEnv x e1 env
interp env' a3
else return VNil
但无法正常工作,请您能解释出什么问题吗?
but is not working, Please can you explain what's wrong?
推荐答案
我在此答案中做出了以下假设,因为没有足够的信息来正确回答:
I am making the following assumptions in this answer, as not enough information is given to answer properly:
- 您有一个
VInt
数据构造函数,就像您的问题中的VBool
一样.我假设它拥有Int
类型的单个值. -
extendEnv
具有三个参数.第一个是变量ID(未知类型),第二个是将其绑定到的表达式(如果该语言不是惰性的,则为未知类型的值),第三个是您正在扩展的环境(令人惊讶的惊喜),未知类型). - 输入的AST已经过类型检查,因此我不处理类型错误.
- You have a
VInt
data constructor much likeVBool
in your question. I assume it holds a single value of typeInt
. extendEnv
takes three parameters. This first is a variable ID (of unknown type), the second is the expression to bind it to (or value if this language isn't lazy, of unknown type), the third is the environment you are extending (of, surprise surprise, unknown type).- The input AST has been type checked, so I'm not handling type errors.
代码:
interp env (For var start end body) = do
(VInt s) <- interp env start
(VInt e) <- interp env end
if s < e
then
let nenv = extendEnv var start env in do
interp nenv body
interp nenv (For var (VInt (s - 1)) end body) -- Maybe only env here.
else return VNil
这篇关于我该如何处理Haskell困难?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!