Send Beginners mailing list submissions to
        [email protected]

To subscribe or unsubscribe via the World Wide Web, visit
        http://www.haskell.org/mailman/listinfo/beginners
or, via email, send a message with subject or body 'help' to
        [email protected]

You can reach the person managing the list at
        [email protected]

When replying, please edit your Subject line so it is more specific
than "Re: Contents of Beginners digest..."


Today's Topics:

   1.  lambda (John Moore)
   2. Re:  lambda (Stephen Tetley)
   3.  Tying the knot (Alex Rozenshteyn)
   4.  strange behaviour : computing lowest divisor (Abhijit Ray)
   5. Re:  strange behaviour : computing lowest divisor
      (Lyndon Maydwell)


----------------------------------------------------------------------

Message: 1
Date: Tue, 28 Dec 2010 16:15:58 +0000
From: John Moore <[email protected]>
Subject: [Haskell-beginners] lambda
To: [email protected]
Message-ID:
        <[email protected]>
Content-Type: text/plain; charset="iso-8859-1"

Hi,
    Can someone explain how this lambda is suppose to work, I cannot figure
out what it does?


data Lambda = Val Double
               | Add Lambda Lambda
               | Subtract Lambda Lambda
               | Multiply Lambda Lambda
               | Divide Lambda Lambda
        | Var String
        | Let String Lambda Lambda
        deriving Show
demo1 = (Add(Multiply(Divide(Subtract(Val 25)(Val 5))(Val 10))(Val 7))(Val
30))
--let x = 1+1 in 3 - x
test1 = Let "x" (Add (Val 1) (Val 1)) (Add(Var "x")(Var "x"))
test6 = Let "y" (Add (Val 7)(Val 6)) (Subtract (Val 6)(Var "y"))
-- 4 * (let x = 1+1 in 3 + x)
test2 = Multiply (Val 4) test1
-- x * (let x = 1+1 in 3 + x)
test3 = Let "y" (Add (Val 4)(Val 7))(Multiply  test1 (Var "y"))
test5 = Add (test1) test6
type Dict =[(String,Lambda)]-- dictionary which takes a string and an
expression
emptyDict :: Dict
emptyDict = []-- empty dictionary null set
addEntry :: String->Lambda ->Dict -> Dict
addEntry= \n e d -> (n,e): d --  add an entry we take a string,expression
and a dictionary :gives back dictionary

lookupEntry :: String -> Dict -> Maybe Lambda
lookupEntry n [] = Nothing
lookupEntry n (x:xs) = if (n == k)-- if the entry is equal to
   then (Just v)
                else lookupEntry n xs -- find the value of n in the rest of
the list
   where (k,v) = x   --


evalStep :: Dict -> Lambda ->  Lambda
evalStep d (Val x) =  (Val x)-- we have a dictionary and a value return the
value

evalStep d(Add x y)-- dictionary and a add expression of x y
  = case x of
      (Val a) -> case y of
                   (Val b) ->  Val (a+b)
                   left -> Add x (evalStep d y)
      right -> Add (evalStep d x)y
evalStep d(Subtract x y)
  = case x of
      (Val a) -> case y of
                   (Val b) -> Val (a-b)
                   left -> Subtract x (evalStep d y)
      right -> Subtract (evalStep d x)y
evalStep d(Multiply x y)
  = case x of
      (Val a) -> case y of
                   (Val b) -> Val (a*b)
                   left -> Multiply x (evalStep d y)
      right -> Multiply (evalStep d x)y
evalStep d (Divide x y)
  = case x of
      (Val a) -> case y of
                   (Val b) -> Val (a/b)
                   left -> Divide x (evalStep d y)
      right -> Divide (evalStep d x)y
evalStep d (Let n e1 e2)
   = case e1 of
 (Val a) -> case e2 of
  (Val b)-> Val b
  left -> evalStep (addEntry n e1 d)e2
 right ->  evalStep (addEntry  n e1 d) e2

evalStep d (Var x)
   = case lookup x d of
       Just e -> e
       Nothing -> error "Error in expression -- no definition for variable!"

evaluate :: Dict-> [Lambda] -> Lambda -> IO()
evaluate d(x:xs) e = do
     putStrLn (show e)
     putStrLn "Do another step (y/n) or rollback (r)? :"
     c <- getLine
     case c of
       "y" -> let e'= (evalStep d e)in evaluate d (e:x:xs) e'-- build up
history

       "r" ->  case (x:xs) of
  (x:xs)-> evaluate d xs x
  []-> do { putStrLn "Empty"
   ;evaluate d(x:xs) e
  }
       "n"  ->  putStrLn $ "Ok you said no :" ++ c

ev :: Dict-> [Lambda] -> Lambda -> IO()
ev = \d (x:xs) e ->do
     putStrLn (show e)
     putStrLn "Do another step (y/n) or rollback (r)? :"
     c <- getLine
     case c of
       "y" -> let e'= (evalStep d e)in evaluate d (e:x:xs) e'-- build up
history

       "r" ->  case (x:xs) of
  (x:xs)-> evaluate d xs x
  []-> do { putStrLn "Empty"
   ;evaluate d(x:xs) e
  }
       "n"  ->  putStrLn $ "Ok you said no :" ++ c


isFree :: String -> Lambda -> Bool
isFree n1 (Let n2 e1 e2)
 |n1 == n2 = False
 |otherwise = True

subst :: String -> Lambda -> Lambda->Lambda
subst n e1 e2
 |isFree n e2 = subst' n e1 e2
 |otherwise = e2
subst' :: String -> Lambda -> Lambda->Lambda
subst' n0 e1 (Var n1)
 |n0 == n1 = e1
 |otherwise = Var n1
subst' s v (Add e1 e2) = Add(subst' s v e1)(subst' s v e2)
subst' s v (Multiply e1 e2) = Multiply(subst' s v e1)(subst' s v e2)
subst' s v (Divide e1 e2) = Divide(subst' s v e1)(subst' s v e2)
subst' s v (Subtract e1 e2) = Subtract(subst' s v e1)(subst' s v e2)

subst' s v (Let n e1 e2) = Let n (subst' s v e1)(subst' s v e2)
John
-------------- next part --------------
An HTML attachment was scrubbed...
URL: 
<http://www.haskell.org/pipermail/beginners/attachments/20101228/4e64eb18/attachment-0001.htm>

------------------------------

Message: 2
Date: Tue, 28 Dec 2010 18:22:59 +0000
From: Stephen Tetley <[email protected]>
Subject: Re: [Haskell-beginners] lambda
Cc: [email protected]
Message-ID:
        <[email protected]>
Content-Type: text/plain; charset=ISO-8859-1

Hi John

I'm not sure it can work properly in its current form.

If this is the code that's been posted to the list before, it was
originally a calculator with a stepper reduced a calculation a term at
at time:

input> (+ 3 (- 5 1))

ans> (+ 3 4)

"Do another step (y/n) or rollback (r)? :"

input> y

ans> 7

However once "let" was added, I'm not convinced the evalStep is right
as I think it works too greedily and stops evaluating a term at a
time, and instead evaluates all sub-terms.

Best wishes

Stephen



------------------------------

Message: 3
Date: Wed, 29 Dec 2010 01:52:18 -0500
From: Alex Rozenshteyn <[email protected]>
Subject: [Haskell-beginners] Tying the knot
To: beginners <[email protected]>
Message-ID:
        <[email protected]>
Content-Type: text/plain; charset="utf-8"

I'm trying to understand the technique referred to as "tying the knot", but
documentation on the internet seems to be much sparser and more obtuse than
I like.

So I'm asking here.

As far as I understand, "tying the knot" refers to a way of using laziness
to implement something like references in a purely functional way.

I'm trying to write a toy simulation:
I have a population :: [Person]
I want to collect a random subset of possible pairs of distinct people.
So I go to each person in the population and select a subset of the people
after him/her in the list; these are pairs in which s/he is the first
element.

I want to then be able to ask for all pairs in which a person is the first
or the second element.  I could give each person a unique id, but it seems
like tying the knot is a valid way to implement this.

Please correct me if I am misunderstanding.

Thank you.

-- 
          Alex R
-------------- next part --------------
An HTML attachment was scrubbed...
URL: 
<http://www.haskell.org/pipermail/beginners/attachments/20101229/2951ef2a/attachment-0001.htm>

------------------------------

Message: 4
Date: Wed, 29 Dec 2010 17:32:50 +0800
From: Abhijit Ray <[email protected]>
Subject: [Haskell-beginners] strange behaviour : computing lowest
        divisor
To: beginners <[email protected]>
Message-ID:
        <[email protected]>
Content-Type: text/plain; charset="iso-8859-1"

HI,
I have written(copied) a small piece of code which finds the lowest divisor
of am integer (greater than 1)

the code is here


ld::Int->Int
ld n = ld' 2 n
       where
  ld' i n | rem n i == 0 = i
                 | i^2 > n = n
                 | otherwise = ld' (i+1) n


now it seems to work fine for small numbers but for a big number we get

*Main> ld 278970415063349480483707695
7

the number is obviously divisible by 5 , so where is the anomaly ?

Thanks,
Abhijit
On Wed, Dec 29, 2010 at 2:52 PM, Alex Rozenshteyn <[email protected]>wrote:

> I'm trying to understand the technique referred to as "tying the knot", but
> documentation on the internet seems to be much sparser and more obtuse than
> I like.
>
> So I'm asking here.
>
> As far as I understand, "tying the knot" refers to a way of using laziness
> to implement something like references in a purely functional way.
>
> I'm trying to write a toy simulation:
> I have a population :: [Person]
> I want to collect a random subset of possible pairs of distinct people.
> So I go to each person in the population and select a subset of the people
> after him/her in the list; these are pairs in which s/he is the first
> element.
>
> I want to then be able to ask for all pairs in which a person is the first
> or the second element.  I could give each person a unique id, but it seems
> like tying the knot is a valid way to implement this.
>
> Please correct me if I am misunderstanding.
>
> Thank you.
>
> --
>            Alex R
>
>
> _______________________________________________
> Beginners mailing list
> [email protected]
> http://www.haskell.org/mailman/listinfo/beginners
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: 
<http://www.haskell.org/pipermail/beginners/attachments/20101229/18fb7c4a/attachment-0001.htm>

------------------------------

Message: 5
Date: Wed, 29 Dec 2010 17:46:34 +0800
From: Lyndon Maydwell <[email protected]>
Subject: Re: [Haskell-beginners] strange behaviour : computing lowest
        divisor
To: Abhijit Ray <[email protected]>
Cc: beginners <[email protected]>
Message-ID:
        <[email protected]>
Content-Type: text/plain; charset=UTF-8

Try with Integer rather than Int. Might be an overflow issue...

On Wed, Dec 29, 2010 at 5:32 PM, Abhijit Ray <[email protected]> wrote:
> HI,
> I have written(copied) a small piece of code which finds the lowest divisor
> of am integer (greater than 1)
>
> the code is here
>
> ld::Int->Int
> ld n = ld' 2 n
> ?????? where
> ? ld' i n | rem n i == 0?= i
> ???????????????? | i^2 > n = n
> ???????????????? | otherwise = ld' (i+1) n
>
>
> now it seems to work fine for small numbers but for a big number we get
>
> *Main> ld 278970415063349480483707695
> 7
>
> the number is obviously divisible by 5 , so where is the anomaly ?
>
> Thanks,
> Abhijit
> On Wed, Dec 29, 2010 at 2:52 PM, Alex Rozenshteyn <[email protected]>
> wrote:
>>
>> I'm trying to understand the technique referred to as "tying the knot",
>> but documentation on the internet seems to be much sparser and more obtuse
>> than I like.
>>
>> So I'm asking here.
>>
>> As far as I understand, "tying the knot" refers to a way of using laziness
>> to implement something like references in a purely functional way.
>>
>> I'm trying to write a toy simulation:
>> I have a population :: [Person]
>> I want to collect a random subset of possible pairs of distinct people.
>> So I go to each person in the population and select a subset of the people
>> after him/her in the list; these are pairs in which s/he is the first
>> element.
>>
>> I want to then be able to ask for all pairs in which a person is the first
>> or the second element.? I could give each person a unique id, but it seems
>> like tying the knot is a valid way to implement this.
>>
>> Please correct me if I am misunderstanding.
>>
>> Thank you.
>>
>> --
>> ?? ? ? ? ?Alex R
>>
>> _______________________________________________
>> Beginners mailing list
>> [email protected]
>> http://www.haskell.org/mailman/listinfo/beginners
>>
>
>
> _______________________________________________
> Beginners mailing list
> [email protected]
> http://www.haskell.org/mailman/listinfo/beginners
>
>



------------------------------

_______________________________________________
Beginners mailing list
[email protected]
http://www.haskell.org/mailman/listinfo/beginners


End of Beginners Digest, Vol 30, Issue 44
*****************************************

Reply via email to