相关疑难解决方法(0)

有什么区别.(点)和$(美元符号)?

(.)和美元符号有($)什么区别?据我了解,它们都是不需要使用括号的语法糖.

syntax haskell function-composition

682
推荐指数
10
解决办法
15万
查看次数

使用foldr编写foldl

Real World Haskell中,第4章.函数式编程

用foldr写foldl:

-- file: ch04/Fold.hs
myFoldl :: (a -> b -> a) -> a -> [b] -> a

myFoldl f z xs = foldr step id xs z
    where step x g a = g (f a x)
Run Code Online (Sandbox Code Playgroud)

上面的代码让我很困惑,有人打电话给dps用一个有意义的名字重写它,使它更清晰:

myFoldl stepL zeroL xs = (foldr stepR id xs) zeroL
where stepR lastL accR accInitL = accR (stepL accInitL lastL)
Run Code Online (Sandbox Code Playgroud)

其他人,Jef G,通过提供一个例子并逐步展示基础机制,做得非常出色:

myFoldl (+) 0 [1, 2, 3]
= (foldR step id [1, 2, …
Run Code Online (Sandbox Code Playgroud)

recursion haskell fold

73
推荐指数
4
解决办法
1万
查看次数

在函数式编程中什么是"currying"?

写作一个未经重构的命令和OO程序员......

最近和Erlang以及Haskell搞砸了.我喜欢Erlang,还不确定Haskell.功能似乎更像数学而不是编程,希望有意义.功能编程似乎非常强大.

阅读关于互动函数编程的文档我经常遇到"currying"这个词.我似乎只找到了一些有点过头的文档 - 很多术语都没有定义.

什么是currying?

我已经找了类似的已经发布的问题,但没有找到任何东西,所以请随意指出我已建立的线程.

functional-programming currying

43
推荐指数
0
解决办法
1万
查看次数

Haskell:使用两个浮动参数的组合函数失败

我试图用类型(Floating a) => a -> a -> a的函数组成一个类型的函数(Floating a) => a -> a来获得类型的函数(Floating a) => a -> a -> a.我有以下代码:

test1 :: (Floating a) => a -> a -> a
test1 x y = x

test2 :: (Floating a) => a -> a
test2 x = x

testBoth :: (Floating a) => a -> a -> a
testBoth = test2 . test1
--testBoth x y = test2 (test1 x y)
Run Code Online (Sandbox Code Playgroud)

但是,当我在GHCI中编译它时,我收到以下错误:

/path/test.hs:8:11:
    Could not …
Run Code Online (Sandbox Code Playgroud)

haskell function-composition

6
推荐指数
1
解决办法
1100
查看次数

为什么更高阶的程序?

因此,如果一种语言提供更高阶的程序,那么我可以有返回程序的程序.就像是:

(define (Proc a b c)
  (lambda (x) ( #| method body here in terms of a b c and x |# )))
Run Code Online (Sandbox Code Playgroud)

要创建新程序,我会做类似的事情:

(define ProcA (Proc a1 b1 c1)) ; Would create ProcA that has 1 argument
Run Code Online (Sandbox Code Playgroud)

类似的任务可以用不支持高阶过程的语言来完成,方法是定义Proc4个而不是3个参数并调用这个过程来定义ProcA,如:

(define (Proc a b c x) ( #| method body -- does not return any procedure |# )
(define (ProcA x) (Proc a1 b1 c1 x))
Run Code Online (Sandbox Code Playgroud)

那么为什么有关高阶程序的模糊呢?我错过了什么吗?

scheme closures abstraction programming-languages functional-programming

5
推荐指数
1
解决办法
1344
查看次数