Send Beginners mailing list submissions to
        [email protected]

To subscribe or unsubscribe via the World Wide Web, visit
        http://mail.haskell.org/cgi-bin/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:  best way to code this~~ (Alex Hammel)
   2. Re:  Command line interface programming (Henk-Jan van Tuyl)
   3.  I think someone had a complicated program to use brackets
      for array indexing - is it possible to use a DSL for this? (KC)
   4. Re:  [Haskell-cafe] I think someone had a complicated program
      to use brackets for array indexing - is it possible to use a DSL
      for this? (KC)
   5. Re:  [Haskell-cafe] I think someone had a complicated program
      to use brackets for array indexing - is it possible to use a DSL
      for this? (Brandon Allbery)


----------------------------------------------------------------------

Message: 1
Date: Mon, 01 Jun 2015 16:10:47 +0000
From: Alex Hammel <[email protected]>
To: The Haskell-Beginners Mailing List - Discussion of primarily
        beginner-level topics related to Haskell <[email protected]>
Subject: Re: [Haskell-beginners] best way to code this~~
Message-ID:
        <ca+_xfeqwv+htzjhzoakljioqrk0aohk+lnvew-dvphz4cmi...@mail.gmail.com>
Content-Type: text/plain; charset="utf-8"

>
> > f as alist = [ b | (a, b) <- alist, a `elem` as ]
> >
> > perhaps?
>
> perhaps.  i have no idea how that works.  but don't spoil it for me
> though, i'm going to go of and study it :-)
>

It's a list comprehension <https://wiki.haskell.org/List_comprehension>. I
hope that's not too much of a spoiler :)


> unsure what the ~(ts, fs) syntax is though, removing the `~` doesn't seem
> to matter.
>

Makes it lazier
<http://en.wikibooks.org/wiki/Haskell/Laziness#Lazy_pattern_matching>


>
> this seems fairly clean. i noticed that partition simply uses foldr.  it
> looks like select is just a helper so that partition isn't cluttered.  i'm
> unsure why select was broken out as a separate function instead of just
> being in a where clause.  possibly to be able to assign it a an explicit
> type signature ?
>

You can assign type signatures in `let` and `where` clauses:

f = let i :: Int
        i = 1
    in increment i
    where
        increment :: Int -> Int
        increment = (+) 1

This is often quite a good idea. Type signatures are excellent,
machine-checkable documentation.


> extract :: [(String,b)] -> [String] -> ([b], [String])
> extract alist l =
>   let inList s = lookup (uppercase s) alist
>       (l1, l2) = partitionMaybe inList l
>   in
>    (l1, l2)
>
> partitionMaybe :: (a -> Maybe b) -> [a] -> ([b],[a])
> partitionMaybe p xs = foldr (select p) ([],[]) xs
>
> select :: (a -> Maybe b) -> a -> ([b], [a]) -> ([b], [a])
> select p x ~(ts,fs) | isJust y = ((fromJust y):ts,fs)
>                     | otherwise = (ts, x:fs)
>   where
>     y = p x
>

 Couple of things:

`fromJust` is a code smell. In general, you should at least consider
replacing it with `fromMaybe (error msg)` where `msg` is a more useful
error message than '*** Exception: Maybe.fromJust: Nothing'.

Of course in this particular case, that will never come up because you can
prove that `y` is never Nothing using the guard condition. But in that
case, it's better practice to use pattern matching and let the compiler
prove it for you:

select p y ~(xs,ys) = acc $ p y
    where
        acc (Just x) = (x:xs,   ys)
        acc Nothing  = (  xs, y:ys)


The `partitionMaybe` function looks a bit odd to me. The computation you're
trying to express is 'If this computation succedes, return whatever value
it returned, but if it fails return some default value'. This is not a
natural use of the Maybe data type: that's what Either is for. There's even
a `partitionEithers` library function. `lookup` returns Maybe, not Either,
but we can fix that.

Here's my go (I've changed around the API a little bit as well)

toEither :: a -> (Maybe b) -> Either a b
toEither _ (Just x) = Right x
toEither y Nothing  = Left  y

lookupEither :: Eq a => a -> [(a,b)] -> Either a b
lookupEither key assocs = toEither key $ lookup key assocs

uppercase :: String -> String
uppercase = map toUpper

extract :: [String] -> [(String,b)] -> ([String],[b])
extract xs assocs =
    let xs' = map uppercase xs
        eithers = map (\x -> lookupEither x assocs) xs'
        -- spoilers: same as [ lookupEither x assocs | x <- xs' ]
    in partitionEithers eithers
main :: IO ()main = print $ extract ["Foo", "Bar"] [("FOO",1), ("BAZ",2)]


This differs slightly from your algorithm in that it returns
'(["BAR"],[1]), where yours would return (["Bar"],[1]). If preserving
the original case in the output, I would either write a
`caseInsensitiveLookup` function, or use a case insensitive text
<https://hackage.haskell.org/package/case-insensitive-0.2.0.1/docs/Data-CaseInsensitive.html>
data type.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: 
<http://mail.haskell.org/pipermail/beginners/attachments/20150601/0d0b5b2d/attachment-0001.html>

------------------------------

Message: 2
Date: Mon, 01 Jun 2015 18:56:51 +0200
From: "Henk-Jan van Tuyl" <[email protected]>
To: "The Haskell-Beginners Mailing List - Discussion of primarily
        beginner-level topics related to Haskell" <[email protected]>,
        "Dananji Liyanage" <[email protected]>
Subject: Re: [Haskell-beginners] Command line interface programming
Message-ID: <op.xzj9c6fnpz0j5l@alquantor>
Content-Type: text/plain; charset=iso-8859-15; format=flowed;
        delsp=yes

On Mon, 01 Jun 2015 12:11:54 +0200, Dananji Liyanage <[email protected]>  
wrote:

> Right now, I have a sample program written using  
> 'System.Console.Haskeline'
> package.
>
> Can someone please give me some additional material, which I can read on
> with respect to this?

You could look at the reverse dependencies for Haskeline:
   http://packdeps.haskellers.com/reverse/haskeline

Regards,
Henk-Jan van Tuyl


-- 
Folding@home
What if you could share your unused computer power to help find a cure? In  
just 5 minutes you can join the world's biggest networked computer and get  
us closer sooner. Watch the video.
http://folding.stanford.edu/


http://Van.Tuyl.eu/
http://members.chello.nl/hjgtuyl/tourdemonad.html
Haskell programming
--


------------------------------

Message: 3
Date: Mon, 1 Jun 2015 13:17:07 -0700
From: KC <[email protected]>
To: haskell-cafe <[email protected]>, Haskell Beginners
        <[email protected]>
Subject: [Haskell-beginners] I think someone had a complicated program
        to use brackets for array indexing - is it possible to use a DSL for
        this?
Message-ID:
        <camlkxykiggezzt5rgybh1_03tsx0kexfu0dwk2oydr1cfj+...@mail.gmail.com>
Content-Type: text/plain; charset="utf-8"

I think someone had a complicated program to use brackets for array
indexing - is it possible to use a DSL for this?

That is, to use myArray [5] and have a DSL convert it to standard Haskell
syntax

--
--

Sent from an expensive device which will be obsolete in a few months! :D

Casey
-------------- next part --------------
An HTML attachment was scrubbed...
URL: 
<http://mail.haskell.org/pipermail/beginners/attachments/20150601/1d4ba523/attachment-0001.html>

------------------------------

Message: 4
Date: Mon, 1 Jun 2015 13:29:27 -0700
From: KC <[email protected]>
To: Tikhon Jelvis <[email protected]>
Cc: Haskell Beginners <[email protected]>, haskell-cafe
        <[email protected]>
Subject: Re: [Haskell-beginners] [Haskell-cafe] I think someone had a
        complicated program to use brackets for array indexing - is it
        possible to use a DSL for this?
Message-ID:
        <CAMLKXy=izywmpoxnxmf-zksebw8gssi_mczxgd6z6ejgyqa...@mail.gmail.com>
Content-Type: text/plain; charset="utf-8"

Then a tuple might work better as input to a function to index into an array

e.g. myArray  (5)

--
--

Sent from an expensive device which will be obsolete in a few months! :D

Casey

On Jun 1, 2015 1:21 PM, "Tikhon Jelvis" <[email protected]> wrote:

> You could make myArray a function that takes a list as an input. Of
> course, all your other array functions have to account for this too. A
> potential advantage is that this approach leaves the underlying array type
> abstract, so you could mix and match different data structure on the
> backend. (IntMap, Array, Vector? etc.)
>
> A disadvantage is that this is non-standard usage which could be confusing
> to people and there's no way I know of to statically ensure the list passed
> in always had one element. That is, myArray [1, 2] would be legal and
> result in a runtime error.
>
> I don't know of a way to do it while using a normal array type directly.
>
> On Mon, Jun 1, 2015 at 1:17 PM, KC <[email protected]> wrote:
>
>> I think someone had a complicated program to use brackets for array
>> indexing - is it possible to use a DSL for this?
>>
>> That is, to use myArray [5] and have a DSL convert it to standard Haskell
>> syntax
>>
>> --
>> --
>>
>> Sent from an expensive device which will be obsolete in a few months! :D
>>
>> Casey
>>
>>
>> _______________________________________________
>> Haskell-Cafe mailing list
>> [email protected]
>> http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe
>>
>>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: 
<http://mail.haskell.org/pipermail/beginners/attachments/20150601/de434c79/attachment-0001.html>

------------------------------

Message: 5
Date: Mon, 1 Jun 2015 16:35:40 -0400
From: Brandon Allbery <[email protected]>
To: KC <[email protected]>
Cc: Haskell Beginners <[email protected]>, Tikhon Jelvis
        <[email protected]>, haskell-cafe <[email protected]>
Subject: Re: [Haskell-beginners] [Haskell-cafe] I think someone had a
        complicated program to use brackets for array indexing - is it
        possible to use a DSL for this?
Message-ID:
        <cakfcl4xk+g5mu-syvloohjoc2whrxay6ktvpgjhbet_wc46...@mail.gmail.com>
Content-Type: text/plain; charset="utf-8"

On Mon, Jun 1, 2015 at 4:29 PM, KC <[email protected]> wrote:

> Then a tuple might work better as input to a function to index into an
> array
>
> e.g. myArray  (5)
>

Aside from 1-tuples not being a thing? (That's the same as (myArray 5).)

-- 
brandon s allbery kf8nh                               sine nomine associates
[email protected]                                  [email protected]
unix, openafs, kerberos, infrastructure, xmonad        http://sinenomine.net
-------------- next part --------------
An HTML attachment was scrubbed...
URL: 
<http://mail.haskell.org/pipermail/beginners/attachments/20150601/eaf17a35/attachment.html>

------------------------------

Subject: Digest Footer

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


------------------------------

End of Beginners Digest, Vol 84, Issue 2
****************************************

Reply via email to