Can I check the length in a guarded expresion

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
9 messages Options
Reply | Threaded
Open this post in threaded view
|

Can I check the length in a guarded expresion

Roelof Wobben
Hello,

I try to make a guarded solution to find the last item in a list.

I thought I could do something like this

| length a == 1 = a

But then I see a lot of error messages.

Is there another way I can check the length of a list with guards ?

Roelof

_______________________________________________
Haskell-Cafe mailing list
[hidden email]
http://www.haskell.org/mailman/listinfo/haskell-cafe
Reply | Threaded
Open this post in threaded view
|

Re: Can I check the length in a guarded expresion

Francesco Ariis
On Sun, Nov 09, 2014 at 07:33:09PM +0100, Roelof Wobben wrote:
> Hello,
>
> I try to make a guarded solution to find the last item in a list.
>
> I thought I could do something like this
>
> | length a == 1 = a

^-- this is valid Haskell.

> But then I see a lot of error messages.

You should paste a complete example or the error or both, so
people can easily see what went wrong with your code
-F
_______________________________________________
Haskell-Cafe mailing list
[hidden email]
http://www.haskell.org/mailman/listinfo/haskell-cafe
Reply | Threaded
Open this post in threaded view
|

Re: Can I check the length in a guarded expresion

Bob Ippolito
In reply to this post by Roelof Wobben
You can do that. If you're getting an error, it's from a mistake on some other line. That said, you shouldn't write it that way. length requires a traversal of the entire list, so if the list is 1000 elements long it will take 1001 steps to compute that result when two steps would do. `length (take 2 a) == 1` would be a constant time way to get the result you want, although likely not the most elegant way to solve this exercise.

Perhaps this is the sort of question better suited for the haskell-beginners mailing list: https://www.haskell.org/mailman/listinfo/beginners

On Sun, Nov 9, 2014 at 10:33 AM, Roelof Wobben <[hidden email]> wrote:
Hello,

I try to make a guarded solution to find the last item in a list.

I thought I could do something like this

| length a == 1 = a

But then I see a lot of error messages.

Is there another way I can check the length of a list with guards ?

Roelof

_______________________________________________
Haskell-Cafe mailing list
[hidden email]
http://www.haskell.org/mailman/listinfo/haskell-cafe


_______________________________________________
Haskell-Cafe mailing list
[hidden email]
http://www.haskell.org/mailman/listinfo/haskell-cafe
Reply | Threaded
Open this post in threaded view
|

Re: Can I check the length in a guarded expresion

Roelof Wobben
Hello,

Here is the complete solution :

last3::[a]-> Maybe a;
last3 a
  | null a = Nothing
  | length a == 1 = a
  otherwise last3 (tail a)

And I see these error messages :

src/Main.hs@9:21-10:27
Couldn't match expected type ‘Bool -> ([a0] -> Maybe a0) -> [a] -> Maybe a’ with actual type
[a]
Relevant bindings include a :: [a] (bound at /home/app/isolation-runner-work/projects/75679/session.207/src/src/Main.hs:7:7) last3 :: [a] -> Maybe a (bound at /home/app/isolation-runner-work/projects/75679/session.207/src/src/Main.hs:7:1) The function
a
is applied to three arguments, but its type
[a]
has none




Bob Ippolito schreef op 9-11-2014 19:45:
You can do that. If you're getting an error, it's from a mistake on some other line. That said, you shouldn't write it that way. length requires a traversal of the entire list, so if the list is 1000 elements long it will take 1001 steps to compute that result when two steps would do. `length (take 2 a) == 1` would be a constant time way to get the result you want, although likely not the most elegant way to solve this exercise.

Perhaps this is the sort of question better suited for the haskell-beginners mailing list: https://www.haskell.org/mailman/listinfo/beginners

On Sun, Nov 9, 2014 at 10:33 AM, Roelof Wobben <[hidden email]> wrote:
Hello,

I try to make a guarded solution to find the last item in a list.

I thought I could do something like this

| length a == 1 = a

But then I see a lot of error messages.

Is there another way I can check the length of a list with guards ?

Roelof

_______________________________________________
Haskell-Cafe mailing list
[hidden email]
http://www.haskell.org/mailman/listinfo/haskell-cafe



_______________________________________________
Haskell-Cafe mailing list
[hidden email]
http://www.haskell.org/mailman/listinfo/haskell-cafe
Reply | Threaded
Open this post in threaded view
|

Re: Can I check the length in a guarded expresion

Jeffrey Brown
The first guard returns a Maybe [a]. The second and third return an [a]. To make them all return the same type, you should be wrapping the second and third return values in a Just constructor.

On Sun, Nov 9, 2014 at 10:51 AM, Roelof Wobben <[hidden email]> wrote:
Hello,

Here is the complete solution :

last3::[a]-> Maybe a;
last3 a
  | null a = Nothing
  | length a == 1 = a
  otherwise last3 (tail a)

And I see these error messages :

src/Main.hs@9:21-10:27
Couldn't match expected type ‘Bool -> ([a0] -> Maybe a0) -> [a] -> Maybe a’ with actual type
[a]
Relevant bindings include a :: [a] (bound at /home/app/isolation-runner-work/projects/75679/session.207/src/src/Main.hs:7:7) last3 :: [a] -> Maybe a (bound at /home/app/isolation-runner-work/projects/75679/session.207/src/src/Main.hs:7:1) The function
a
is applied to three arguments, but its type
[a]
has none




Bob Ippolito schreef op 9-11-2014 19:45:
You can do that. If you're getting an error, it's from a mistake on some other line. That said, you shouldn't write it that way. length requires a traversal of the entire list, so if the list is 1000 elements long it will take 1001 steps to compute that result when two steps would do. `length (take 2 a) == 1` would be a constant time way to get the result you want, although likely not the most elegant way to solve this exercise.

Perhaps this is the sort of question better suited for the haskell-beginners mailing list: https://www.haskell.org/mailman/listinfo/beginners

On Sun, Nov 9, 2014 at 10:33 AM, Roelof Wobben <[hidden email]> wrote:
Hello,

I try to make a guarded solution to find the last item in a list.

I thought I could do something like this

| length a == 1 = a

But then I see a lot of error messages.

Is there another way I can check the length of a list with guards ?

Roelof

_______________________________________________
Haskell-Cafe mailing list
[hidden email]
http://www.haskell.org/mailman/listinfo/haskell-cafe



_______________________________________________
Haskell-Cafe mailing list
[hidden email]
http://www.haskell.org/mailman/listinfo/haskell-cafe



_______________________________________________
Haskell-Cafe mailing list
[hidden email]
http://www.haskell.org/mailman/listinfo/haskell-cafe
Reply | Threaded
Open this post in threaded view
|

Re: Can I check the length in a guarded expresion

Britt Mathis
In reply to this post by Roelof Wobben

I believe your problem is that the type of your function is [a] -> Maybe a, but you are returning a and last3 tail a, anything you return that is a Maybe needs to have either the Nothing or the Just constructor.

On Nov 9, 2014 1:51 PM, "Roelof Wobben" <[hidden email]> wrote:
Hello,

Here is the complete solution :

last3::[a]-> Maybe a;
last3 a
  | null a = Nothing
  | length a == 1 = a
  otherwise last3 (tail a)

And I see these error messages :

src/Main.hs@9:21-10:27
Couldn't match expected type ‘Bool -> ([a0] -> Maybe a0) -> [a] -> Maybe a’ with actual type
[a]
Relevant bindings include a :: [a] (bound at /home/app/isolation-runner-work/projects/75679/session.207/src/src/Main.hs:7:7) last3 :: [a] -> Maybe a (bound at /home/app/isolation-runner-work/projects/75679/session.207/src/src/Main.hs:7:1) The function
a
is applied to three arguments, but its type
[a]
has none




Bob Ippolito schreef op 9-11-2014 19:45:
You can do that. If you're getting an error, it's from a mistake on some other line. That said, you shouldn't write it that way. length requires a traversal of the entire list, so if the list is 1000 elements long it will take 1001 steps to compute that result when two steps would do. `length (take 2 a) == 1` would be a constant time way to get the result you want, although likely not the most elegant way to solve this exercise.

Perhaps this is the sort of question better suited for the haskell-beginners mailing list: https://www.haskell.org/mailman/listinfo/beginners

On Sun, Nov 9, 2014 at 10:33 AM, Roelof Wobben <[hidden email]> wrote:
Hello,

I try to make a guarded solution to find the last item in a list.

I thought I could do something like this

| length a == 1 = a

But then I see a lot of error messages.

Is there another way I can check the length of a list with guards ?

Roelof

_______________________________________________
Haskell-Cafe mailing list
[hidden email]
http://www.haskell.org/mailman/listinfo/haskell-cafe



_______________________________________________
Haskell-Cafe mailing list
[hidden email]
http://www.haskell.org/mailman/listinfo/haskell-cafe


_______________________________________________
Haskell-Cafe mailing list
[hidden email]
http://www.haskell.org/mailman/listinfo/haskell-cafe
Reply | Threaded
Open this post in threaded view
|

Re: Can I check the length in a guarded expresion

Andy Morris-5
In reply to this post by Roelof Wobben
You also forgot the `|` and `=` either side of the `otherwise`. So the weird error is because GHC is understanding the second clause to be the application `a otherwise last3 (tail a)`.
On 09 Nov 2014, at 19:51, Roelof Wobben <[hidden email]> wrote:

Hello,

Here is the complete solution :

last3::[a]-> Maybe a;
last3 a
  | null a = Nothing
  | length a == 1 = a
  otherwise last3 (tail a)

And I see these error messages :

src/Main.hs@9:21-10:27
Couldn't match expected type ‘Bool -> ([a0] -> Maybe a0) -> [a] -> Maybe a’ with actual type
[a]
Relevant bindings include a :: [a] (bound at /home/app/isolation-runner-work/projects/75679/session.207/src/src/Main.hs:7:7) last3 :: [a] -> Maybe a (bound at /home/app/isolation-runner-work/projects/75679/session.207/src/src/Main.hs:7:1) The function
a
is applied to three arguments, but its type
[a]
has none




Bob Ippolito schreef op 9-11-2014 19:45:
You can do that. If you're getting an error, it's from a mistake on some other line. That said, you shouldn't write it that way. length requires a traversal of the entire list, so if the list is 1000 elements long it will take 1001 steps to compute that result when two steps would do. `length (take 2 a) == 1` would be a constant time way to get the result you want, although likely not the most elegant way to solve this exercise.

Perhaps this is the sort of question better suited for the haskell-beginners mailing list: https://www.haskell.org/mailman/listinfo/beginners

On Sun, Nov 9, 2014 at 10:33 AM, Roelof Wobben <[hidden email]> wrote:
Hello,

I try to make a guarded solution to find the last item in a list.

I thought I could do something like this

| length a == 1 = a

But then I see a lot of error messages.

Is there another way I can check the length of a list with guards ?

Roelof

_______________________________________________
Haskell-Cafe mailing list
[hidden email]
http://www.haskell.org/mailman/listinfo/haskell-cafe


_______________________________________________
Haskell-Cafe mailing list
[hidden email]
http://www.haskell.org/mailman/listinfo/haskell-cafe


_______________________________________________
Haskell-Cafe mailing list
[hidden email]
http://www.haskell.org/mailman/listinfo/haskell-cafe
Reply | Threaded
Open this post in threaded view
|

Re: Can I check the length in a guarded expresion

Roelof Wobben
Found it.

This is working ;

last3::[a]-> Maybe a;
last3 a
  | null a = Nothing
  | length a == 1 = Just (head a)
  | otherwise = last3 (tail a)





Andy Morris schreef op 9-11-2014 19:59:
You also forgot the `|` and `=` either side of the `otherwise`. So the weird error is because GHC is understanding the second clause to be the application `a otherwise last3 (tail a)`.
On 09 Nov 2014, at 19:51, Roelof Wobben <[hidden email]> wrote:

Hello,

Here is the complete solution :

last3::[a]-> Maybe a;
last3 a
  | null a = Nothing
  | length a == 1 = a
  otherwise last3 (tail a)

And I see these error messages :

src/Main.hs@9:21-10:27
Couldn't match expected type ‘Bool -> ([a0] -> Maybe a0) -> [a] -> Maybe a’ with actual type
[a]
Relevant bindings include a :: [a] (bound at /home/app/isolation-runner-work/projects/75679/session.207/src/src/Main.hs:7:7) last3 :: [a] -> Maybe a (bound at /home/app/isolation-runner-work/projects/75679/session.207/src/src/Main.hs:7:1) The function
a
is applied to three arguments, but its type
[a]
has none




Bob Ippolito schreef op 9-11-2014 19:45:
You can do that. If you're getting an error, it's from a mistake on some other line. That said, you shouldn't write it that way. length requires a traversal of the entire list, so if the list is 1000 elements long it will take 1001 steps to compute that result when two steps would do. `length (take 2 a) == 1` would be a constant time way to get the result you want, although likely not the most elegant way to solve this exercise.

Perhaps this is the sort of question better suited for the haskell-beginners mailing list: https://www.haskell.org/mailman/listinfo/beginners

On Sun, Nov 9, 2014 at 10:33 AM, Roelof Wobben <[hidden email]> wrote:
Hello,

I try to make a guarded solution to find the last item in a list.

I thought I could do something like this

| length a == 1 = a

But then I see a lot of error messages.

Is there another way I can check the length of a list with guards ?

Roelof

_______________________________________________
Haskell-Cafe mailing list
[hidden email]
http://www.haskell.org/mailman/listinfo/haskell-cafe


_______________________________________________
Haskell-Cafe mailing list
[hidden email]
http://www.haskell.org/mailman/listinfo/haskell-cafe



_______________________________________________
Haskell-Cafe mailing list
[hidden email]
http://www.haskell.org/mailman/listinfo/haskell-cafe
Reply | Threaded
Open this post in threaded view
|

Re: Can I check the length in a guarded expresion

Richard A. O'Keefe

On 10/11/2014, at 8:07 am, Roelof Wobben <[hidden email]> wrote:

> Found it.
>
> This is working ;
>
> last3::[a]-> Maybe a;
> last3 a
>   | null a = Nothing
>   | length a == 1 = Just (head a)
>   | otherwise = last3 (tail a)

I believe it has already been mentioned,
but what you have here is an O(|a|**2) solution.

The way I’d write it is

    last4 :: [a] -> Maybe a

    last4 []     = Nothing
    last4 (x:xs) = Just $ loop x xs
      where loop x [] = x
            loop _ (y:ys) = loop y ys


In ghci,
*Main> :set +s

*Main> last3 [1..10000]
Just 10000
(0.14 secs, 13696656 bytes)
*Main> last4 [1..10000]
Just 10000
(0.00 secs, 3131552 bytes)

*Main> last3 [1..100000]
Just 100000
(11.92 secs, 33134136 bytes)
*Main> last4 [1..100000]
Just 100000
(0.02 secs, 15520360 bytes)

You really _don’t_ want to be calling length in a loop like that.

length a == 1 if and only if a == [_]

Using null, head, tail is not really the Haskell Way.

last3 []     = Nothing
last3 [x]    = Just x
last3 (_:xs) = last3 xs


_______________________________________________
Haskell-Cafe mailing list
[hidden email]
http://www.haskell.org/mailman/listinfo/haskell-cafe