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. type problems with my little code (raffa f)
2. About scope range: outside of the do-block (S. H. Aegis)
3. Re: About scope range: outside of the do-block (Roel van Dijk)
4. Re: About scope range: outside of the do-block (Norbert Melzer)
5. Re: About scope range: outside of the do-block (S. H. Aegis)
6. Re: About scope range: outside of the do-block (S. H. Aegis)
----------------------------------------------------------------------
Message: 1
Date: Sun, 20 Apr 2014 09:35:52 -0300
From: raffa f <[email protected]>
To: [email protected]
Subject: [Haskell-beginners] type problems with my little code
Message-ID:
<CAHdWBe+gUv=aags9ygurxatybo2gz+clpmlkq6_btuqtiow...@mail.gmail.com>
Content-Type: text/plain; charset="utf-8"
hi everyone, i'm a complete beginner on programming and i've been solving
the 99 haskell problems and i've come into a situation. just like all my
problems with haskell, it's about types. i wrote a little thing that takes
a list and puts all the equal characters together, like [1,1,3,2,3] will be
[ [1,1], [3,3], [2] ]. here it is:
pack xs = pack' xs
where
pack' :: [a] -> [[a]]
pack' [] = []
pack' (x:y) = extra x xs ++ pack' y
extra x xs = [sum' (map (remover x) (map ((==) x) xs))]
remover :: a -> Bool -> [a]
remover y x = if x then [y] else []
sum' :: [[a]] -> [a]
sum' [] = []
sum' (x:xs) = x ++ sum' xs
i know, i know, this code is probably terrible and i'm sure there are more
clever ways to do this...but i wanna understand what's wrong. the "extra"
function works perfectly, it takes a variable and then looks at how many
times it's presented on a list and outputs a list with a list of that
variable the amount of times that it was presented on the original list.
however, pack does not work quite right. first of all, it'll use extra on
repeated characters, creating repeated lists, but that doesn't matter
because i have a function that fixes that issue and i'll use it after i
figure out what's wrong with pack. now, the real problem is with types.
here's what haskell says when i try to load it:
noventa.hs:77:32:
Couldn't match type `a' with `a1'
`a' is a rigid type variable bound by
the inferred type of pack :: [a] -> [[a]] at noventa.hs:73:1
`a1' is a rigid type variable bound by
the type signature for pack' :: [a1] -> [[a1]] at
noventa.hs:75:19
Expected type: [a1]
Actual type: [a]
In the second argument of `extra', namely `xs'
In the first argument of `(++)', namely `extra x xs'
In the expression: extra x xs ++ pack' y
Failed, modules loaded: none.
i don't understand this at all! if i replace [a] with String and [[a]] with
[String], it works! but i want pack to work for lists of numbers too...
types are so confusing. can anyone help me?
-------------- next part --------------
An HTML attachment was scrubbed...
URL:
<http://www.haskell.org/pipermail/beginners/attachments/20140420/d5f7870d/attachment-0001.html>
------------------------------
Message: 2
Date: Sun, 20 Apr 2014 21:57:58 +0900
From: "S. H. Aegis" <[email protected]>
To: "[email protected]" <[email protected]>
Subject: [Haskell-beginners] About scope range: outside of the
do-block
Message-ID:
<cajp-nqyhm8ku8uwokdwfdt8uiech1aiqixhjvmzm9a3vgg7...@mail.gmail.com>
Content-Type: text/plain; charset="utf-8"
I'm trying to solve problem 22 of Euler Project. (
http://projecteuler.net/problem=22)
I got file contents using " names <- readFile "names.txt" " in "do-block".
When I try to use 'names' outside of "do-block", I get following error
message;
namesScores.hs:30:35: Not in scope: `names'
Failed, modules loaded: none.
Is there any other ways to use variables outside of the "do-block"?
Sorry for taking your time.
Thank you.
My code is...
-------------------------------------------------------------
import Data.List
import Data.List.Split
import Data.Char
import Data.Maybe
main :: IO()
main = do
names <- readFile "names.txt"
print $ sum $ map (\x -> getPosition x * getScore x) names
getPosition :: String -> Int
getPosition x = fromMaybe 0 (elemIndex x sortedNamesList) + 1
getScore :: String -> Int
getScore xs = sum $ map (\x -> ord x - 64) xs
sortedNamesList :: [String]
sortedNamesList = sort $ nameList names
nameList :: String -> [String]
nameList = split (dropDelims . dropBlanks $ oneOf ",\"")
Error message is...
------------------------------------------------------------------
Prelude> :l namesScores.hs
[1 of 1] Compiling Main ( namesScores.hs, interpreted )
namesScores.hs:31:35: Not in scope: `names'
Failed, modules loaded: none.
-------------- next part --------------
An HTML attachment was scrubbed...
URL:
<http://www.haskell.org/pipermail/beginners/attachments/20140420/9acc12c5/attachment-0001.html>
------------------------------
Message: 3
Date: Sun, 20 Apr 2014 15:09:42 +0200
From: Roel van Dijk <[email protected]>
To: The Haskell-Beginners Mailing List - Discussion of primarily
beginner-level topics related to Haskell <[email protected]>
Subject: Re: [Haskell-beginners] About scope range: outside of the
do-block
Message-ID:
<CABw4ky5ciaVh=hmq7hrxv3rxdxoo8uavogyftt2plwcg8yw...@mail.gmail.com>
Content-Type: text/plain; charset="utf-8"
On 20 April 2014 14:57, S. H. Aegis <[email protected]> wrote:
>
> Is there any other ways to use variables outside of the "do-block"?
>
You can pass the list of names as an argument where it is needed.
> import Data.List
> import Data.List.Split
> import Data.Char
> import Data.Maybe
>
> main :: IO ()
> main = do
> names <- readFile "names.txt"
> print $ sum $ map (\x -> getPosition names x * getScore x) names
>
> getPosition :: [String] -> String -> Int
> getPosition names x = fromMaybe 0 (elemIndex x $ sortedNamesList names) + 1
>
> getScore :: String -> Int
> getScore xs = sum $ map (\x -> ord x - 64) xs
>
> sortedNamesList :: [String] -> [String]
> sortedNamesList names = sort $ nameList names
>
> nameList :: String -> [String]
> nameList = split (dropDelims . dropBlanks $ oneOf ",\"")
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL:
<http://www.haskell.org/pipermail/beginners/attachments/20140420/63492b5e/attachment-0001.html>
------------------------------
Message: 4
Date: Sun, 20 Apr 2014 15:17:10 +0200
From: Norbert Melzer <[email protected]>
To: The Haskell-Beginners Mailing List - Discussion of primarily
beginner-level topics related to Haskell <[email protected]>
Subject: Re: [Haskell-beginners] About scope range: outside of the
do-block
Message-ID:
<ca+bcvsu0o40w+ypnz1flszbwpg9j_o5znwqtcchgtksdsy9...@mail.gmail.com>
Content-Type: text/plain; charset="utf-8"
Am 20.04.2014 14:58 schrieb "S. H. Aegis" <[email protected]>:
> namesScores.hs:31:35: Not in scope: `names'
> Failed, modules loaded: none.
The code you posted has only 21 lines while your error message denotes an
error in line 31, so there is something missing.
-------------- next part --------------
An HTML attachment was scrubbed...
URL:
<http://www.haskell.org/pipermail/beginners/attachments/20140420/a5c0c403/attachment-0001.html>
------------------------------
Message: 5
Date: Sun, 20 Apr 2014 22:38:05 +0900
From: "S. H. Aegis" <[email protected]>
To: The Haskell-Beginners Mailing List - Discussion of primarily
beginner-level topics related to Haskell <[email protected]>
Subject: Re: [Haskell-beginners] About scope range: outside of the
do-block
Message-ID:
<cajp-nqxztb777q7yx7v8sej0knjt2tesedrktzss7byfgdt...@mail.gmail.com>
Content-Type: text/plain; charset="utf-8"
Oh, I'm sorry.
I delete some of commented lines.
Sorry for confusing.
Error occurred in
sortedNamesList = sort $ nameList names
Thank you.
2014-04-20 22:17 GMT+09:00 Norbert Melzer <[email protected]>:
>
> Am 20.04.2014 14:58 schrieb "S. H. Aegis" <[email protected]>:
>
> > namesScores.hs:31:35: Not in scope: `names'
> > Failed, modules loaded: none.
>
> The code you posted has only 21 lines while your error message denotes an
> error in line 31, so there is something missing.
>
> _______________________________________________
> Beginners mailing list
> [email protected]
> http://www.haskell.org/mailman/listinfo/beginners
>
>
--
Sok Ha, CHANG
Dr. Chang's Clinic. #203. 503-23. AmSa-Dong, GangDong-Gu, Seoul.
Tel: +82-2-442-7585
-------------- next part --------------
An HTML attachment was scrubbed...
URL:
<http://www.haskell.org/pipermail/beginners/attachments/20140420/9570e7f0/attachment-0001.html>
------------------------------
Message: 6
Date: Sun, 20 Apr 2014 22:47:10 +0900
From: "S. H. Aegis" <[email protected]>
To: The Haskell-Beginners Mailing List - Discussion of primarily
beginner-level topics related to Haskell <[email protected]>
Subject: Re: [Haskell-beginners] About scope range: outside of the
do-block
Message-ID:
<cajp-nqw_r-7keu5zbdazalt6dkqjentbm_g-vwpandx9jyx...@mail.gmail.com>
Content-Type: text/plain; charset="utf-8"
It works !
Thank you so much !
Have a nice day~
2014-04-20 22:09 GMT+09:00 Roel van Dijk <[email protected]>:
> On 20 April 2014 14:57, S. H. Aegis <[email protected]> wrote:
>>
>> Is there any other ways to use variables outside of the "do-block"?
>>
>
> You can pass the list of names as an argument where it is needed.
>
>
>> import Data.List
>> import Data.List.Split
>> import Data.Char
>> import Data.Maybe
>>
>> main :: IO ()
>> main = do
>> names <- readFile "names.txt"
>> print $ sum $ map (\x -> getPosition names x * getScore x) names
>>
>> getPosition :: [String] -> String -> Int
>> getPosition names x = fromMaybe 0 (elemIndex x $ sortedNamesList names) +
>> 1
>>
>> getScore :: String -> Int
>> getScore xs = sum $ map (\x -> ord x - 64) xs
>>
>> sortedNamesList :: [String] -> [String]
>> sortedNamesList names = sort $ nameList names
>>
>> nameList :: String -> [String]
>> nameList = split (dropDelims . dropBlanks $ oneOf ",\"")
>>
>
>
>
> _______________________________________________
> Beginners mailing list
> [email protected]
> http://www.haskell.org/mailman/listinfo/beginners
>
>
--
Sok Ha, CHANG
Dr. Chang's Clinic. #203. 503-23. AmSa-Dong, GangDong-Gu, Seoul.
Tel: +82-2-442-7585
-------------- next part --------------
An HTML attachment was scrubbed...
URL:
<http://www.haskell.org/pipermail/beginners/attachments/20140420/452b2234/attachment.html>
------------------------------
Subject: Digest Footer
_______________________________________________
Beginners mailing list
[email protected]
http://www.haskell.org/mailman/listinfo/beginners
------------------------------
End of Beginners Digest, Vol 70, Issue 38
*****************************************