|
|
Subject: fixed point - msg#00200
List: lang.haskell.cafe
Hi -
I am trying to do Exercise 9.9 in HSOE; and I've come up with an
answer that works but I'm not sure if it answers the question properly. The
problem is:
The Question:
-------------
Suppose we define a function "fix" as:
fix f = f (fix f)
Suppose further we have a recursive function:
remainder :: Integer -> Integer -> Integer
remainder a b = if a < b then a
else remainder (a - b) b
Rewrite this function using "fix" so that it's not recursive.
My Answer:
----------
wierdFunc x y z = if y - z > z then ((\a b c -> a b c) x (y-z) z)
else ((\a b c -> a b c) (\d e -> d) (y-z) z)
myRemainder = fix wierdFunc
My Question:
------------
Does the ((\a b c -> a b c) x (y-z) z) mean that I've not removed the
recursion? I was assuming that I was returning a function that is to be
evaluated and not actually doing any recursion. That's why I thought I
answered the question. However, I have a headache now and would like
another opinion.
thanks,
-andrew
Was this page helpful?
Thread at a glance:
Previous Message by Date:
click to view message preview
Re: Regex parser for Dfa
G'day.
Quoting Graham Klyne <GK@xxxxxxxxxxxxxx>:
> I tackled this as an exercise in writing a lightweight Parsec parser. A
> copy is attached.
Thanks, I've checked it in.
> I do have a sourceforge count (GrahamK), but I'm not familiar with the
> procedure for submitting a file.
I'll mail you offlist.
Cheers,
Andrew Bromage
Next Message by Date:
click to view message preview
Re: fixed point
On Sun, 26 Oct 2003 19:24:26 -0500
"Harris, Andrew" <Andrew.Harris@xxxxxxxxxx> wrote:
> Hi -
>
> I am trying to do Exercise 9.9 in HSOE; and I've come up with an
> answer that works but I'm not sure if it answers the question
> properly. The problem is:
>
> The Question:
> -------------
>
> Suppose we define a function "fix" as:
>
> fix f = f (fix f)
>
> Suppose further we have a recursive function:
>
> remainder :: Integer -> Integer -> Integer
> remainder a b = if a < b then a
> else remainder (a - b) b
>
> Rewrite this function using "fix" so that it's not recursive.
>
> My Answer:
> ----------
>
> wierdFunc x y z = if y - z > z then ((\a b c -> a b c) x (y-z) z)
> else ((\a b c -> a b c) (\d e -> d) (y-z) z)
>
> myRemainder = fix wierdFunc
>
> My Question:
> ------------
>
> Does the ((\a b c -> a b c) x (y-z) z) mean that I've not removed the
> recursion? I was assuming that I was returning a function that is to
> be evaluated and not actually doing any recursion. That's why I
> thought I answered the question. However, I have a headache now and
> would like another opinion.
Notice that, (\x -> x) a reduces to a, so (\a b c -> a b c) x (y-z) z
reduces to x (y-z) z. You can therefore simplify your function quite a
bit.
wierdFunc x y z = if y-z > z then x (y-z) z else (\d e -> d) (y-z) z
and you can still apply that lambda abstraction (beta-reduce)
wierdFunc x y z = if y-z > z then x (y-z) z else y-z
None of these (except, of course, fix and remainder) are recursive. A
recursive function is just one that calls itself. For wierdFunc to be
recursive, the identifier wierdFunc would have to occur in the
right-hand side of it's definition.
This all said, you are making the problem much more difficult than it
needs to be. Try matching up your x,y,z's to things in remainder and I
think the expected answer will become obvious. Also, you may want to
look at the type of fix and wierdFunc (you can use :type in Hugs or
GHCi).
Previous Message by Thread:
click to view message preview
random numbers
I'm very much a beginning programmer, studying Haskell. I have two
questions -
Firstly, I am trying to generate random numbers in Haskell, but although I
have found a 'random' library on www.zvon.org, I don't really know how to
include library functions, and the documentation given doesn't really tell
me the effective difference between all the functions provided in the
library. In another interesting development, I couldn't find any mention
of this library in haskell.org's list of libraries.
Secondly, but perhaps more importantly, is there a more 'beginnerish' list
that I should be addressing this to? I've been following discussions on
this one and they don't seem to be at quite this level.
Thanks
Jac
Next Message by Thread:
click to view message preview
Re: fixed point
On Sun, 26 Oct 2003 19:24:26 -0500
"Harris, Andrew" <Andrew.Harris@xxxxxxxxxx> wrote:
> Hi -
>
> I am trying to do Exercise 9.9 in HSOE; and I've come up with an
> answer that works but I'm not sure if it answers the question
> properly. The problem is:
>
> The Question:
> -------------
>
> Suppose we define a function "fix" as:
>
> fix f = f (fix f)
>
> Suppose further we have a recursive function:
>
> remainder :: Integer -> Integer -> Integer
> remainder a b = if a < b then a
> else remainder (a - b) b
>
> Rewrite this function using "fix" so that it's not recursive.
>
> My Answer:
> ----------
>
> wierdFunc x y z = if y - z > z then ((\a b c -> a b c) x (y-z) z)
> else ((\a b c -> a b c) (\d e -> d) (y-z) z)
>
> myRemainder = fix wierdFunc
>
> My Question:
> ------------
>
> Does the ((\a b c -> a b c) x (y-z) z) mean that I've not removed the
> recursion? I was assuming that I was returning a function that is to
> be evaluated and not actually doing any recursion. That's why I
> thought I answered the question. However, I have a headache now and
> would like another opinion.
Notice that, (\x -> x) a reduces to a, so (\a b c -> a b c) x (y-z) z
reduces to x (y-z) z. You can therefore simplify your function quite a
bit.
wierdFunc x y z = if y-z > z then x (y-z) z else (\d e -> d) (y-z) z
and you can still apply that lambda abstraction (beta-reduce)
wierdFunc x y z = if y-z > z then x (y-z) z else y-z
None of these (except, of course, fix and remainder) are recursive. A
recursive function is just one that calls itself. For wierdFunc to be
recursive, the identifier wierdFunc would have to occur in the
right-hand side of it's definition.
This all said, you are making the problem much more difficult than it
needs to be. Try matching up your x,y,z's to things in remainder and I
think the expected answer will become obvious. Also, you may want to
look at the type of fix and wierdFunc (you can use :type in Hugs or
GHCi).
|
|