簡體   English   中英

第一個Haskell程序

[英]First Haskell program

我在Haskell中寫了一些代碼來學習haskell。 看起來像:

first :: [Int] -> Int -> Int
first [] x     = x
first xs y     = y

我想表達的是,我得到一個列表([]或xs)和一個參數(x或y),無論列表看起來如何,都應該顯示該參數。

因此,當我編寫以下代碼時:首先[1,2,3] 4,然后ghci說:

Couldnt match expected type ´a0 -> t´ wth actual type ´[t0]´. Relevant bindings       
include it :: t(boudn at <>:6:1)but its type ´[t 0]´ has none
In the expression : [1,2,3] 4
In an equation for ´it´: it = [1,2,3] 4

我該如何解決? 我犯了什么錯誤? 有人能幫我嗎?

看起來您使用[1,2,3,4] 4而不是first [1,2,3,4] 4

> [1,2,3,4] 4

<interactive>:1:0:
    Couldn't match expected type `t1 -> t' against inferred type `[a]'
    In the expression: [1, 2, 3, 4] 4
    In the definition of `it': it = [1, 2, 3, 4] 4

順便說一句,您對first的定義與以下相同:

first :: [Int] -> Int -> Int
first _ x     = x

這意味着無論第一個參數是什么,總是返回第二個參數。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM