Send Beginners mailing list submissions to
[email protected]
To subscribe or unsubscribe via the World Wide Web, visit
http://www.haskell.org/mailman/listinfo/beginners
or, via email, send a message with subject or body 'help' to
[email protected]
You can reach the person managing the list at
[email protected]
When replying, please edit your Subject line so it is more specific
than "Re: Contents of Beginners digest..."
Today's Topics:
1. Re: Are these soloutions all valid and a good use of Haskell
(Roelof Wobben)
2. Re: Finding Keith Numbers with Haskell (Jack Mott)
3. Re: Are these soloutions all valid and a good use of Haskell
(Kees Bleijenberg)
4. Re: Finding Keith Numbers with Haskell (Kim-Ee Yeoh)
5. Re: Are these soloutions all valid and a good use of Haskell
(Roelof Wobben)
6. Re: Are these soloutions all valid and a good use of Haskell
(Kees Bleijenberg)
----------------------------------------------------------------------
Message: 1
Date: Tue, 11 Nov 2014 14:20:25 +0100
From: Roelof Wobben <[email protected]>
To: The Haskell-Beginners Mailing List - Discussion of primarily
beginner-level topics related to Haskell <[email protected]>
Subject: Re: [Haskell-beginners] Are these soloutions all valid and a
good use of Haskell
Message-ID: <[email protected]>
Content-Type: text/plain; charset="us-ascii"
An HTML attachment was scrubbed...
URL:
<http://www.haskell.org/pipermail/beginners/attachments/20141111/90dbef38/attachment-0001.html>
------------------------------
Message: 2
Date: Tue, 11 Nov 2014 07:26:53 -0600
From: Jack Mott <[email protected]>
To: The Haskell-Beginners Mailing List - Discussion of primarily
beginner-level topics related to Haskell <[email protected]>
Subject: Re: [Haskell-beginners] Finding Keith Numbers with Haskell
Message-ID:
<caou+pyh4jt_mjfteglqrtf13zxaptlu4qzdxg+tiuu4edim...@mail.gmail.com>
Content-Type: text/plain; charset="utf-8"
Thanks, is the notation with the single quote (isKeith') a convention for
helper functions or does it actually change something about the program?
I did try this approach as well, reversing the digits and adding elements
to the head, however having to use take and letting the list build up
larger seems to result in slightly worse performance overall.
On Mon, Nov 10, 2014 at 8:50 PM, Rein Henrichs <[email protected]>
wrote:
> Since summing is commutative (sum [a,b] = sum [b,a]), it doesn't matter
> what order they appear in. You can start out by reversing the digits and
> then by taking from and adding to the beginning of the list instead of the
> end. Also, isKeith needs to know the length of the decimal representation
> of n. Since you are already computing that representation in isKeithHelper,
> it might be best to compute this there as well and pass it into isKeith.
>
> (Also please consider the criterion library for benchmarking.)
>
> > import Data.Digits
>
> > isKeith :: Int -> Bool
> > isKeith n = let ds = digitsRev 10 n in isKeith' n (length ds) ds
>
> > isKeith' :: Int -> Int -> [Int] -> Bool
> > isKeith' n len ds
> > | n == s = True
> > | s > n = False
> > | otherwise = isKeith' n len (take len (s : ds))
> > where s = sum ds
>
> > main :: IO ()
> > main = print (isKeith 197)
>
> True
>
>
> _______________________________________________
> Beginners mailing list
> [email protected]
> http://www.haskell.org/mailman/listinfo/beginners
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL:
<http://www.haskell.org/pipermail/beginners/attachments/20141111/0fabea7b/attachment-0001.html>
------------------------------
Message: 3
Date: Tue, 11 Nov 2014 15:46:02 +0100
From: "Kees Bleijenberg" <[email protected]>
To: "'The Haskell-Beginners Mailing List - Discussion of primarily
beginner-level topics related to Haskell'" <[email protected]>
Subject: Re: [Haskell-beginners] Are these soloutions all valid and a
good use of Haskell
Message-ID: <000601cffdbe$374a1810$a5de4830$@[email protected]>
Content-Type: text/plain; charset="iso-8859-1"
The program traverses the list from right to left. The first element that
acc sees during traversal is the last element of the list.
The idea is as follows:
Parameter y is the partial calcresult.
If y (in your code) is Nothing then acc returns Just x. So the new partial
result is the current list element in a Just.
The next time acc is called, y is not Nothing but a Just. In that case acc
returns the partial calcresult unchanged.
So, y is Nothing for the last list element and Just <last element of the
list> for all other list elements.
last5 :: [a] -> Maybe a
last5 xs = foldr acc initial xs
acc :: a -> Maybe a -> Maybe a
acc x Nothing = Just x
acc _ aJust = aJust
initial :: Maybe a
initial = Nothing
In your code you don?t need the ?where? in the acc definition. Define acc
for Nothing and for Just.
Kees
---------------
Im now trying to make a rfold solution to this problem.
Till now I have this :
last5 :: [a] -> Maybe a
last5 xs = foldr acc initial xs
acc :: a -> Maybe a -> Maybe a
acc x y = where
??? acc x Nothing? = Just x
??? acc _ j??????? = j
initial :: Maybe a
initial = Nothing
So still not working.
Can someone explain to me in small steps how to figure out what the right
answer is to the acc part.
Roelof
Roelof Wobben schreef op 11-11-2014 8:20:
Thanks, this is working well.
But just for understanding
in the part acc x Nothing?? x is the input string,
and in the part _j means the same as (xs:x)
Roelof
?
Alex Hammel schreef op 10-11-2014 22:47:
It's an indentation issue. Make sure that there are no spaces before the bit
about 'last5 = foldr acc' etc.
On Mon, Nov 10, 2014 at 12:48 PM, Roelof Wobben <[email protected]> wrote:
Roelof Wobben schreef op 10-11-2014 20:59:
last5 :: [a] -> Maybe a
? ?last5 = foldr acc Nothing where
? ? ? ?acc x Nothing? = Just x
? ? ? ?acc _ j? ? ? ? = j
When I enter this in GHCI? I see this error :
Illegal type signature: ?[a] -> Maybe a last5?
_______________________________________________
Beginners mailing list
[email protected]
http://www.haskell.org/mailman/listinfo/beginners
_______________________________________________
Beginners mailing list
[email protected]
http://www.haskell.org/mailman/listinfo/beginners
------------------------------
Message: 4
Date: Tue, 11 Nov 2014 22:17:04 +0700
From: Kim-Ee Yeoh <[email protected]>
To: The Haskell-Beginners Mailing List - Discussion of primarily
beginner-level topics related to Haskell <[email protected]>
Subject: Re: [Haskell-beginners] Finding Keith Numbers with Haskell
Message-ID:
<capy+zdrpozak5ay4_ffij-2t7potwtpm4p2yyyza6dbmaop...@mail.gmail.com>
Content-Type: text/plain; charset="utf-8"
On Tue, Nov 11, 2014 at 8:26 PM, Jack Mott <[email protected]> wrote:
> Thanks, is the notation with the single quote (isKeith') a convention for
> helper functions or does it actually change something about the program?
An unspoken coding convention.
Function foo' would be read "foo-prime". E.g.of usage: haskell-prime is the
next standard of haskell, which became moot because you need good vibes for
diverse peoples to collaborate on such an undertaking, including supporting
it by writing more than one implementation.
Strictly syntax, the compiler doesn't treat it differently from any other
name label.
So yes, you could have foo-double-prime and so forth.
-- Kim-Ee
-------------- next part --------------
An HTML attachment was scrubbed...
URL:
<http://www.haskell.org/pipermail/beginners/attachments/20141111/774d380a/attachment-0001.html>
------------------------------
Message: 5
Date: Tue, 11 Nov 2014 17:35:28 +0100
From: Roelof Wobben <[email protected]>
To: The Haskell-Beginners Mailing List - Discussion of primarily
beginner-level topics related to Haskell <[email protected]>
Subject: Re: [Haskell-beginners] Are these soloutions all valid and a
good use of Haskell
Message-ID: <[email protected]>
Content-Type: text/plain; charset=windows-1252; format=flowed
Oke,
But what If I try it on a left folt.
Then it must be the other way.
let's say we have [1 , 2, 3]
so first it's nothing and 1 is taken.
so y could be one and nothuing must stay nothing because it's not the
last part,
The same for 2 , When it's three nothing must changed to just 3.
Fold is something I find it very difficult to wrap my head around it.
Maybe haskell or any functional proggramming language is not for me,
Roelof
Kees Bleijenberg schreef op 11-11-2014 15:46:
> The program traverses the list from right to left. The first element that
> acc sees during traversal is the last element of the list.
> The idea is as follows:
> Parameter y is the partial calcresult.
> If y (in your code) is Nothing then acc returns Just x. So the new partial
> result is the current list element in a Just.
> The next time acc is called, y is not Nothing but a Just. In that case acc
> returns the partial calcresult unchanged.
> So, y is Nothing for the last list element and Just <last element of the
> list> for all other list elements.
>
> last5 :: [a] -> Maybe a
> last5 xs = foldr acc initial xs
>
> acc :: a -> Maybe a -> Maybe a
> acc x Nothing = Just x
> acc _ aJust = aJust
>
> initial :: Maybe a
> initial = Nothing
>
> In your code you don?t need the ?where? in the acc definition. Define acc
> for Nothing and for Just.
>
> Kees
>
> ---------------
> Im now trying to make a rfold solution to this problem.
>
> Till now I have this :
>
> last5 :: [a] -> Maybe a
> last5 xs = foldr acc initial xs
>
> acc :: a -> Maybe a -> Maybe a
> acc x y = where
> acc x Nothing = Just x
> acc _ j = j
>
> initial :: Maybe a
> initial = Nothing
>
> So still not working.
>
> Can someone explain to me in small steps how to figure out what the right
> answer is to the acc part.
>
> Roelof
>
>
>
>
>
>
>
>
>
> Roelof Wobben schreef op 11-11-2014 8:20:
> Thanks, this is working well.
>
> But just for understanding
>
> in the part acc x Nothing x is the input string,
> and in the part _j means the same as (xs:x)
>
> Roelof
>
>
>
> Alex Hammel schreef op 10-11-2014 22:47:
> It's an indentation issue. Make sure that there are no spaces before the bit
> about 'last5 = foldr acc' etc.
>
> On Mon, Nov 10, 2014 at 12:48 PM, Roelof Wobben <[email protected]> wrote:
> Roelof Wobben schreef op 10-11-2014 20:59:
> last5 :: [a] -> Maybe a
> last5 = foldr acc Nothing where
> acc x Nothing = Just x
> acc _ j = j
>
> When I enter this in GHCI I see this error :
>
> Illegal type signature: ?[a] -> Maybe a last5?
>
>
>
>
> _______________________________________________
> Beginners mailing list
> [email protected]
> http://www.haskell.org/mailman/listinfo/beginners
>
>
>
>
> _______________________________________________
> Beginners mailing list
> [email protected]
> http://www.haskell.org/mailman/listinfo/beginners
>
>
>
> _______________________________________________
> Beginners mailing list
> [email protected]
> http://www.haskell.org/mailman/listinfo/beginners
>
------------------------------
Message: 6
Date: Tue, 11 Nov 2014 18:43:21 +0100
From: "Kees Bleijenberg" <[email protected]>
To: "'The Haskell-Beginners Mailing List - Discussion of primarily
beginner-level topics related to Haskell'" <[email protected]>
Subject: Re: [Haskell-beginners] Are these soloutions all valid and a
good use of Haskell
Message-ID: <000001cffdd6$fc774fa0$f565eee0$@[email protected]>
Content-Type: text/plain; charset="us-ascii"
Roelof,
The acc for the foldr doesn't work in foldl. But foldl is easier then foldr.
foldl traverses the list from left to right.
For foldl acc returns the current list element (second param) in a Just and
ignores the result of the partial calculation (param 1).
Walking through the list [1,2,4], acc returns for the first element Just 1,
for the second element Just 2 en finally Just 4. The result of the foldl is
in this case Just 4.
If the foldl is over the empty list, acc is never called and foldl returns
initial, so Nothing.
In code:
module Test()
where
last5 :: [a] -> Maybe a
last5 xs = foldl acc initial xs
acc :: Maybe a -> a -> Maybe a -- swapped params!
acc _ x = Just x
initial :: Maybe a
initial = Nothing
Kees
-----Oorspronkelijk bericht-----
Oke,
But what If I try it on a left folt.
Then it must be the other way.
let's say we have [1 , 2, 3]
so first it's nothing and 1 is taken.
so y could be one and nothuing must stay nothing because it's not the last
part, The same for 2 , When it's three nothing must changed to just 3.
Fold is something I find it very difficult to wrap my head around it.
Maybe haskell or any functional proggramming language is not for me,
Roelof
Kees Bleijenberg schreef op 11-11-2014 15:46:
> The program traverses the list from right to left. The first element
> that acc sees during traversal is the last element of the list.
> The idea is as follows:
> Parameter y is the partial calcresult.
> If y (in your code) is Nothing then acc returns Just x. So the new
> partial result is the current list element in a Just.
> The next time acc is called, y is not Nothing but a Just. In that case
> acc returns the partial calcresult unchanged.
> So, y is Nothing for the last list element and Just <last element of
> the
> list> for all other list elements.
>
> last5 :: [a] -> Maybe a
> last5 xs = foldr acc initial xs
>
> acc :: a -> Maybe a -> Maybe a
> acc x Nothing = Just x
> acc _ aJust = aJust
>
> initial :: Maybe a
> initial = Nothing
>
> In your code you don't need the 'where' in the acc definition. Define
> acc for Nothing and for Just.
>
> Kees
--- snip ----
------------------------------
Subject: Digest Footer
_______________________________________________
Beginners mailing list
[email protected]
http://www.haskell.org/mailman/listinfo/beginners
------------------------------
End of Beginners Digest, Vol 77, Issue 12
*****************************************