On syntactic sugar

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

On syntactic sugar

Lauri Oksanen
Hi,

I'm writing my first real Haskell program and I came up with the following code snippet.

---
let x' = x \+ dist \* dir
    nx' =  normal geometry
    wi = (-1) \* dir
    in do
        (p, wo) <- brdfCosSampling reflector nx' wi    
        let color' = p \** color
            q = min 1 (scalarContribution p)
            in do
                sampler <- biasedCoinSampler q
                        (radianceSampler surfaces x' wo (q \* color'))
                        (terminalRadianceSampler surfaces x' nx' ((1-q) \* color'))
                sampler
---

This works just fine but I don't like the way I had to indent the code because of alternating lets and dos.
I would like to indent the code more like an imperative code i.e. like this

---
let {
    x' = x \+ dist \* dir ;
    nx' =  normal geometry ;
    wi = (-1) \* dir ;
} in do {
    (p, wo) <- brdfCosSampling reflector nx' wi ;
let {
    color' = p \** color ;
    q = min 1 (scalarContribution p) ;
} in do {
    sampler <- biasedCoinSampler q
            (radianceSampler surfaces x' wo (q \* color'))
            (terminalRadianceSampler surfaces x' nx' ((1-q) \* color')) ;
    sampler ;
}}
---

but without braces and semicolons. Following works also, but is ugly (and probably less efficient?).

---
do
    x' <- return $ x \+ dist \* dir
    nx' <- return $  normal geometry
    wi <- return $ (-1) \* dir
    (p, wo) <- brdfCosSampling reflector nx' wi
    color' <- return $ p \** color
    q <- return $ min 1 (scalarContribution p)
    sampler <- biasedCoinSampler q
            (radianceSampler surfaces x' wo (q \* color'))
            (terminalRadianceSampler surfaces x' nx' ((1-q) \* color'))
    sampler
---

Is there some nice trick to do this?

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

Re: On syntactic sugar

Denis Bueno
2008/5/12 Lauri Oksanen <[hidden email]>:

> Hi,
>
> I'm writing my first real Haskell program and I came up with the following
> code snippet.
>
> ---
> let x' = x \+ dist \* dir
>     nx' =  normal geometry
>     wi = (-1) \* dir
>     in do
>         (p, wo) <- brdfCosSampling reflector nx' wi
>         let color' = p \** color
>             q = min 1 (scalarContribution p)
>             in do
>                 sampler <- biasedCoinSampler q
>                         (radianceSampler surfaces x' wo (q \* color'))
>                         (terminalRadianceSampler surfaces x' nx' ((1-q) \*
> color'))
>                 sampler
> ---
>
> This works just fine but I don't like the way I had to indent the code
> because of alternating lets and dos.
> I would like to indent the code more like an imperative code i.e. like this

You can use let in a do-block, just don't use "in":

do x <- something
   let y = ... x ...
   return (x + y)

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

Re: On syntactic sugar

Bugzilla from alfonso.acosta@gmail.com
In reply to this post by Lauri Oksanen
2008/5/12 Lauri Oksanen <[hidden email]>:

> Hi,
>
> I'm writing my first real Haskell program and I came up with the following
> code snippet.
>
> ---
> let x' = x \+ dist \* dir
>     nx' =  normal geometry
>     wi = (-1) \* dir
>     in do
>          (p, wo) <- brdfCosSampling reflector nx' wi
>         let color' = p \** color
>             q = min 1 (scalarContribution p)
>             in do
>                 sampler <- biasedCoinSampler q
>                          (radianceSampler surfaces x' wo (q \* color'))
>                         (terminalRadianceSampler surfaces x' nx' ((1-q) \*
> color'))
>                 sampler

Do-notation already offers what you want. Try this out:


 do let x' = x \+ dist \* dir
           nx' =  normal geometry
           wi = (-1) \* dir
      (p, wo) <- brdfCosSampling reflector nx' wi
      let color' = p \** color
           q = min 1 (scalarContribution p)
       sampler <- biasedCoinSampler q
                          (radianceSampler surfaces x' wo (q \* color'))
                         (terminalRadianceSampler surfaces x' nx'
((1-q) \* color'))
       sampler
_______________________________________________
Haskell-Cafe mailing list
[hidden email]
http://www.haskell.org/mailman/listinfo/haskell-cafe
Reply | Threaded
Open this post in threaded view
|

Re: On syntactic sugar

Jake McArthur
In reply to this post by Lauri Oksanen
On May 12, 2008, at 11:41 AM, Lauri Oksanen wrote:

> let x' = x \+ dist \* dir
>     nx' =  normal geometry
>     wi = (-1) \* dir
>     in do
>         (p, wo) <- brdfCosSampling reflector nx' wi
>         let color' = p \** color
>             q = min 1 (scalarContribution p)
>             in do
>                 sampler <- biasedCoinSampler q
>                         (radianceSampler surfaces x' wo (q \* color'))
>                         (terminalRadianceSampler surfaces x' nx' ((1-
> q) \* color'))
>                 sampler


Something like the following should work:

do let x'     = x \+ dist \* dir
    let nx'    = normal geometry
    let wi     = (-1) \* dir
    (p, wo)   <- brdfCosSampling reflector nx' wi
    let color' = p \** color
    let q      = min 1 (scalarContribution p)
    sampler   <- biasedCoinSampler q
                    (radianceSampler surfaces x' wo (q \* color'))
                    (terminalRadianceSampler surfaces x' nx' ((1-q) \*  
color'))
    sampler

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