Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
2.3k views
in Technique[技术] by (71.8m points)

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

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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{..}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...