Re: [Haskell-cafe] Producing MinimumValue

2007-07-20 Thread Benja Fallenstein

2007/7/19, Jason Dagit [EMAIL PROTECTED]:

 I prefer,

 allEqual [] = True
 allEqual xs = foldl1 (==) xs

 But, unfortunately, it's not a one liner like yours (unless you allow
 allEqual [] = undefined).

Oh and silly me, that only works for [Bool].


My natural instinct is,

allEqual [] = True
allEqual (x:xs) = all (== x) xs

with the same caveat about allEqual [] as in your case.

- Benja
___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re[2]: [Haskell-cafe] Producing MinimumValue

2007-07-20 Thread Bulat Ziganshin
Hello Benja,

Friday, July 20, 2007, 6:10:15 PM, you wrote:
 My natural instinct is,

 allEqual [] = True
 allEqual (x:xs) = all (== x) xs

 with the same caveat about allEqual [] as in your case.

allEqual xs  =  all (== head xs) xs

-- 
Best regards,
 Bulatmailto:[EMAIL PROTECTED]

___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: Re[2]: [Haskell-cafe] Producing MinimumValue

2007-07-20 Thread Benja Fallenstein

2007/7/20, Bulat Ziganshin [EMAIL PROTECTED]:

 allEqual [] = True
 allEqual (x:xs) = all (== x) xs

 with the same caveat about allEqual [] as in your case.

allEqual xs  =  all (== head xs) xs


Rght. Not evaluated in the edge case, because xs is empty. Didn't
think of that, nice :-)

- Benja
___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] Producing MinimumValue

2007-07-19 Thread Alexteslin

The question suggests to use some functions defined in the section, and one
of them is iSort.
So i used the function but without using higher-order functions - i don't
know though how correct is it?

minimumValue :: [Int] - Int
minimumValue ns = head (iSort ns)

The other question is to test whether the values of allEqual on inputs 0 to
n are all equal.  Again, I defined the method but not sure if its concise?

allEqual :: [Int] - Bool
allEqual xs = length xs == length (filter isEqual xs)
where
isEqual n = (head xs) == n


Excuse me for questions like these, probably they are not interested ones
but I am just trying to learn the thinking behind the function definitions. 

Thank you



Joachim Breitner-2 wrote:
 
 Hi,
 
 Am Mittwoch, den 18.07.2007, 13:42 -0700 schrieb Alexteslin:
 I am trying to define a function as part of the exercises that gives a
 result of a minimum value of the input list of ints. Such as this:
 
 minimumValue :: [Int] - Int
 minimumValue ns ...
 
 using either filter or map functions but Not foldr1, because the exercise
 precedes the section on foldr1.
 I am confused because using filter - it generates a list on some
 constraint
 function for each element.  
 
 Any hints will be very appreciated.  Thank you  
 
 Are you allowed to define the function without any of filter or map,
 just yourself?
 
 minimumValue [a] = ...
 minimumValue (x:xs) = .. x .. minimumValue xs ..
 
 Greetings,
 Joachim
 
 -- 
 Joachim Breitner
   e-Mail: [EMAIL PROTECTED]
   Homepage: http://www.joachim-breitner.de
   ICQ#: 74513189
 ___
 Haskell-Cafe mailing list
 Haskell-Cafe@haskell.org
 http://www.haskell.org/mailman/listinfo/haskell-cafe
 
 

-- 
View this message in context: 
http://www.nabble.com/Producing-MinimumValue-tf4106379.html#a11694422
Sent from the Haskell - Haskell-Cafe mailing list archive at Nabble.com.

___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] Producing MinimumValue

2007-07-19 Thread Hugh Perkins

You can get the head and the tail by pattern matching.  Lets say you have a
function that takes a list:

myfunction list = -- do something here

go = myfuction [1,4,2,6]

... you can write the list bit of the function as (x:xs), where x is the
head, or first element, of the list, and xs is the tail, or the rest of the
list:

myfunction (x:xs) = -- do something here

then you can call myfunction on xs, and compare that to x, to give the
result.

This is recursive: the function calls itself over and over again, until at
some point it's going to execute myfuction [] or myfunction [6], which
is easy to handle, for example by adding the definition:

myfunction [a] = -- the value of myfunction given a

Go here for a really great tutorial:

http://www.cs.nott.ac.uk/~gmh/book.html

Recursive functions are in slides, number 6, but the tutorials are really
great: just start from 1 and work your way through.
___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] Producing MinimumValue

2007-07-19 Thread Steve Schafer
On Thu, 19 Jul 2007 10:55:19 -0700 (PDT), you wrote:

The question suggests to use some functions defined in the section, and one
of them is iSort.

Aha. Well, that one certainly lends itself better to this particular
proplen than either map or filter.

minimumValue :: [Int] - Int
minimumValue ns = head (iSort ns)

If I were going to use a sort, then yes, that's the way I would do it.
Of course, sorting isn't the best way to solve the problem, as sorting
will always be at least O(n * log n), whereas a more straightforward
algorithm would be O(n).

The other question is to test whether the values of allEqual on inputs 0 to
n are all equal.  Again, I defined the method but not sure if its concise?

allEqual :: [Int] - Bool
allEqual xs = length xs == length (filter isEqual xs)
   where
   isEqual n = (head xs) == n

Simple recursion is probably the most conceptually straightforward
approach here. One little difficulty with this problem (and with
minimumValue, too) is that the problem isn't completely specified,
because we don't know what answer we're supposed to give when the list
is empty. Let's assume that allEqual is supposed to return True on an
empty list. In that case, we can write it like this:

 allEqual :: [Int] - Bool
 allEqual (x1:x2:xs) = ???
 allEqual _  = ???

where the two ???s are left as an exercise for the reader.

-Steve
___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] Producing MinimumValue

2007-07-19 Thread Sebastian Sylvan

On 19/07/07, Steve Schafer [EMAIL PROTECTED] wrote:

On Thu, 19 Jul 2007 10:55:19 -0700 (PDT), you wrote:

The question suggests to use some functions defined in the section, and one
of them is iSort.

Aha. Well, that one certainly lends itself better to this particular
proplen than either map or filter.

minimumValue :: [Int] - Int
minimumValue ns = head (iSort ns)

If I were going to use a sort, then yes, that's the way I would do it.
Of course, sorting isn't the best way to solve the problem, as sorting
will always be at least O(n * log n), whereas a more straightforward
algorithm would be O(n).


Actually, since Haskell is lazy and only the first element is required
for minimumValue, the above algorithm should be O(n).


--
Sebastian Sylvan
+44(0)7857-300802
UIN: 44640862
___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] Producing MinimumValue

2007-07-19 Thread Steve Schafer
On Thu, 19 Jul 2007 19:26:39 +0100, you wrote:

Actually, since Haskell is lazy and only the first element is required
for minimumValue, the above algorithm should be O(n).

You're right (as long as the sort algorithm itself is sufficiently lazy,
of course).

Steve Schafer
Fenestra Technologies Corp.
http://www.fenestra.com/
___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] Producing MinimumValue

2007-07-19 Thread Alexteslin

I have defined the first line it seems right to me but second line not sure. 
I have True or False and whatever value i give it produces that value.

allEqual :: [Int] - Bool
allEqual (x1:x2:xs) = (x1 == x2)  allEqual xs
allEqual _  = ???



Steve Schafer wrote:
 
 On Thu, 19 Jul 2007 10:55:19 -0700 (PDT), you wrote:
 
The question suggests to use some functions defined in the section, and
one
of them is iSort.
 
 Aha. Well, that one certainly lends itself better to this particular
 proplen than either map or filter.
 
minimumValue :: [Int] - Int
minimumValue ns = head (iSort ns)
 
 If I were going to use a sort, then yes, that's the way I would do it.
 Of course, sorting isn't the best way to solve the problem, as sorting
 will always be at least O(n * log n), whereas a more straightforward
 algorithm would be O(n).
 
The other question is to test whether the values of allEqual on inputs 0
to
n are all equal.  Again, I defined the method but not sure if its concise?

allEqual :: [Int] - Bool
allEqual xs = length xs == length (filter isEqual xs)
  where
  isEqual n = (head xs) == n
 
 Simple recursion is probably the most conceptually straightforward
 approach here. One little difficulty with this problem (and with
 minimumValue, too) is that the problem isn't completely specified,
 because we don't know what answer we're supposed to give when the list
 is empty. Let's assume that allEqual is supposed to return True on an
 empty list. In that case, we can write it like this:
 
  allEqual :: [Int] - Bool
  allEqual (x1:x2:xs) = ???
  allEqual _  = ???
 
 where the two ???s are left as an exercise for the reader.
 
 -Steve
 ___
 Haskell-Cafe mailing list
 Haskell-Cafe@haskell.org
 http://www.haskell.org/mailman/listinfo/haskell-cafe
 
 

-- 
View this message in context: 
http://www.nabble.com/Producing-MinimumValue-tf4106379.html#a11696192
Sent from the Haskell - Haskell-Cafe mailing list archive at Nabble.com.

___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] Producing MinimumValue

2007-07-19 Thread Jonathan Cast
On Thursday 19 July 2007, Alexteslin wrote:
 I have defined the first line it seems right to me but second line not
 sure. I have True or False and whatever value i give it produces that
 value.

 allEqual :: [Int] - Bool
 allEqual (x1:x2:xs) = (x1 == x2)  allEqual xs
 allEqual _  = ???

Try defining just

allEqual [x1, x2, x3]

and then generalize that.

Jonathan Cast
http://sourceforge.net/projects/fid-core
http://sourceforge.net/projects/fid-emacs
___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] Producing MinimumValue

2007-07-19 Thread Dan Weston
The standard (and simplest) definition for universally quantified 
predicates (all) starts with True and looks for a False occurrance:


Prelude all even []
True

Conversely, existentially-quantified predicates (any) start with False 
and looks for a True occurrance:


Prelude any even []
False

I would define:

allEqual [] = True
allEqual [_]= True
allEqual (x1:x2:xs) = (x1 == x2)  allEqual xs

As a purely stylistic note, (per a previous mail thread) you don't need 
to worry about the order that mutually-exclusive patterns are listed, so 
I find it clearer to put the base case of an induction first, so I don't 
forget it.


Dan

Alexteslin wrote:
I have defined the first line it seems right to me but second line not sure. 
I have True or False and whatever value i give it produces that value.


allEqual :: [Int] - Bool
allEqual (x1:x2:xs) = (x1 == x2)  allEqual xs
allEqual _  = ???



Steve Schafer wrote:

On Thu, 19 Jul 2007 10:55:19 -0700 (PDT), you wrote:


The question suggests to use some functions defined in the section, and

one

of them is iSort.

Aha. Well, that one certainly lends itself better to this particular
proplen than either map or filter.


minimumValue :: [Int] - Int
minimumValue ns = head (iSort ns)

If I were going to use a sort, then yes, that's the way I would do it.
Of course, sorting isn't the best way to solve the problem, as sorting
will always be at least O(n * log n), whereas a more straightforward
algorithm would be O(n).


The other question is to test whether the values of allEqual on inputs 0

to

n are all equal.  Again, I defined the method but not sure if its concise?

allEqual :: [Int] - Bool
allEqual xs = length xs == length (filter isEqual xs)
where
isEqual n = (head xs) == n

Simple recursion is probably the most conceptually straightforward
approach here. One little difficulty with this problem (and with
minimumValue, too) is that the problem isn't completely specified,
because we don't know what answer we're supposed to give when the list
is empty. Let's assume that allEqual is supposed to return True on an
empty list. In that case, we can write it like this:

 allEqual :: [Int] - Bool
 allEqual (x1:x2:xs) = ???
 allEqual _  = ???

where the two ???s are left as an exercise for the reader.

-Steve
___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe







___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] Producing MinimumValue

2007-07-19 Thread Antoine Latter

On 7/19/07, Dan Weston [EMAIL PROTECTED] wrote:


I would define:

allEqual [] = True
allEqual [_]= True
allEqual (x1:x2:xs) = (x1 == x2)  allEqual xs


What does this function do for allEqual [1, 1, 2] ?

Antoine
___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] Producing MinimumValue

2007-07-19 Thread Steve Schafer
On Thu, 19 Jul 2007 12:30:06 -0700 (PDT), you wrote:

I have defined the first line it seems right to me

Close, but not quite. Think of the result that line would give on
[1,1,2].

Steve Schafer
Fenestra Technologies Corp.
http://www.fenestra.com/
___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] Producing MinimumValue

2007-07-19 Thread Dan Weston
Oops, you got me. I didn't even look at the third line, I just took it 
from the previous post. My first instinct actually was to write:


allEqual x@(h:t) = and (zipWith (==) x t)

but I don't think that zipWith is allowed in the question.

Dan


Antoine Latter wrote:

On 7/19/07, Dan Weston [EMAIL PROTECTED] wrote:


I would define:

allEqual [] = True
allEqual [_]= True
allEqual (x1:x2:xs) = (x1 == x2)  allEqual xs


What does this function do for allEqual [1, 1, 2] ?

Antoine
___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe





___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] Producing MinimumValue

2007-07-19 Thread Alexteslin

Thanks you, that works fine.  Sometimes I am not sure what the exercise asks. 
For example in the section of higher order functions i presume most
exercises would be to use those functions, but it seems that that not always
true.


Dan Weston wrote:
 
 The standard (and simplest) definition for universally quantified 
 predicates (all) starts with True and looks for a False occurrance:
 
 Prelude all even []
 True
 
 Conversely, existentially-quantified predicates (any) start with False 
 and looks for a True occurrance:
 
 Prelude any even []
 False
 
 I would define:
 
 allEqual [] = True
 allEqual [_]= True
 allEqual (x1:x2:xs) = (x1 == x2)  allEqual xs
 
 As a purely stylistic note, (per a previous mail thread) you don't need 
 to worry about the order that mutually-exclusive patterns are listed, so 
 I find it clearer to put the base case of an induction first, so I don't 
 forget it.
 
 Dan
 
 Alexteslin wrote:
 I have defined the first line it seems right to me but second line not
 sure. 
 I have True or False and whatever value i give it produces that value.
 
 allEqual :: [Int] - Bool
 allEqual (x1:x2:xs) = (x1 == x2)  allEqual xs
 allEqual _  = ???
 
 
 
 Steve Schafer wrote:
 On Thu, 19 Jul 2007 10:55:19 -0700 (PDT), you wrote:

 The question suggests to use some functions defined in the section, and
 one
 of them is iSort.
 Aha. Well, that one certainly lends itself better to this particular
 proplen than either map or filter.

 minimumValue :: [Int] - Int
 minimumValue ns = head (iSort ns)
 If I were going to use a sort, then yes, that's the way I would do it.
 Of course, sorting isn't the best way to solve the problem, as sorting
 will always be at least O(n * log n), whereas a more straightforward
 algorithm would be O(n).

 The other question is to test whether the values of allEqual on inputs
 0
 to
 n are all equal.  Again, I defined the method but not sure if its
 concise?

 allEqual :: [Int] - Bool
 allEqual xs = length xs == length (filter isEqual xs)
where
isEqual n = (head xs) == n
 Simple recursion is probably the most conceptually straightforward
 approach here. One little difficulty with this problem (and with
 minimumValue, too) is that the problem isn't completely specified,
 because we don't know what answer we're supposed to give when the list
 is empty. Let's assume that allEqual is supposed to return True on an
 empty list. In that case, we can write it like this:

  allEqual :: [Int] - Bool
  allEqual (x1:x2:xs) = ???
  allEqual _  = ???

 where the two ???s are left as an exercise for the reader.

 -Steve
 ___
 Haskell-Cafe mailing list
 Haskell-Cafe@haskell.org
 http://www.haskell.org/mailman/listinfo/haskell-cafe


 
 
 
 ___
 Haskell-Cafe mailing list
 Haskell-Cafe@haskell.org
 http://www.haskell.org/mailman/listinfo/haskell-cafe
 
 

-- 
View this message in context: 
http://www.nabble.com/Producing-MinimumValue-tf4106379.html#a11696723
Sent from the Haskell - Haskell-Cafe mailing list archive at Nabble.com.

___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] Producing MinimumValue

2007-07-19 Thread Jason Dagit

On 7/19/07, Dan Weston [EMAIL PROTECTED] wrote:

Oops, you got me. I didn't even look at the third line, I just took it
from the previous post. My first instinct actually was to write:

allEqual x@(h:t) = and (zipWith (==) x t)


I prefer,

allEqual [] = True
allEqual xs = foldl1 (==) xs

But, unfortunately, it's not a one liner like yours (unless you allow
allEqual [] = undefined).

Jason



but I don't think that zipWith is allowed in the question.

Dan


Antoine Latter wrote:
 On 7/19/07, Dan Weston [EMAIL PROTECTED] wrote:

 I would define:

 allEqual [] = True
 allEqual [_]= True
 allEqual (x1:x2:xs) = (x1 == x2)  allEqual xs

 What does this function do for allEqual [1, 1, 2] ?

 Antoine
 ___
 Haskell-Cafe mailing list
 Haskell-Cafe@haskell.org
 http://www.haskell.org/mailman/listinfo/haskell-cafe




___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe



___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] Producing MinimumValue

2007-07-19 Thread Jason Dagit

On 7/19/07, Jason Dagit [EMAIL PROTECTED] wrote:

On 7/19/07, Dan Weston [EMAIL PROTECTED] wrote:
 Oops, you got me. I didn't even look at the third line, I just took it
 from the previous post. My first instinct actually was to write:

 allEqual x@(h:t) = and (zipWith (==) x t)

I prefer,

allEqual [] = True
allEqual xs = foldl1 (==) xs

But, unfortunately, it's not a one liner like yours (unless you allow
allEqual [] = undefined).


Oh and silly me, that only works for [Bool].

I'll hushup :)

Jason



 but I don't think that zipWith is allowed in the question.

 Dan


 Antoine Latter wrote:
  On 7/19/07, Dan Weston [EMAIL PROTECTED] wrote:
 
  I would define:
 
  allEqual [] = True
  allEqual [_]= True
  allEqual (x1:x2:xs) = (x1 == x2)  allEqual xs
 
  What does this function do for allEqual [1, 1, 2] ?
 
  Antoine
  ___
  Haskell-Cafe mailing list
  Haskell-Cafe@haskell.org
  http://www.haskell.org/mailman/listinfo/haskell-cafe
 
 


 ___
 Haskell-Cafe mailing list
 Haskell-Cafe@haskell.org
 http://www.haskell.org/mailman/listinfo/haskell-cafe




___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] Producing MinimumValue

2007-07-19 Thread Dan Weston

The real lesson here is that

1) no problem is too easy to cheat good software engineering practices.
2) no solution not accompanied by at least a QuickCheck result should be 
viewed with suspicion


I obviously have to go back and relearn these lessons.

Dan

Jason Dagit wrote:

On 7/19/07, Jason Dagit [EMAIL PROTECTED] wrote:

On 7/19/07, Dan Weston [EMAIL PROTECTED] wrote:
 Oops, you got me. I didn't even look at the third line, I just took it
 from the previous post. My first instinct actually was to write:

 allEqual x@(h:t) = and (zipWith (==) x t)

I prefer,

allEqual [] = True
allEqual xs = foldl1 (==) xs

But, unfortunately, it's not a one liner like yours (unless you allow
allEqual [] = undefined).


Oh and silly me, that only works for [Bool].

I'll hushup :)

Jason



 but I don't think that zipWith is allowed in the question.

 Dan


 Antoine Latter wrote:
  On 7/19/07, Dan Weston [EMAIL PROTECTED] wrote:
 
  I would define:
 
  allEqual [] = True
  allEqual [_]= True
  allEqual (x1:x2:xs) = (x1 == x2)  allEqual xs
 
  What does this function do for allEqual [1, 1, 2] ?
 
  Antoine
  ___
  Haskell-Cafe mailing list
  Haskell-Cafe@haskell.org
  http://www.haskell.org/mailman/listinfo/haskell-cafe
 
 


 ___
 Haskell-Cafe mailing list
 Haskell-Cafe@haskell.org
 http://www.haskell.org/mailman/listinfo/haskell-cafe









___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] Producing MinimumValue

2007-07-19 Thread Alexteslin

What wrong with my original solution?

allEqual2 :: [Int] - Bool
allEqual2 xs = length xs == length (filter isEqual xs)
where
isEqual n = (head xs) == n

It looks simpler to me


Dan Weston wrote:
 
 The real lesson here is that
 
 1) no problem is too easy to cheat good software engineering practices.
 2) no solution not accompanied by at least a QuickCheck result should be 
 viewed with suspicion
 
 I obviously have to go back and relearn these lessons.
 
 Dan
 
 Jason Dagit wrote:
 On 7/19/07, Jason Dagit [EMAIL PROTECTED] wrote:
 On 7/19/07, Dan Weston [EMAIL PROTECTED] wrote:
  Oops, you got me. I didn't even look at the third line, I just took it
  from the previous post. My first instinct actually was to write:
 
  allEqual x@(h:t) = and (zipWith (==) x t)

 I prefer,

 allEqual [] = True
 allEqual xs = foldl1 (==) xs

 But, unfortunately, it's not a one liner like yours (unless you allow
 allEqual [] = undefined).
 
 Oh and silly me, that only works for [Bool].
 
 I'll hushup :)
 
 Jason
 
 
  but I don't think that zipWith is allowed in the question.
 
  Dan
 
 
  Antoine Latter wrote:
   On 7/19/07, Dan Weston [EMAIL PROTECTED] wrote:
  
   I would define:
  
   allEqual [] = True
   allEqual [_]= True
   allEqual (x1:x2:xs) = (x1 == x2)  allEqual xs
  
   What does this function do for allEqual [1, 1, 2] ?
  
   Antoine
   ___
   Haskell-Cafe mailing list
   Haskell-Cafe@haskell.org
   http://www.haskell.org/mailman/listinfo/haskell-cafe
  
  
 
 
  ___
  Haskell-Cafe mailing list
  Haskell-Cafe@haskell.org
  http://www.haskell.org/mailman/listinfo/haskell-cafe
 
 

 
 
 
 
 ___
 Haskell-Cafe mailing list
 Haskell-Cafe@haskell.org
 http://www.haskell.org/mailman/listinfo/haskell-cafe
 
 

-- 
View this message in context: 
http://www.nabble.com/Producing-MinimumValue-tf4106379.html#a11697071
Sent from the Haskell - Haskell-Cafe mailing list archive at Nabble.com.

___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] Producing MinimumValue

2007-07-19 Thread Dan Weston
The length command is too greedy. It wants to check the entire length of 
both lists, and only then compare. If you did this by hand, you would 
know to quit at the first nonequal element. To do this in Haskell, you 
should generally avoid length. Lazier functions such as and stop as 
soon as they can:


and (map isEqual xs)

doesn't really map to every xs. The and keeps asking map to check the 
next element for equality, but stops asking when it gets a negative 
answer. length doesn't know to stop asking.


Dan

Alexteslin wrote:

What wrong with my original solution?

allEqual2 :: [Int] - Bool
allEqual2 xs = length xs == length (filter isEqual xs)
where
isEqual n = (head xs) == n

It looks simpler to me


Dan Weston wrote:

The real lesson here is that

1) no problem is too easy to cheat good software engineering practices.
2) no solution not accompanied by at least a QuickCheck result should be 
viewed with suspicion


I obviously have to go back and relearn these lessons.

Dan

Jason Dagit wrote:

On 7/19/07, Jason Dagit [EMAIL PROTECTED] wrote:

On 7/19/07, Dan Weston [EMAIL PROTECTED] wrote:

Oops, you got me. I didn't even look at the third line, I just took it
from the previous post. My first instinct actually was to write:

allEqual x@(h:t) = and (zipWith (==) x t)

I prefer,

allEqual [] = True
allEqual xs = foldl1 (==) xs

But, unfortunately, it's not a one liner like yours (unless you allow
allEqual [] = undefined).

Oh and silly me, that only works for [Bool].

I'll hushup :)

Jason


but I don't think that zipWith is allowed in the question.

Dan


Antoine Latter wrote:

On 7/19/07, Dan Weston [EMAIL PROTECTED] wrote:

I would define:

allEqual [] = True
allEqual [_]= True
allEqual (x1:x2:xs) = (x1 == x2)  allEqual xs

What does this function do for allEqual [1, 1, 2] ?

Antoine
___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe




___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe






___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe







___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] Producing MinimumValue

2007-07-19 Thread Colin DeVilbiss

On 7/19/07, Alexteslin [EMAIL PROTECTED] wrote:

What wrong with my original solution?

allEqual2 :: [Int] - Bool
allEqual2 xs = length xs == length (filter isEqual xs)
where
isEqual n = (head xs) == n

It looks simpler to me


I believe it's correct, but the use of length and filter seems forced.

For example, for a very long list that has the second element
mismatch, the versions with explicit recursion (or fold variants) will
terminate with False as soon as a mismatch is found; the length
version will not terminate until both have been fully traversed.

If you're allowed to use everything, perhaps the clearest would be:

allEqual3 [] = True
allEqual3 (x:xs) = all (== x) xs

(with all built on top of a fold.)

Colin
___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] Producing MinimumValue

2007-07-19 Thread Hugh Perkins

On 7/19/07, Sebastian Sylvan [EMAIL PROTECTED] wrote:


Actually, since Haskell is lazy and only the first element is required
for minimumValue, the above algorithm should be O(n).



That's pretty cool :-)
___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


[Haskell-cafe] Producing MinimumValue

2007-07-18 Thread Alexteslin

Hello,

I am trying to define a function as part of the exercises that gives a
result of a minimum value of the input list of ints. Such as this:

minimumValue :: [Int] - Int
minimumValue ns ...

using either filter or map functions but Not foldr1, because the exercise
precedes the section on foldr1.
I am confused because using filter - it generates a list on some constraint
function for each element.  

Any hints will be very appreciated.  Thank you  
-- 
View this message in context: 
http://www.nabble.com/Producing-MinimumValue-tf4106379.html#a11677240
Sent from the Haskell - Haskell-Cafe mailing list archive at Nabble.com.

___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] Producing MinimumValue

2007-07-18 Thread Sebastian Sylvan

On 18/07/07, Alexteslin [EMAIL PROTECTED] wrote:


Hello,

I am trying to define a function as part of the exercises that gives a
result of a minimum value of the input list of ints. Such as this:

minimumValue :: [Int] - Int
minimumValue ns ...

using either filter or map functions but Not foldr1, because the exercise
precedes the section on foldr1.
I am confused because using filter - it generates a list on some constraint
function for each element.

Any hints will be very appreciated.  Thank you


Are you allowed to use primitive recursion?

minimumValue [x] = x
minimumValue (x:xs) = ... -- exercise!


--
Sebastian Sylvan
+44(0)7857-300802
UIN: 44640862
___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] Producing MinimumValue

2007-07-18 Thread Joachim Breitner
Hi,

Am Mittwoch, den 18.07.2007, 13:42 -0700 schrieb Alexteslin:
 I am trying to define a function as part of the exercises that gives a
 result of a minimum value of the input list of ints. Such as this:
 
 minimumValue :: [Int] - Int
 minimumValue ns ...
 
 using either filter or map functions but Not foldr1, because the exercise
 precedes the section on foldr1.
 I am confused because using filter - it generates a list on some constraint
 function for each element.  
 
 Any hints will be very appreciated.  Thank you  

Are you allowed to define the function without any of filter or map,
just yourself?

minimumValue [a] = ...
minimumValue (x:xs) = .. x .. minimumValue xs ..

Greetings,
Joachim

-- 
Joachim Breitner
  e-Mail: [EMAIL PROTECTED]
  Homepage: http://www.joachim-breitner.de
  ICQ#: 74513189
___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe