Are these solution the Haskell way ?

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

Are these solution the Haskell way ?

Roelof Wobben
Hello,

For practising pattern matching and recursion I did recreate some
commands of Data,list.

My re-implementation of ++ :

plusplus :: [a] -> [a] -> [a]
plusplus [] [] = [] ;
plusplus [] (xs) = xs
plusplus (xs) [] = xs
plusplus (xs) yx = plusplus' (reverse xs) yx

plusplus' :: [a] -> [a] -> [a]
plusplus' [] (xs) = xs
plusplus' (x:xs) yx = plusplus' xs (x:yx)

main = print $ plusplus ["a","b"] ["c","d"]

my re-implementation of init :

import Data.Maybe

-- | The main entry point.
init' :: [a] -> Maybe [a]
init' [] = Nothing
init' [x] = Just []
init' (x:xs) = Just (x:fromMaybe xs (init' xs))

main = print . init' $ [1,3]


my re-implementation of last :

-- | The main entry point.
last' :: [a] -> Maybe a
last' [] = Nothing
last' [x] = Just x
last' (_:xs) = last' xs

main = print . last' $ []

Now I wonder if these solutions are the haskell way ? if not so, how can
I improve them ,

Roelof


---
Dit e-mailbericht is gecontroleerd op virussen met Avast antivirussoftware.
http://www.avast.com

_______________________________________________
Beginners mailing list
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
Reply | Threaded
Open this post in threaded view
|

Re: Are these solution the Haskell way ?

Daniel P. Wright
Hi Roelof,

If you don't consider it cheating (and I suggest you shouldn't, having had a stab at the answers), you will find great enlightenment looking at how these functions are *actually* implemented in the wild.  Did you know that there is a "source" link for each function on Hackage?  By clicking on that, for your first example, you can compare the actual implementation of (++) with your "plusplus" function:


(By the way, it's that function in particular that gave me one of my first "Aha!" moments in Haskell... it's quite beautiful in its simplicity).

One thing with viewing the source on Hackage is that sometimes it can be a little more confusing than it needs to be for the sake of efficiency.  A really good source for good, readable examples of Prelude functions in Haskell is the Haskell Report:


(In this case, though, the implementation is the same).

Having a shot at defining these library functions yourself, as you have done, and then comparing your version with the "official" version in the prelude is a great way to learn good style!

-Dani. 

2015-05-13 15:10 GMT+09:00 Roelof Wobben <[hidden email]>:
Hello,

For practising pattern matching and recursion I did recreate some commands of Data,list.

My re-implementation of ++ :

plusplus :: [a] -> [a] -> [a]
plusplus [] [] = [] ;
plusplus [] (xs) = xs
plusplus (xs) [] = xs
plusplus (xs) yx = plusplus' (reverse xs) yx

plusplus' :: [a] -> [a] -> [a]
plusplus' [] (xs) = xs
plusplus' (x:xs) yx = plusplus' xs (x:yx)

main = print $ plusplus ["a","b"] ["c","d"]

my re-implementation of init :

import Data.Maybe

-- | The main entry point.
init' :: [a] -> Maybe [a]
init' [] = Nothing
init' [x] = Just []
init' (x:xs) = Just (x:fromMaybe xs (init' xs))

main = print . init' $ [1,3]


my re-implementation of last :

-- | The main entry point.
last' :: [a] -> Maybe a
last' [] = Nothing
last' [x] = Just x
last' (_:xs) = last' xs

main = print . last' $ []

Now I wonder if these solutions are the haskell way ? if not so, how can I improve them ,

Roelof


---
Dit e-mailbericht is gecontroleerd op virussen met Avast antivirussoftware.
http://www.avast.com

_______________________________________________
Beginners mailing list
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners


_______________________________________________
Beginners mailing list
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
Reply | Threaded
Open this post in threaded view
|

Re: Are these solution the Haskell way ?

Roelof Wobben
Thanks,

If I re-implement it like this :

plusplus :: [a] -> [a] -> [a]
plusplus [] (xs) = xs
plusplus (x:xs) yx = x : xs plusplus yx
 
main = print $ ["a","b"] plusplus ["c","d"]


I see this error appear :

src/Main.hs@3:26-3:40
Couldn't match expected type ‘([a0] -> [a0] -> [a0]) -> [a] -> [a]’ with actual type
[a]
Relevant bindings include yx :: [a] (bound at /home/app/isolation-runner-work/projects/112825/session.207/src/src/Main.hs:3:17) xs :: [a] (bound at /home/app/isolation-runner-work/projects/112825/session.207/src/src/Main.hs:3:13) x :: a (bound at /home/app/isolation-runner-work/projects/112825/session.207/src/src/Main.hs:3:11) plusplus :: [a] -> [a] -> [a] (bound at /home/app/isolation-runner-work/projects/112825/session.207/src/src/Main.hs:2:1) The function
xs
is applied to two arguments, but its type
[a]
has none

So for me not a aha moment.  I was hoping I would get it

Roelof


Daniel P. Wright schreef op 13-5-2015 om 8:27:
Hi Roelof,

If you don't consider it cheating (and I suggest you shouldn't, having had a stab at the answers), you will find great enlightenment looking at how these functions are *actually* implemented in the wild.  Did you know that there is a "source" link for each function on Hackage?  By clicking on that, for your first example, you can compare the actual implementation of (++) with your "plusplus" function:


(By the way, it's that function in particular that gave me one of my first "Aha!" moments in Haskell... it's quite beautiful in its simplicity).

One thing with viewing the source on Hackage is that sometimes it can be a little more confusing than it needs to be for the sake of efficiency.  A really good source for good, readable examples of Prelude functions in Haskell is the Haskell Report:


(In this case, though, the implementation is the same).

Having a shot at defining these library functions yourself, as you have done, and then comparing your version with the "official" version in the prelude is a great way to learn good style!

-Dani. 

2015-05-13 15:10 GMT+09:00 Roelof Wobben <[hidden email]>:
Hello,

For practising pattern matching and recursion I did recreate some commands of Data,list.

My re-implementation of ++ :

plusplus :: [a] -> [a] -> [a]
plusplus [] [] = [] ;
plusplus [] (xs) = xs
plusplus (xs) [] = xs
plusplus (xs) yx = plusplus' (reverse xs) yx

plusplus' :: [a] -> [a] -> [a]
plusplus' [] (xs) = xs
plusplus' (x:xs) yx = plusplus' xs (x:yx)

main = print $ plusplus ["a","b"] ["c","d"]

my re-implementation of init :

import Data.Maybe

-- | The main entry point.
init' :: [a] -> Maybe [a]
init' [] = Nothing
init' [x] = Just []
init' (x:xs) = Just (x:fromMaybe xs (init' xs))

main = print . init' $ [1,3]


my re-implementation of last :

-- | The main entry point.
last' :: [a] -> Maybe a
last' [] = Nothing
last' [x] = Just x
last' (_:xs) = last' xs

main = print . last' $ []

Now I wonder if these solutions are the haskell way ? if not so, how can I improve them ,

Roelof


---
Dit e-mailbericht is gecontroleerd op virussen met Avast antivirussoftware.
http://www.avast.com

_______________________________________________
Beginners mailing list
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners



_______________________________________________
Beginners mailing list
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners




Avast logo

Dit e-mailbericht is gecontroleerd op virussen met Avast antivirussoftware.
www.avast.com



_______________________________________________
Beginners mailing list
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
Reply | Threaded
Open this post in threaded view
|

Re: Are these solution the Haskell way ?

Daniel P. Wright
Ah, that's just a small syntactic issue -- in Haskell, operators are infix (go between the arguments) by default, but named functions are not.  So you would have to write it:

plusplus [] xs = xs
plusplus (x:xs) ys = x : plusplus xs ys


2015-05-13 15:39 GMT+09:00 Roelof Wobben <[hidden email]>:
Thanks,

If I re-implement it like this :

plusplus :: [a] -> [a] -> [a]
plusplus [] (xs) = xs
plusplus (x:xs) yx = x : xs plusplus yx
 
main = print $ ["a","b"] plusplus ["c","d"]


I see this error appear :

src/Main.hs@3:26-3:40
Couldn't match expected type ‘([a0] -> [a0] -> [a0]) -> [a] -> [a]’ with actual type
[a]
Relevant bindings include yx :: [a] (bound at /home/app/isolation-runner-work/projects/112825/session.207/src/src/Main.hs:3:17) xs :: [a] (bound at /home/app/isolation-runner-work/projects/112825/session.207/src/src/Main.hs:3:13) x :: a (bound at /home/app/isolation-runner-work/projects/112825/session.207/src/src/Main.hs:3:11) plusplus :: [a] -> [a] -> [a] (bound at /home/app/isolation-runner-work/projects/112825/session.207/src/src/Main.hs:2:1) The function
xs
is applied to two arguments, but its type
[a]
has none

So for me not a aha moment.  I was hoping I would get it

Roelof


Daniel P. Wright schreef op 13-5-2015 om 8:27:
Hi Roelof,

If you don't consider it cheating (and I suggest you shouldn't, having had a stab at the answers), you will find great enlightenment looking at how these functions are *actually* implemented in the wild.  Did you know that there is a "source" link for each function on Hackage?  By clicking on that, for your first example, you can compare the actual implementation of (++) with your "plusplus" function:


(By the way, it's that function in particular that gave me one of my first "Aha!" moments in Haskell... it's quite beautiful in its simplicity).

One thing with viewing the source on Hackage is that sometimes it can be a little more confusing than it needs to be for the sake of efficiency.  A really good source for good, readable examples of Prelude functions in Haskell is the Haskell Report:


(In this case, though, the implementation is the same).

Having a shot at defining these library functions yourself, as you have done, and then comparing your version with the "official" version in the prelude is a great way to learn good style!

-Dani. 

2015-05-13 15:10 GMT+09:00 Roelof Wobben <[hidden email]>:
Hello,

For practising pattern matching and recursion I did recreate some commands of Data,list.

My re-implementation of ++ :

plusplus :: [a] -> [a] -> [a]
plusplus [] [] = [] ;
plusplus [] (xs) = xs
plusplus (xs) [] = xs
plusplus (xs) yx = plusplus' (reverse xs) yx

plusplus' :: [a] -> [a] -> [a]
plusplus' [] (xs) = xs
plusplus' (x:xs) yx = plusplus' xs (x:yx)

main = print $ plusplus ["a","b"] ["c","d"]

my re-implementation of init :

import Data.Maybe

-- | The main entry point.
init' :: [a] -> Maybe [a]
init' [] = Nothing
init' [x] = Just []
init' (x:xs) = Just (x:fromMaybe xs (init' xs))

main = print . init' $ [1,3]


my re-implementation of last :

-- | The main entry point.
last' :: [a] -> Maybe a
last' [] = Nothing
last' [x] = Just x
last' (_:xs) = last' xs

main = print . last' $ []

Now I wonder if these solutions are the haskell way ? if not so, how can I improve them ,

Roelof


---
Dit e-mailbericht is gecontroleerd op virussen met Avast antivirussoftware.
http://www.avast.com

_______________________________________________
Beginners mailing list
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners



_______________________________________________
Beginners mailing list
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners




Avast logo

Dit e-mailbericht is gecontroleerd op virussen met Avast antivirussoftware.
www.avast.com



_______________________________________________
Beginners mailing list
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners



_______________________________________________
Beginners mailing list
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
Reply | Threaded
Open this post in threaded view
|

Re: Are these solution the Haskell way ?

Roelof Wobben
Thanks,

So the answer is x and the rest of xs and ys.
How do x then get added to ys.

Roelof

Daniel P. Wright schreef op 13-5-2015 om 8:52:
Ah, that's just a small syntactic issue -- in Haskell, operators are infix (go between the arguments) by default, but named functions are not.  So you would have to write it:

plusplus [] xs = xs
plusplus (x:xs) ys = x : plusplus xs ys


2015-05-13 15:39 GMT+09:00 Roelof Wobben <[hidden email]>:
Thanks,

If I re-implement it like this :

plusplus :: [a] -> [a] -> [a]
plusplus [] (xs) = xs
plusplus (x:xs) yx = x : xs plusplus yx
 
main = print $ ["a","b"] plusplus ["c","d"]


I see this error appear :

src/Main.hs@3:26-3:40
Couldn't match expected type ‘([a0] -> [a0] -> [a0]) -> [a] -> [a]’ with actual type
[a]
Relevant bindings include yx :: [a] (bound at /home/app/isolation-runner-work/projects/112825/session.207/src/src/Main.hs:3:17) xs :: [a] (bound at /home/app/isolation-runner-work/projects/112825/session.207/src/src/Main.hs:3:13) x :: a (bound at /home/app/isolation-runner-work/projects/112825/session.207/src/src/Main.hs:3:11) plusplus :: [a] -> [a] -> [a] (bound at /home/app/isolation-runner-work/projects/112825/session.207/src/src/Main.hs:2:1) The function
xs
is applied to two arguments, but its type
[a]
has none

So for me not a aha moment.  I was hoping I would get it

Roelof


Daniel P. Wright schreef op 13-5-2015 om 8:27:
Hi Roelof,

If you don't consider it cheating (and I suggest you shouldn't, having had a stab at the answers), you will find great enlightenment looking at how these functions are *actually* implemented in the wild.  Did you know that there is a "source" link for each function on Hackage?  By clicking on that, for your first example, you can compare the actual implementation of (++) with your "plusplus" function:


(By the way, it's that function in particular that gave me one of my first "Aha!" moments in Haskell... it's quite beautiful in its simplicity).

One thing with viewing the source on Hackage is that sometimes it can be a little more confusing than it needs to be for the sake of efficiency.  A really good source for good, readable examples of Prelude functions in Haskell is the Haskell Report:


(In this case, though, the implementation is the same).

Having a shot at defining these library functions yourself, as you have done, and then comparing your version with the "official" version in the prelude is a great way to learn good style!

-Dani. 

2015-05-13 15:10 GMT+09:00 Roelof Wobben <[hidden email]>:
Hello,

For practising pattern matching and recursion I did recreate some commands of Data,list.

My re-implementation of ++ :

plusplus :: [a] -> [a] -> [a]
plusplus [] [] = [] ;
plusplus [] (xs) = xs
plusplus (xs) [] = xs
plusplus (xs) yx = plusplus' (reverse xs) yx

plusplus' :: [a] -> [a] -> [a]
plusplus' [] (xs) = xs
plusplus' (x:xs) yx = plusplus' xs (x:yx)

main = print $ plusplus ["a","b"] ["c","d"]

my re-implementation of init :

import Data.Maybe

-- | The main entry point.
init' :: [a] -> Maybe [a]
init' [] = Nothing
init' [x] = Just []
init' (x:xs) = Just (x:fromMaybe xs (init' xs))

main = print . init' $ [1,3]


my re-implementation of last :

-- | The main entry point.
last' :: [a] -> Maybe a
last' [] = Nothing
last' [x] = Just x
last' (_:xs) = last' xs

main = print . last' $ []

Now I wonder if these solutions are the haskell way ? if not so, how can I improve them ,

Roelof


---
Dit e-mailbericht is gecontroleerd op virussen met Avast antivirussoftware.
http://www.avast.com

_______________________________________________
Beginners mailing list
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners



_______________________________________________
Beginners mailing list
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners




Avast logo

Dit e-mailbericht is gecontroleerd op virussen met Avast antivirussoftware.
www.avast.com



_______________________________________________
Beginners mailing list
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners




_______________________________________________
Beginners mailing list
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners




Avast logo

Dit e-mailbericht is gecontroleerd op virussen met Avast antivirussoftware.
www.avast.com



_______________________________________________
Beginners mailing list
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
Reply | Threaded
Open this post in threaded view
|

Re: Are these solution the Haskell way ?

Daniel P. Wright
Think about what the possible values of "xs" might be, and trace through the next call to "plusplus xs ys".

It would help if I didn't name the variables stupidly... here is a slightly better version:

plusplus [] ys = ys
plusplus (x:xs) ys = x : plusplus xs ys

If it helps, try tracing through the steps required to evaluate "plusplus [1, 2] [3, 4]" manually (using a pen and paper, not on the computer)

2015-05-13 15:55 GMT+09:00 Roelof Wobben <[hidden email]>:
Thanks,

So the answer is x and the rest of xs and ys.
How do x then get added to ys.

Roelof

Daniel P. Wright schreef op 13-5-2015 om 8:52:
Ah, that's just a small syntactic issue -- in Haskell, operators are infix (go between the arguments) by default, but named functions are not.  So you would have to write it:

plusplus [] xs = xs
plusplus (x:xs) ys = x : plusplus xs ys


2015-05-13 15:39 GMT+09:00 Roelof Wobben <[hidden email]>:
Thanks,

If I re-implement it like this :

plusplus :: [a] -> [a] -> [a]
plusplus [] (xs) = xs
plusplus (x:xs) yx = x : xs plusplus yx
 
main = print $ ["a","b"] plusplus ["c","d"]


I see this error appear :

src/Main.hs@3:26-3:40
Couldn't match expected type ‘([a0] -> [a0] -> [a0]) -> [a] -> [a]’ with actual type
[a]
Relevant bindings include yx :: [a] (bound at /home/app/isolation-runner-work/projects/112825/session.207/src/src/Main.hs:3:17) xs :: [a] (bound at /home/app/isolation-runner-work/projects/112825/session.207/src/src/Main.hs:3:13) x :: a (bound at /home/app/isolation-runner-work/projects/112825/session.207/src/src/Main.hs:3:11) plusplus :: [a] -> [a] -> [a] (bound at /home/app/isolation-runner-work/projects/112825/session.207/src/src/Main.hs:2:1) The function
xs
is applied to two arguments, but its type
[a]
has none

So for me not a aha moment.  I was hoping I would get it

Roelof


Daniel P. Wright schreef op 13-5-2015 om 8:27:
Hi Roelof,

If you don't consider it cheating (and I suggest you shouldn't, having had a stab at the answers), you will find great enlightenment looking at how these functions are *actually* implemented in the wild.  Did you know that there is a "source" link for each function on Hackage?  By clicking on that, for your first example, you can compare the actual implementation of (++) with your "plusplus" function:


(By the way, it's that function in particular that gave me one of my first "Aha!" moments in Haskell... it's quite beautiful in its simplicity).

One thing with viewing the source on Hackage is that sometimes it can be a little more confusing than it needs to be for the sake of efficiency.  A really good source for good, readable examples of Prelude functions in Haskell is the Haskell Report:


(In this case, though, the implementation is the same).

Having a shot at defining these library functions yourself, as you have done, and then comparing your version with the "official" version in the prelude is a great way to learn good style!

-Dani. 

2015-05-13 15:10 GMT+09:00 Roelof Wobben <[hidden email]>:
Hello,

For practising pattern matching and recursion I did recreate some commands of Data,list.

My re-implementation of ++ :

plusplus :: [a] -> [a] -> [a]
plusplus [] [] = [] ;
plusplus [] (xs) = xs
plusplus (xs) [] = xs
plusplus (xs) yx = plusplus' (reverse xs) yx

plusplus' :: [a] -> [a] -> [a]
plusplus' [] (xs) = xs
plusplus' (x:xs) yx = plusplus' xs (x:yx)

main = print $ plusplus ["a","b"] ["c","d"]

my re-implementation of init :

import Data.Maybe

-- | The main entry point.
init' :: [a] -> Maybe [a]
init' [] = Nothing
init' [x] = Just []
init' (x:xs) = Just (x:fromMaybe xs (init' xs))

main = print . init' $ [1,3]


my re-implementation of last :

-- | The main entry point.
last' :: [a] -> Maybe a
last' [] = Nothing
last' [x] = Just x
last' (_:xs) = last' xs

main = print . last' $ []

Now I wonder if these solutions are the haskell way ? if not so, how can I improve them ,

Roelof


---
Dit e-mailbericht is gecontroleerd op virussen met Avast antivirussoftware.
http://www.avast.com

_______________________________________________
Beginners mailing list
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners



_______________________________________________
Beginners mailing list
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners




Avast logo

Dit e-mailbericht is gecontroleerd op virussen met Avast antivirussoftware.
www.avast.com



_______________________________________________
Beginners mailing list
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners




_______________________________________________
Beginners mailing list
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners




Avast logo

Dit e-mailbericht is gecontroleerd op virussen met Avast antivirussoftware.
www.avast.com



_______________________________________________
Beginners mailing list
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners



_______________________________________________
Beginners mailing list
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
Reply | Threaded
Open this post in threaded view
|

Re: Are these solution the Haskell way ?

Frerich Raabe
In reply to this post by Roelof Wobben
Roelof,

since these functions are all pure (i.e. solely defined through their
arguments) you can apply equational reasoning, which means: everytime the
'plusplus' function is called you can replace that call with the definition
of the function without changing the behaviour of the program.

That allows you to just evaluate a call manually, expanding each function
call by hand. I added some extra parentheses to emphasize the functions being
evaluate:

   plusplus [1,2,3] [4,5,6]                    | apply 'plusplus', second
pattern for 'plusplus' matches
   == 1 : (plusplus [2,3] [4,5,6])             | "
   == 1 : (2 : (plusplus [3] [4,5,6]))         | "
   == 1 : (2 : (3 : (plusplus [] [4,5,6])))    | apply 'plusplus', first
pattern for 'plusplus' matches
   == 1 : (2 : (3 : [4,5,6])                   | apply (:) function
   == 1 : (2 : [3,4,5,6])                      | "
   == 1 : [2,3,4,5,6]                          | "
   == [1,2,3,4,5,6]                            | "


On 2015-05-13 08:55, Roelof Wobben wrote:

> Thanks,
>
>  So the answer is x and the rest of xs and ys.
>  How do x then get added to ys.
>
>  Roelof
>
>  Daniel P. Wright schreef op 13-5-2015 om 8:52:
>
>> Ah, that's just a small syntactic issue -- in Haskell, operators are infix
>> (go between the arguments) by default, but named functions are
>> not. So you would have to write it:
>>
>> plusplus [] xs = xs
>> plusplus (x:xs) ys = x : plusplus xs ys
>>
>> 2015-05-13 15:39 GMT+09:00 Roelof Wobben <[hidden email]>:
>>
>> Thanks,
>>
>> If I re-implement it like this :
>>
>> plusplus :: [a] -> [a] -> [a]
>> plusplus [] (xs) = xs
>> plusplus (x:xs) yx = x : xs plusplus yx
>>
>> main = print $ ["a","b"] plusplus ["c","d"]
>>
>> I see this error appear :
>>
>> src/Main.hs@3:26-3:40
>> Couldn't match expected type ‘([a0] -> [a0] -> [a0]) -> [a] -> [a]’ with
>> actual type
>> [a] Relevant bindings include yx :: [a] (bound at
>> /home/app/isolation-runner-work/projects/112825/session.207/src/src/Main.hs:3:17)
>> xs ::
>> [a] (bound at
>> /home/app/isolation-runner-work/projects/112825/session.207/src/src/Main.hs:3:13)
>> x :: a (bound at
>> /home/app/isolation-runner-work/projects/112825/session.207/src/src/Main.hs:3:11)
>> plusplus :: [a] -> [a] -> [a] (bound at
>> /home/app/isolation-runner-work/projects/112825/session.207/src/src/Main.hs:2:1)
>> The function
>> xs is applied to two arguments, but its type
>> [a] has none …
>>
>> So for me not a aha moment. I was hoping I would get it
>>
>> Roelof
>>
>> Daniel P. Wright schreef op 13-5-2015 om 8:27:
>>
>> Hi Roelof,
>>
>> If you don't consider it cheating (and I suggest you shouldn't, having had
>> a stab at the answers), you will find great enlightenment looking
>> at how these functions are *actually* implemented in the wild. Did you know
>> that there is a "source" link for each function on Hackage? By
>> clicking on that, for your first example, you can compare the actual
>> implementation of (++) with your "plusplus" function:
>>
>> http://hackage.haskell.org/package/base-4.8.0.0/docs/src/GHC-Base.html#%2B%2B 
>> [2]
>>
>> (By the way, it's that function in particular that gave me one of my first
>> "Aha!" moments in Haskell... it's quite beautiful in its
>> simplicity).
>>
>> One thing with viewing the source on Hackage is that sometimes it can be a
>> little more confusing than it needs to be for the sake of
>> efficiency. A really good source for good, readable examples of Prelude
>> functions in Haskell is the Haskell Report:
>>
>> https://www.haskell.org/onlinereport/standard-prelude.html [3]
>>
>> (In this case, though, the implementation is the same).
>>
>> Having a shot at defining these library functions yourself, as you have
>> done, and then comparing your version with the "official" version in
>> the prelude is a great way to learn good style!
>>
>> -Dani.
>>
>> 2015-05-13 15:10 GMT+09:00 Roelof Wobben <[hidden email]>:
>> Hello,
>>
>> For practising pattern matching and recursion I did recreate some commands
>> of Data,list.
>>
>> My re-implementation of ++ :
>>
>> plusplus :: [a] -> [a] -> [a]
>> plusplus [] [] = [] ;
>> plusplus [] (xs) = xs
>> plusplus (xs) [] = xs
>> plusplus (xs) yx = plusplus' (reverse xs) yx
>>
>> plusplus' :: [a] -> [a] -> [a]
>> plusplus' [] (xs) = xs
>> plusplus' (x:xs) yx = plusplus' xs (x:yx)
>>
>> main = print $ plusplus ["a","b"] ["c","d"]
>>
>> my re-implementation of init :
>>
>> import Data.Maybe
>>
>> -- | The main entry point.
>> init' :: [a] -> Maybe [a]
>> init' [] = Nothing
>> init' [x] = Just []
>> init' (x:xs) = Just (x:fromMaybe xs (init' xs))
>>
>> main = print . init' $ [1,3]
>>
>> my re-implementation of last :
>>
>> -- | The main entry point.
>> last' :: [a] -> Maybe a
>> last' [] = Nothing
>> last' [x] = Just x
>> last' (_:xs) = last' xs
>>
>> main = print . last' $ []
>>
>> Now I wonder if these solutions are the haskell way ? if not so, how can I
>> improve them ,
>>
>> Roelof
>>
>> ---
>> Dit e-mailbericht is gecontroleerd op virussen met Avast antivirussoftware.
>> http://www.avast.com [4]
>>
>> _______________________________________________
>> Beginners mailing list
>> [hidden email]
>> http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners [1]
>>
>> _______________________________________________
>> Beginners mailing list
>> [hidden email]
>> http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners [1]
>
> -------------------------
>
>   [5]
>
>  Dit e-mailbericht is gecontroleerd op virussen met Avast antivirussoftware.
>  www.avast.com [5]
>
>  _______________________________________________
>  Beginners mailing list
>  [hidden email]
>  http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners [1]
>
> _______________________________________________
> Beginners mailing list
> [hidden email]
> http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners [1]
>
> -------------------------
>
>   [5]
>
>  Dit e-mailbericht is gecontroleerd op virussen met Avast antivirussoftware.
> www.avast.com [5]
>
>
>
> Links:
> ------
> [1] http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
> [2]
> http://hackage.haskell.org/package/base-4.8.0.0/docs/src/GHC-Base.html#%2B%2B
> [3] https://www.haskell.org/onlinereport/standard-prelude.html
> [4] http://www.avast.com
> [5] http://www.avast.com/
>
> _______________________________________________
> Beginners mailing list
> [hidden email]
> http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners

--
Frerich Raabe - [hidden email]
www.froglogic.com - Multi-Platform GUI Testing
_______________________________________________
Beginners mailing list
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
Reply | Threaded
Open this post in threaded view
|

Re: Are these solution the Haskell way ?

Roelof Wobben
In reply to this post by Daniel P. Wright
Hello,

I see it.

plusplus [1,2] [3,4]
plusplus  [1] : plusplus [2] [3,4]
plusplus [1] :  [2] : plusplus [] [3,4]
plusplus [1] : [2] : [3,4]
[1:2:3:4]

I think I need some more practice on this.
Does anyone know a good exercise ?

Roelof


Daniel P. Wright schreef op 13-5-2015 om 9:08:
Think about what the possible values of "xs" might be, and trace through the next call to "plusplus xs ys".

It would help if I didn't name the variables stupidly... here is a slightly better version:

plusplus [] ys = ys
plusplus (x:xs) ys = x : plusplus xs ys

If it helps, try tracing through the steps required to evaluate "plusplus [1, 2] [3, 4]" manually (using a pen and paper, not on the computer)

2015-05-13 15:55 GMT+09:00 Roelof Wobben <[hidden email]>:
Thanks,

So the answer is x and the rest of xs and ys.
How do x then get added to ys.

Roelof

Daniel P. Wright schreef op 13-5-2015 om 8:52:
Ah, that's just a small syntactic issue -- in Haskell, operators are infix (go between the arguments) by default, but named functions are not.  So you would have to write it:

plusplus [] xs = xs
plusplus (x:xs) ys = x : plusplus xs ys


2015-05-13 15:39 GMT+09:00 Roelof Wobben <[hidden email]>:
Thanks,

If I re-implement it like this :

plusplus :: [a] -> [a] -> [a]
plusplus [] (xs) = xs
plusplus (x:xs) yx = x : xs plusplus yx
 
main = print $ ["a","b"] plusplus ["c","d"]


I see this error appear :

src/Main.hs@3:26-3:40
Couldn't match expected type ‘([a0] -> [a0] -> [a0]) -> [a] -> [a]’ with actual type
[a]
Relevant bindings include yx :: [a] (bound at /home/app/isolation-runner-work/projects/112825/session.207/src/src/Main.hs:3:17) xs :: [a] (bound at /home/app/isolation-runner-work/projects/112825/session.207/src/src/Main.hs:3:13) x :: a (bound at /home/app/isolation-runner-work/projects/112825/session.207/src/src/Main.hs:3:11) plusplus :: [a] -> [a] -> [a] (bound at /home/app/isolation-runner-work/projects/112825/session.207/src/src/Main.hs:2:1) The function
xs
is applied to two arguments, but its type
[a]
has none

So for me not a aha moment.  I was hoping I would get it

Roelof


Daniel P. Wright schreef op 13-5-2015 om 8:27:
Hi Roelof,

If you don't consider it cheating (and I suggest you shouldn't, having had a stab at the answers), you will find great enlightenment looking at how these functions are *actually* implemented in the wild.  Did you know that there is a "source" link for each function on Hackage?  By clicking on that, for your first example, you can compare the actual implementation of (++) with your "plusplus" function:


(By the way, it's that function in particular that gave me one of my first "Aha!" moments in Haskell... it's quite beautiful in its simplicity).

One thing with viewing the source on Hackage is that sometimes it can be a little more confusing than it needs to be for the sake of efficiency.  A really good source for good, readable examples of Prelude functions in Haskell is the Haskell Report:


(In this case, though, the implementation is the same).

Having a shot at defining these library functions yourself, as you have done, and then comparing your version with the "official" version in the prelude is a great way to learn good style!

-Dani. 

2015-05-13 15:10 GMT+09:00 Roelof Wobben <[hidden email]>:
Hello,

For practising pattern matching and recursion I did recreate some commands of Data,list.

My re-implementation of ++ :

plusplus :: [a] -> [a] -> [a]
plusplus [] [] = [] ;
plusplus [] (xs) = xs
plusplus (xs) [] = xs
plusplus (xs) yx = plusplus' (reverse xs) yx

plusplus' :: [a] -> [a] -> [a]
plusplus' [] (xs) = xs
plusplus' (x:xs) yx = plusplus' xs (x:yx)

main = print $ plusplus ["a","b"] ["c","d"]

my re-implementation of init :

import Data.Maybe

-- | The main entry point.
init' :: [a] -> Maybe [a]
init' [] = Nothing
init' [x] = Just []
init' (x:xs) = Just (x:fromMaybe xs (init' xs))

main = print . init' $ [1,3]


my re-implementation of last :

-- | The main entry point.
last' :: [a] -> Maybe a
last' [] = Nothing
last' [x] = Just x
last' (_:xs) = last' xs

main = print . last' $ []

Now I wonder if these solutions are the haskell way ? if not so, how can I improve them ,

Roelof


---
Dit e-mailbericht is gecontroleerd op virussen met Avast antivirussoftware.
http://www.avast.com

_______________________________________________
Beginners mailing list
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners



_______________________________________________
Beginners mailing list
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners




Avast logo

Dit e-mailbericht is gecontroleerd op virussen met Avast antivirussoftware.
www.avast.com



_______________________________________________
Beginners mailing list
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners




_______________________________________________
Beginners mailing list
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners




Avast logo

Dit e-mailbericht is gecontroleerd op virussen met Avast antivirussoftware.
www.avast.com



_______________________________________________
Beginners mailing list
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners




_______________________________________________
Beginners mailing list
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners




Avast logo

Dit e-mailbericht is gecontroleerd op virussen met Avast antivirussoftware.
www.avast.com



_______________________________________________
Beginners mailing list
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
Reply | Threaded
Open this post in threaded view
|

Re: Are these solution the Haskell way ?

Sumit Sahrawat, Maths & Computing,
 IIT (BHU)
The evaluation would go the way Frerich shows it. In particular you should know that these two notations are equivalent.

[1,2,3] == 1 : 2 : 3 : []

The cons (:) operator (with type a -> [a] -> [a]) adds an element to the start of a list. Also, as it is right associative

    1 : 2 : 3 : []
==  1 : (2 : (3 : []))   { right associativity }
==  1 : (2 : [3])        { add element to empty list }
==  1 : [2, 3]           { add element }
==  [1, 2, 3]            { add element }

On 13 May 2015 at 12:50, Roelof Wobben <[hidden email]> wrote:
Hello,

I see it.

plusplus [1,2] [3,4]
plusplus  [1] : plusplus [2] [3,4]
plusplus [1] :  [2] : plusplus [] [3,4]
plusplus [1] : [2] : [3,4]
[1:2:3:4]

I think I need some more practice on this.
Does anyone know a good exercise ?

Roelof


Daniel P. Wright schreef op 13-5-2015 om 9:08:
Think about what the possible values of "xs" might be, and trace through the next call to "plusplus xs ys".

It would help if I didn't name the variables stupidly... here is a slightly better version:

plusplus [] ys = ys
plusplus (x:xs) ys = x : plusplus xs ys

If it helps, try tracing through the steps required to evaluate "plusplus [1, 2] [3, 4]" manually (using a pen and paper, not on the computer)

2015-05-13 15:55 GMT+09:00 Roelof Wobben <[hidden email]>:
Thanks,

So the answer is x and the rest of xs and ys.
How do x then get added to ys.

Roelof

Daniel P. Wright schreef op 13-5-2015 om 8:52:
Ah, that's just a small syntactic issue -- in Haskell, operators are infix (go between the arguments) by default, but named functions are not.  So you would have to write it:

plusplus [] xs = xs
plusplus (x:xs) ys = x : plusplus xs ys


2015-05-13 15:39 GMT+09:00 Roelof Wobben <[hidden email]>:
Thanks,

If I re-implement it like this :

plusplus :: [a] -> [a] -> [a]
plusplus [] (xs) = xs
plusplus (x:xs) yx = x : xs plusplus yx
 
main = print $ ["a","b"] plusplus ["c","d"]


I see this error appear :

src/Main.hs@3:26-3:40
Couldn't match expected type ‘([a0] -> [a0] -> [a0]) -> [a] -> [a]’ with actual type
[a]
Relevant bindings include yx :: [a] (bound at /home/app/isolation-runner-work/projects/112825/session.207/src/src/Main.hs:3:17) xs :: [a] (bound at /home/app/isolation-runner-work/projects/112825/session.207/src/src/Main.hs:3:13) x :: a (bound at /home/app/isolation-runner-work/projects/112825/session.207/src/src/Main.hs:3:11) plusplus :: [a] -> [a] -> [a] (bound at /home/app/isolation-runner-work/projects/112825/session.207/src/src/Main.hs:2:1) The function
xs
is applied to two arguments, but its type
[a]
has none

So for me not a aha moment.  I was hoping I would get it

Roelof


Daniel P. Wright schreef op 13-5-2015 om 8:27:
Hi Roelof,

If you don't consider it cheating (and I suggest you shouldn't, having had a stab at the answers), you will find great enlightenment looking at how these functions are *actually* implemented in the wild.  Did you know that there is a "source" link for each function on Hackage?  By clicking on that, for your first example, you can compare the actual implementation of (++) with your "plusplus" function:


(By the way, it's that function in particular that gave me one of my first "Aha!" moments in Haskell... it's quite beautiful in its simplicity).

One thing with viewing the source on Hackage is that sometimes it can be a little more confusing than it needs to be for the sake of efficiency.  A really good source for good, readable examples of Prelude functions in Haskell is the Haskell Report:


(In this case, though, the implementation is the same).

Having a shot at defining these library functions yourself, as you have done, and then comparing your version with the "official" version in the prelude is a great way to learn good style!

-Dani. 

2015-05-13 15:10 GMT+09:00 Roelof Wobben <[hidden email]>:
Hello,

For practising pattern matching and recursion I did recreate some commands of Data,list.

My re-implementation of ++ :

plusplus :: [a] -> [a] -> [a]
plusplus [] [] = [] ;
plusplus [] (xs) = xs
plusplus (xs) [] = xs
plusplus (xs) yx = plusplus' (reverse xs) yx

plusplus' :: [a] -> [a] -> [a]
plusplus' [] (xs) = xs
plusplus' (x:xs) yx = plusplus' xs (x:yx)

main = print $ plusplus ["a","b"] ["c","d"]

my re-implementation of init :

import Data.Maybe

-- | The main entry point.
init' :: [a] -> Maybe [a]
init' [] = Nothing
init' [x] = Just []
init' (x:xs) = Just (x:fromMaybe xs (init' xs))

main = print . init' $ [1,3]


my re-implementation of last :

-- | The main entry point.
last' :: [a] -> Maybe a
last' [] = Nothing
last' [x] = Just x
last' (_:xs) = last' xs

main = print . last' $ []

Now I wonder if these solutions are the haskell way ? if not so, how can I improve them ,

Roelof


---
Dit e-mailbericht is gecontroleerd op virussen met Avast antivirussoftware.
http://www.avast.com

_______________________________________________
Beginners mailing list
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners



_______________________________________________
Beginners mailing list
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners




Avast logo

Dit e-mailbericht is gecontroleerd op virussen met Avast antivirussoftware.
www.avast.com



_______________________________________________
Beginners mailing list
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners




_______________________________________________
Beginners mailing list
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners




Avast logo

Dit e-mailbericht is gecontroleerd op virussen met Avast antivirussoftware.
www.avast.com



_______________________________________________
Beginners mailing list
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners




_______________________________________________
Beginners mailing list
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners




Avast logo

Dit e-mailbericht is gecontroleerd op virussen met Avast antivirussoftware.
www.avast.com



_______________________________________________
Beginners mailing list
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners




--
Regards

Sumit Sahrawat

_______________________________________________
Beginners mailing list
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners