Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

is there any syntax for non-recursive binding in Haskell, just like the difference between `let` and `let rec` in similar languages?

Tags:

haskell

Non-recursive binding allows me to shadow bound value, for instance:

b a = let norec a = a + 10 in a

here let norec created by myself means a let binding but not recursive.

This is extremely helpful when using record wildcards:

data MyRecord = MyRecord{ {- vary huuuuuge set of definitions -} }

foo MyRecord{..} = let norec field1 = field1 + 1
                             field2 = modify field2
                             {- some other modifications to the fields -}
                    in MyRecord{..}

Is that achievable? Or how do you deal with it in your cases?

like image 950
Jason Hu Avatar asked Aug 04 '15 18:08

Jason Hu


1 Answers

Are record wildcards actually that useful here? The usual old way of doing things looks quite concise to me:

foo r = r { field1 = field1 r + 1, field2 = modify (field2 r) }

The direct answer to your question is that there is no non-recursive analog of let in Haskell; though you can use the Identity monad to sort of hack something into place:

foo MyRecord{..} = runIdentity $ do
    field1 <- return (field1 + 1)
    field2 <- return (modify field2)
    return MyRecord{..}
like image 152
Daniel Wagner Avatar answered Nov 15 '22 05:11

Daniel Wagner