<div>Hi,</div>
<div>    Can someone explain how this lambda is suppose to work, I cannot figure out what it does?</div>
<div> </div>
<div> </div>
<div>data Lambda = Val Double<br>               | Add Lambda Lambda<br>               | Subtract Lambda Lambda<br>               | Multiply Lambda Lambda<br>               | Divide Lambda Lambda<br>        | Var String<br>
        | Let String Lambda Lambda<br>        deriving Show</div>
<div>demo1 = (Add(Multiply(Divide(Subtract(Val 25)(Val 5))(Val 10))(Val 7))(Val 30))<br>--let x = 1+1 in 3 - x<br>test1 = Let &quot;x&quot; (Add (Val 1) (Val 1)) (Add(Var &quot;x&quot;)(Var &quot;x&quot;))</div>
<div>test6 = Let &quot;y&quot; (Add (Val 7)(Val 6)) (Subtract (Val 6)(Var &quot;y&quot;))</div>
<div>-- 4 * (let x = 1+1 in 3 + x)<br>test2 = Multiply (Val 4) test1</div>
<div>-- x * (let x = 1+1 in 3 + x)<br>test3 = Let &quot;y&quot; (Add (Val 4)(Val 7))(Multiply  test1 (Var &quot;y&quot;))</div>
<div>test5 = Add (test1) test6<br>type Dict =[(String,Lambda)]-- dictionary which takes a string and an expression<br>emptyDict :: Dict<br>emptyDict = []-- empty dictionary null set</div>
<div>addEntry :: String-&gt;Lambda -&gt;Dict -&gt; Dict<br>addEntry= \n e d -&gt; (n,e): d --  add an entry we take a string,expression and a dictionary :gives back dictionary</div>
<div><br>lookupEntry :: String -&gt; Dict -&gt; Maybe Lambda<br>lookupEntry n [] = Nothing<br>lookupEntry n (x:xs) = if (n == k)-- if the entry is equal to <br>   then (Just v)<br>                else lookupEntry n xs -- find the value of n in the rest of the list<br>
   where (k,v) = x   -- <br> <br>       </div>
<div>evalStep :: Dict -&gt; Lambda -&gt;  Lambda<br>evalStep d (Val x) =  (Val x)-- we have a dictionary and a value return the value</div>
<div><br>evalStep d(Add x y)-- dictionary and a add expression of x y<br>  = case x of<br>      (Val a) -&gt; case y of<br>                   (Val b) -&gt;  Val (a+b)<br>                   left -&gt; Add x (evalStep d y)<br>
      right -&gt; Add (evalStep d x)y</div>
<div>evalStep d(Subtract x y)<br>  = case x of<br>      (Val a) -&gt; case y of<br>                   (Val b) -&gt; Val (a-b)<br>                   left -&gt; Subtract x (evalStep d y)<br>      right -&gt; Subtract (evalStep d x)y</div>

<div>evalStep d(Multiply x y)<br>  = case x of<br>      (Val a) -&gt; case y of<br>                   (Val b) -&gt; Val (a*b)<br>                   left -&gt; Multiply x (evalStep d y)<br>      right -&gt; Multiply (evalStep d x)y</div>

<div>evalStep d (Divide x y)<br>  = case x of<br>      (Val a) -&gt; case y of<br>                   (Val b) -&gt; Val (a/b)<br>                   left -&gt; Divide x (evalStep d y)<br>      right -&gt; Divide (evalStep d x)y</div>

<div>evalStep d (Let n e1 e2) <br>   = case e1 of<br> (Val a) -&gt; case e2 of <br>  (Val b)-&gt; Val b<br>  left -&gt; evalStep (addEntry n e1 d)e2<br> right -&gt;  evalStep (addEntry  n e1 d) e2<br>      <br>evalStep d (Var x)<br>
   = case lookup x d of<br>       Just e -&gt; e<br>       Nothing -&gt; error &quot;Error in expression -- no definition for variable!&quot;</div>
<div>   <br>evaluate :: Dict-&gt; [Lambda] -&gt; Lambda -&gt; IO()<br>evaluate d(x:xs) e = do</div>
<div>     putStrLn (show e)<br>     putStrLn &quot;Do another step (y/n) or rollback (r)? :&quot;<br>     c &lt;- getLine<br>     case c of<br>       &quot;y&quot; -&gt; let e&#39;= (evalStep d e)in evaluate d (e:x:xs) e&#39;-- build up history<br>
                 <br>       &quot;r&quot; -&gt;  case (x:xs) of<br>  (x:xs)-&gt; evaluate d xs x<br>  []-&gt; do { putStrLn &quot;Empty&quot;<br>   ;evaluate d(x:xs) e<br>  } <br>       &quot;n&quot;  -&gt;  putStrLn $ &quot;Ok you said no :&quot; ++ c</div>

<div><br>ev :: Dict-&gt; [Lambda] -&gt; Lambda -&gt; IO() <br>ev = \d (x:xs) e -&gt;do<br>     putStrLn (show e)<br>     putStrLn &quot;Do another step (y/n) or rollback (r)? :&quot;<br>     c &lt;- getLine<br>     case c of<br>
       &quot;y&quot; -&gt; let e&#39;= (evalStep d e)in evaluate d (e:x:xs) e&#39;-- build up history<br>                 <br>       &quot;r&quot; -&gt;  case (x:xs) of<br>  (x:xs)-&gt; evaluate d xs x<br>  []-&gt; do { putStrLn &quot;Empty&quot;<br>
   ;evaluate d(x:xs) e<br>  } <br>       &quot;n&quot;  -&gt;  putStrLn $ &quot;Ok you said no :&quot; ++ c</div>
<div> </div>
<div><br>isFree :: String -&gt; Lambda -&gt; Bool<br>isFree n1 (Let n2 e1 e2)<br> |n1 == n2 = False<br> |otherwise = True</div>
<div> </div>
<div>subst :: String -&gt; Lambda -&gt; Lambda-&gt;Lambda<br>subst n e1 e2<br> |isFree n e2 = subst&#39; n e1 e2<br> |otherwise = e2</div>
<div>subst&#39; :: String -&gt; Lambda -&gt; Lambda-&gt;Lambda<br>subst&#39; n0 e1 (Var n1) <br> |n0 == n1 = e1<br> |otherwise = Var n1</div>
<div>subst&#39; s v (Add e1 e2) = Add(subst&#39; s v e1)(subst&#39; s v e2)</div>
<div>subst&#39; s v (Multiply e1 e2) = Multiply(subst&#39; s v e1)(subst&#39; s v e2)</div>
<div>subst&#39; s v (Divide e1 e2) = Divide(subst&#39; s v e1)(subst&#39; s v e2)</div>
<div>subst&#39; s v (Subtract e1 e2) = Subtract(subst&#39; s v e1)(subst&#39; s v e2)</div>
<div><br>subst&#39; s v (Let n e1 e2) = Let n (subst&#39; s v e1)(subst&#39; s v e2)<br></div>
<div>John</div>